test: Stabilize AI unit tests against cold heavy-load timeouts (#31592)

Co-authored-by: n8n-cat-bot[bot] <n8n-cat-bot[bot]@users.noreply.github.com>
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
n8n-cat-bot[bot] 2026-06-02 21:13:49 +00:00 committed by GitHub
parent e8089b4ec7
commit 098fcb39cb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 0 deletions

View File

@ -1,5 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unsafe-argument */
import { encodingForModel } from 'src/utils/tokenizer/tiktoken';
import {
estimateTokensByCharCount,
estimateTextSplitsByTokens,
@ -7,6 +8,15 @@ import {
} from 'src/utils/tokenizer/token-estimator';
describe('token-estimator', () => {
// Warm both BPE encodings before the timed test bodies. `cl100k_base` (gpt-4) is ~1 MB and
// `o200k_base` (gpt-4o) is ~2.2 MB; the first test that touches a fresh encoding pays a
// readFile + jsonParse + 200k-entry Tiktoken construct that under CI CPU contention can
// exceed the default 5s per-test timeout. Scope the headroom to this hook so the default
// per-test timeout still guards every other test in the file.
beforeAll(async () => {
await Promise.all([encodingForModel('gpt-4'), encodingForModel('gpt-4o')]);
}, 30_000);
describe('estimateTokensByCharCount', () => {
it('should estimate tokens for text using default model', () => {
const text = 'This is a test text with some content.';

View File

@ -77,6 +77,13 @@ vi.mock('../lifecycles/introspection-analysis', () => ({
createIntrospectionAnalysisLifecycle: () => ({}),
}));
// Stub the code-builder module so importing `../cli` doesn't drag in the heavy
// langchain / workflow-builder graph. `runEvaluation` is mocked below, so the
// generation path never actually invokes `CodeWorkflowBuilder`.
vi.mock('@/code-builder', () => ({
CodeWorkflowBuilder: vi.fn(),
}));
vi.mock('../index', () => ({
runEvaluation: (...args: unknown[]): unknown => mockRunEvaluation(...args),
createConsoleLifecycle: (...args: unknown[]): unknown => mockCreateConsoleLifecycle(...args),