diff --git a/AGENTS.md b/AGENTS.md index df51d782c..600990a4e 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -49,6 +49,7 @@ vp run build:core # Build shared packages, CLI, and platform r vp run test # Affected local tests, with integration fallbacks vp run test --list # Preview affected test files without builds/hooks vp run test --base origin/main # Include committed branch changes +vp run test:iterate # Native dependency-based iteration; see TESTING.md for limits vp test run # Run tests (unit + integration tests) vpr test:all # Format, lint, build, typecheck, and run all SDK/platform tests vp test run packages/void # Only test the core plugin diff --git a/TESTING.md b/TESTING.md index c92f9fbda..0369bd4bd 100644 --- a/TESTING.md +++ b/TESTING.md @@ -28,6 +28,21 @@ while building unrelated apps, including fixtures that have not generated their Type-only contracts use `*.typecheck.ts`. Package typechecking verifies them; the Vitest runtime does not need to execute their `expectTypeOf` assertions. +### Iterating on imported code + +`vp run test:iterate ` uses Vitest's native dependency records to select files changed +since their last run. New tests and previously failed tests run too. The task itself is never +cached. For example: + +```sh +vp run test:iterate packages/void/test/unit/plugin-routing.test.ts +``` + +This is an iteration shortcut. Records do not cover arbitrary filesystem reads, newly added +routes, subprocess inputs, or environment changes. For those changes, use `vp run test` with its +integration fallbacks, or run the known consumer without incremental selection. Finish with the +affected and full verification below. + ### Affected local tests `vp run test` is the local affected-tests default; `vp test run`, `vp run test:full`, and CI still @@ -109,8 +124,9 @@ The cf runner's transport checks live in `cf-version-transport.test.ts` so real not serialize the whole runner suite. Both files use the same fixture helpers with per-file isolated roots and cleanup; keep their real HTTP, process, and deadline assertions. Unit files and most API integration files use isolated worker threads to reduce startup time. On Linux and macOS, -API integration files and the PGlite unit files (`unit-pglite`, `platform-unit-pglite`) use forks to isolate PGlite's -WebAssembly JIT allocations after native Node 24 crashes in shared worker processes. API tests +API integration files and platform PGlite unit files (`platform-unit-pglite`) use forks to isolate PGlite's +WebAssembly JIT allocations after native Node 24 crashes in shared worker processes. +SDK PGlite units (`unit-pglite`) use forks on every platform; Windows also exhibits the JIT teardown crash. API tests that change the process timezone also use forks, because threads cannot change Node's timezone. Select every API file with `vp test run --project 'platform-api-integration*'`. SDK integration files use forks. This keeps database engines from competing with all framework compilers at startup. Keep module @@ -136,6 +152,18 @@ the cache for V8 coverage, and `NODE_V8_COVERAGE` disables it too, to preserve p ## Profiling slow tests +Capture comparable full-suite or focused measurements with the opt-in reporter: + +```sh +vp run test:benchmark --outputFile=/tmp/void-tests.json +vp run test:benchmark packages/void/test/unit/plugin-routing.test.ts --outputFile=/tmp/void-routing.json +``` + +Reports contain total elapsed time, per-file setup/import/execution costs, test names and outcomes, +and machine/toolchain details. Every selected test executes. Compare identical selections on the +same machine and record whether builds and transform caches were prepared. Per-file durations +overlap; summing them does not measure wall-clock savings. + Vitest 5's import diagnostics are opt-in so ordinary runs stay quiet: ```sh diff --git a/packages/void/src/cli/cloudflare-operation-process.ts b/packages/void/src/cli/cloudflare-operation-process.ts index dc10a1691..cc94ee29a 100644 --- a/packages/void/src/cli/cloudflare-operation-process.ts +++ b/packages/void/src/cli/cloudflare-operation-process.ts @@ -20,15 +20,8 @@ import { readCfUploadOutput, safePositional, } from './cf-command-values.ts'; -import { persistCfKeyringPreference, readCfSessionCredential } from './cf-session.ts'; import { getCfCliVersion, getVoidUserAgent } from './cf-user-agent.ts'; import { publishCloudflareWorkerSettings } from './cf-worker-settings.ts'; -import { - cloudflareDeviceCode, - cloudflareVerificationUrl, - printCloudflareLoginLink, - printCloudflareLoginPrompt, -} from './cloudflare-login-output.ts'; import { platformWorkerUploadAttribution } from './platform-worker-user-agent.ts'; import { sanitizeTerminalLogText } from './terminal-log-text.ts'; @@ -148,7 +141,13 @@ function runCf( return result(child.status, child.stdout ?? '', stderr); } -function runInteractiveCf(command: Array): Promise { +async function runInteractiveCf(command: Array): Promise { + const { + cloudflareDeviceCode, + cloudflareVerificationUrl, + printCloudflareLoginLink, + printCloudflareLoginPrompt, + } = await import('./cloudflare-login-output.ts'); return new Promise((resolve) => { const child = spawn(process.execPath, [cf, ...command, '--quiet'], { cwd: process.cwd(), @@ -556,6 +555,7 @@ async function sessionCredential(): Promise< if (state.authenticated !== true || state.tokenValid !== true) { return { failure: NO_OAUTH_BEARER }; } + const { readCfSessionCredential } = await import('./cf-session.ts'); const credential = readCfSessionCredential(process.cwd(), state); return credential ? { type: 'oauth', token: credential } : { failure: NO_OAUTH_BEARER }; } @@ -564,6 +564,7 @@ async function auth(): Promise { if (operation.type === 'auth.login') { const loggedIn = await runInteractiveCf(['auth', 'login', '--force']); if (loggedIn.status === 0) { + const { persistCfKeyringPreference } = await import('./cf-session.ts'); persistCfKeyringPreference(process.cwd()); } return loggedIn; @@ -581,6 +582,7 @@ async function auth(): Promise { return result(1, '', 'Cloudflare authentication is required.', { loggedIn: false }); } if (String(identity.authSource).startsWith('OAuth token from ')) { + const { persistCfKeyringPreference } = await import('./cf-session.ts'); persistCfKeyringPreference(process.cwd()); } return result(0, '', called.stderr, { diff --git a/packages/void/test/unit/cloudflare-operation-process.test.ts b/packages/void/test/unit/cloudflare-operation-process.test.ts index c18006709..77d7148af 100644 --- a/packages/void/test/unit/cloudflare-operation-process.test.ts +++ b/packages/void/test/unit/cloudflare-operation-process.test.ts @@ -19,6 +19,66 @@ import { tokenEnv, } from '../helpers/cloudflare-operation-process.ts'; +it.each(['CLOUDFLARE_API_TOKEN', 'CF_API_TOKEN'] as const)( + 'keeps OAuth storage and terminal UI unloaded when %s supplies the bearer', + async (name) => { + const root = temporaryDirectory('void-token-only-startup-'); + const preload = join(root, 'reject-auth-imports.mjs'); + writeFileSync( + preload, + `import { registerHooks } from 'node:module'; +registerHooks({ resolve(specifier, context, nextResolve) { + if (specifier.includes('/cf-session') || specifier.includes('/cloudflare-login-output') || + specifier.startsWith('@cloudflare/workers-auth') || specifier === '@voidzero-dev/vite-plus-prompts') { + throw new Error('Token-only operations must not load OAuth storage or terminal UI'); + } + return nextResolve(specifier, context); +} });`, + ); + const result = await runOperation( + root, + { type: 'auth.token' }, + tokenEnv(undefined, name), + preload, + ); + expect(result.status, result.stderr).toBe(0); + expect(JSON.parse(result.stdout).data).toEqual({ type: 'api_token', token: 'synthetic-token' }); + }, +); + +it('loads the live terminal presentation when the login operation needs it', async () => { + const root = temporaryDirectory('void-login-presentation-'); + const preload = join(root, 'login-child.mjs'); + writeFileSync( + preload, + `import childProcess from 'node:child_process'; +import { EventEmitter } from 'node:events'; +import { syncBuiltinESMExports } from 'node:module'; +import { PassThrough } from 'node:stream'; +childProcess.spawn = (_command, args) => { + if (!args.includes('login') || !args.includes('--force')) throw new Error('Expected forced login'); + const child = new EventEmitter(); + child.stdout = new PassThrough(); + child.stderr = new PassThrough(); + setImmediate(() => { + child.stderr.write('Visit this link to authenticate: https://example.test/device\\nand enter the code: TEST-1234\\n'); + child.stdout.end(); child.stderr.end(); child.emit('close', 0, null); + }); + return child; +}; +syncBuiltinESMExports();`, + ); + const result = await runOperation( + root, + { type: 'auth.login' }, + { ...systemEnv, CLOUDFLARE_AUTH_USE_KEYRING: 'false' }, + preload, + ); + expect(result.status, result.stderr).toBe(0); + expect(result.stdout).toContain('TEST-1234'); + expect(result.stdout).toContain('https://example.test/device'); +}); + const voidVersion = JSON.parse(readFileSync(new URL('../../package.json', import.meta.url), 'utf8')) .version as string; const cfVersion = JSON.parse(readFileSync(new URL(import.meta.resolve('cf/package.json')), 'utf8')) diff --git a/packages/void/test/unit/test-iteration.test.ts b/packages/void/test/unit/test-iteration.test.ts new file mode 100644 index 000000000..081f860f9 --- /dev/null +++ b/packages/void/test/unit/test-iteration.test.ts @@ -0,0 +1,27 @@ +import { join } from 'node:path'; +import { loadConfigFromFile } from 'vite-plus'; +import { expect, it } from 'vite-plus/test'; +import { parseCLI } from 'vite-plus/test/node'; + +it('keeps a forwarded filename as a filter for the native iteration command', async () => { + const loaded = await loadConfigFromFile( + { command: 'serve', mode: 'test' }, + join(import.meta.dirname, '../../../../vite.config.ts'), + ); + const task = loaded?.config.run?.tasks?.['test:iterate']; + if ( + !task || + typeof task !== 'object' || + !('command' in task) || + typeof task.command !== 'string' + ) { + throw new Error('Expected a native Vitest command'); + } + const file = 'packages/void/test/unit/plugin-routing.test.ts'; + const { filter, options } = parseCLI( + `${task.command.replace(/^vp test/, 'vitest')} ${file} --project unit`, + ); + expect(filter).toEqual([file]); + expect(options.sinceLastRun).toBeTruthy(); + expect(options.project).toEqual(['unit']); +}); diff --git a/test/performance-reporter.ts b/test/performance-reporter.ts new file mode 100644 index 000000000..8218061d4 --- /dev/null +++ b/test/performance-reporter.ts @@ -0,0 +1,73 @@ +import { mkdirSync, writeFileSync } from 'node:fs'; +import { availableParallelism, cpus, platform, release, totalmem } from 'node:os'; +import { dirname, relative, resolve } from 'node:path'; +import type { Reporter, TestModule, Vitest } from 'vite-plus/test/node'; + +/** Opt-in measurements; this reporter never participates in test selection or result reuse. */ +export default class PerformanceReporter implements Reporter { + private root = process.cwd(); + private output = ''; + private started = 0; + + onInit(vitest: Vitest): void { + this.root = vitest.config.root; + const configured = vitest.config.outputFile; + this.output = resolve( + this.root, + (typeof configured === 'string' ? configured : configured?.performance) ?? + 'node_modules/.cache/void-tests/performance.json', + ); + this.started = performance.now(); + } + + onTestRunEnd( + modules: ReadonlyArray, + errors: ReadonlyArray, + reason: string, + ): void { + const files = modules.map((module) => { + const diagnostic = module.diagnostic(); + return { + project: module.project.name, + file: relative(this.root, module.moduleId).replaceAll('\\', '/'), + durationMs: diagnostic.duration, + collectMs: diagnostic.collectDuration, + setupMs: diagnostic.setupDuration, + prepareMs: diagnostic.prepareDuration, + environmentMs: diagnostic.environmentSetupDuration, + worker: diagnostic.concurrencyId, + tests: [...module.children.allTests()].map((test) => ({ + name: test.fullName, + state: test.result().state, + durationMs: test.diagnostic()?.duration ?? 0, + })), + }; + }); + mkdirSync(dirname(this.output), { recursive: true }); + writeFileSync( + this.output, + `${JSON.stringify( + { + version: 1, + recordedAt: new Date().toISOString(), + durationMs: performance.now() - this.started, + reason, + unhandledErrors: errors.length, + machine: { + node: process.version, + platform: platform(), + release: release(), + arch: process.arch, + cpu: cpus()[0]?.model, + parallelism: availableParallelism(), + memoryBytes: totalmem(), + }, + files, + }, + null, + 2, + )}\n`, + ); + console.log(`Test performance report: ${this.output}`); + } +} diff --git a/vite.config.ts b/vite.config.ts index e960c9262..3cf45b4a3 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -75,6 +75,11 @@ const platformAlias = { export default defineConfig({ run: { tasks: { + 'test:iterate': { command: 'vp test run --since-last-run=true', cache: false }, + 'test:benchmark': { + command: 'vp test run --reporter=default --reporter=./test/performance-reporter.ts', + cache: false, + }, 'build:all': { command: "vp run build && vp run --filter './platform/packages/**' build && vp run --filter './playground/**' --parallel build", @@ -487,9 +492,9 @@ export default defineConfig({ fsModuleCache: true, include: sdkPgliteUnitTests, sequence: { groupOrder: 2 }, - // Native Node 24 worker crashes can abort the run on Linux and macOS. - // Keep PGlite's Wasm heap in a separate process for each database. - pool: ['linux', 'darwin'].includes(process.platform) ? 'forks' : 'threads', + // PGlite's Wasm JIT teardown can crash Node 24 worker threads, including Windows. + // Keep each database in a separate process on every platform. + pool: 'forks', maxWorkers, ...(process.platform === 'win32' ? { testTimeout: 20_000 } : {}), },