From a3ab134d4e9247b4b5b4827d119d9d030f970e6e Mon Sep 17 00:00:00 2001 From: "Li, Amazing Ang" Date: Mon, 12 Oct 2026 15:04:35 +0800 Subject: [PATCH] test(cli): handle escaped paths and native timer rounding --- src/tests/client-fetch-delegate-native.ts | 9 ++++++--- src/tests/workers-build-process.test.ts | 9 +++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/tests/client-fetch-delegate-native.ts b/src/tests/client-fetch-delegate-native.ts index 79be099..a71a32a 100644 --- a/src/tests/client-fetch-delegate-native.ts +++ b/src/tests/client-fetch-delegate-native.ts @@ -89,12 +89,15 @@ try { pull(controller) { emptyPulls++; controller.enqueue(new Uint8Array()); }, cancel() { emptyCancelled = true; }, }, { highWaterMark: 0 }); - const emptyStart = Date.now(); + const emptyStart = performance.now(); await assert.rejects(uploadRequest(base + '/empty-upload', { method: 'POST', body: emptyBody }), RequestTimeoutError); assert.ok(emptyPulls > 64); assert.equal(emptyCancelled, true); - assert.ok(Date.now() - emptyStart >= budget); - assert.ok(Date.now() - emptyStart < budget * 5); + const emptyElapsed = performance.now() - emptyStart; + // Native timers can wake slightly early; allow 5ms of scheduler rounding, + // while still rejecting premature aborts and empty chunks resetting the timer. + assert.ok(emptyElapsed >= budget - 5, `empty upload timed out too early: ${emptyElapsed}ms`); + assert.ok(emptyElapsed < budget * 5, `empty upload did not time out promptly: ${emptyElapsed}ms`); assert.deepEqual(await uploadRequest(base + '/slow-response', { method: 'POST', body: 'once' }), { ok: true }); await assert.rejects(uploadRequest(base + '/stalled-response', { method: 'POST', body: 'once' }), RequestTimeoutError); diff --git a/src/tests/workers-build-process.test.ts b/src/tests/workers-build-process.test.ts index 44b1bea..3bb9ca5 100644 --- a/src/tests/workers-build-process.test.ts +++ b/src/tests/workers-build-process.test.ts @@ -3,6 +3,7 @@ import { spawn, type ChildProcess } from "node:child_process"; import { existsSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; +import { fileURLToPath, pathToFileURL } from "node:url"; import { build } from "esbuild"; const pause = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)); @@ -19,7 +20,11 @@ function running(pid: number) { // rather than a mocked ChildProcess or Bun's differing signal implementation. async function fixture(mode: "normal" | "stubborn" | "pipes", timeoutMs: number) { const root = mkdtempSync(join(tmpdir(), "xapi-build-process-")); - symlinkSync(new URL("../../node_modules", import.meta.url).pathname, join(root, "node_modules"), process.platform === "win32" ? "junction" : "dir"); + symlinkSync(fileURLToPath(new URL("../../node_modules", import.meta.url)), join(root, "node_modules"), process.platform === "win32" ? "junction" : "dir"); + // Always cover URL escaping, even when the checkout path has no spaces. + const source = join(root, "source files #%"); + symlinkSync(fileURLToPath(new URL("../", import.meta.url)), source, process.platform === "win32" ? "junction" : "dir"); + const buildModule = pathToFileURL(join(source, "workers-project-build.ts")); writeFileSync(join(root, "package.json"), JSON.stringify({ type: "module", scripts: { build: "node parent.cjs" } })); writeFileSync(join(root, "parent.cjs"), ` const {spawn} = require('node:child_process'); @@ -38,7 +43,7 @@ async function fixture(mode: "normal" | "stubborn" | "pipes", timeoutMs: number) ${mode === "normal" ? "writeFileSync('worker.mjs', 'export default {}');" : "setTimeout(() => {writeFileSync('late-output', 'must not happen'); console.log('late-stdout'); console.error('late-stderr');}, 4000); setInterval(() => {}, 1000);"} `); writeFileSync(join(root, "runner.ts"), ` - import {runWorkerProjectBuild} from ${JSON.stringify(new URL("../workers-project-build.ts", import.meta.url).pathname)}; + import {runWorkerProjectBuild} from ${JSON.stringify(fileURLToPath(buildModule))}; try { await runWorkerProjectBuild('npm run build', ${JSON.stringify(root)}, ${timeoutMs}); console.log('build-complete'); } catch(error) { console.error(error.message); process.exitCode = 1; } `);