Skip to content

Commit a4578c8

Browse files
test(web): use namespace node imports in evidence scripts
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f2ded46 commit a4578c8

7 files changed

Lines changed: 94 additions & 87 deletions

File tree

‎.evidence/pr-22193/lib.mjs‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// Shared browser helpers for the PR #22193 evidence drivers.
2-
import { createRequire } from "node:module";
3-
import { execFileSync } from "node:child_process";
4-
import { existsSync, readFileSync } from "node:fs";
5-
import { dirname, join } from "node:path";
6-
import { fileURLToPath } from "node:url";
2+
import * as NodeModule from "node:module";
3+
import * as NodeChildProcess from "node:child_process";
4+
import * as NodeFS from "node:fs";
5+
import * as NodePath from "node:path";
6+
import * as NodeURL from "node:url";
77

8-
const here = dirname(fileURLToPath(import.meta.url));
8+
const here = NodePath.dirname(NodeURL.fileURLToPath(import.meta.url));
99
// Playwright comes from apps/web's dev dependencies in this checkout.
10-
const require = createRequire(join(here, "../../apps/web/package.json"));
10+
const require = NodeModule.createRequire(NodePath.join(here, "../../apps/web/package.json"));
1111
export const { chromium } = require("playwright");
1212

1313
// Web origin from the newest [dev-runner] line of a dev server log.
@@ -20,13 +20,13 @@ export function webOriginFromLog(logFile) {
2020
// Mints a short-lived pairing URL for the dev server that owns <worktree>/.t3.
2121
// The token is never logged; the resulting session is cached in authFile.
2222
export function mintPairingUrl(worktree) {
23-
const out = execFileSync(
23+
const out = NodeChildProcess.execFileSync(
2424
"node",
2525
[
26-
join(worktree, "apps/server/src/bin.ts"),
26+
NodePath.join(worktree, "apps/server/src/bin.ts"),
2727
"pair",
2828
"--base-dir",
29-
join(worktree, ".t3"),
29+
NodePath.join(worktree, ".t3"),
3030
"--ttl",
3131
"10m",
3232
],
@@ -54,7 +54,7 @@ export async function openApp({
5454
...(videoDir ? { recordVideo: { dir: videoDir, size: { width, height } } } : {}),
5555
};
5656
let pairing = null;
57-
if (!existsSync(authFile)) {
57+
if (!NodeFS.existsSync(authFile)) {
5858
pairing = mintPairingUrl(worktree);
5959
const context = await browser.newContext({ viewport: { width, height } });
6060
const page = await context.newPage();

‎.evidence/pr-22193/new-thread.mjs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { setTimeout as sleep } from "node:timers/promises";
1+
import * as NodeTimersPromises from "node:timers/promises";
22

33
// Opens a fresh draft on the new-thread screen, picking the first project when asked.
44
export async function openNewThread(page, origin) {
@@ -7,19 +7,19 @@ export async function openNewThread(page, origin) {
77
.getByRole("button", { name: "New thread", exact: true })
88
.first()
99
.waitFor({ timeout: 90_000 });
10-
await sleep(1500);
10+
await NodeTimersPromises.setTimeout(1500);
1111
await page.getByRole("button", { name: "New thread", exact: true }).first().click();
1212
// New thread asks for a project; take the first one.
1313
const picker = page.locator('[data-slot="command-dialog-backdrop"]');
1414
await picker.waitFor({ timeout: 5000 }).catch(() => {});
1515
if (await picker.count()) {
16-
await sleep(800);
16+
await NodeTimersPromises.setTimeout(800);
1717
await page.keyboard.press("Enter");
1818
await picker.waitFor({ state: "detached", timeout: 10_000 }).catch(() => {});
1919
}
2020
await page
2121
.locator('[data-composer-shortcut="composer.effort"], [data-chat-provider-model-picker="true"]')
2222
.first()
2323
.waitFor({ timeout: 30_000 });
24-
await sleep(2500);
24+
await NodeTimersPromises.setTimeout(2500);
2525
}

‎.evidence/pr-22193/run-row.mjs‎

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
// Stops the worktree's dev server (only the group this script family started), seeds
55
// settings.json, starts `vp run dev`, records the scripted steps, and logs the persisted
66
// defaultModelSelection before and after. The server is left running for stills; stop it with stop.mjs.
7-
import { execFileSync } from "node:child_process";
8-
import { appendFileSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
9-
import { join } from "node:path";
10-
import { setTimeout as sleep } from "node:timers/promises";
7+
import * as NodeChildProcess from "node:child_process";
8+
import * as NodeFS from "node:fs";
9+
import * as NodePath from "node:path";
10+
import * as NodeTimersPromises from "node:timers/promises";
1111
import { caption, gotoSettingsGeneral, modelRow, openApp, webOriginFromLog } from "./lib.mjs";
1212
import { startServer, stopServer } from "./server.mjs";
1313
import { openNewThread } from "./new-thread.mjs";
@@ -30,42 +30,46 @@ if (!spec || !label || !worktree) {
3030
process.exit(2);
3131
}
3232

33-
mkdirSync(outDir, { recursive: true });
34-
const settingsFile = join(worktree, ".t3/userdata/settings.json");
35-
const pidFile = join(outDir, `${label}.pgid`);
36-
const logFile = join(outDir, `${label}-${row}-dev.log`);
37-
const persistedLog = join(outDir, "persisted.log");
33+
NodeFS.mkdirSync(outDir, { recursive: true });
34+
const settingsFile = NodePath.join(worktree, ".t3/userdata/settings.json");
35+
const pidFile = NodePath.join(outDir, `${label}.pgid`);
36+
const logFile = NodePath.join(outDir, `${label}-${row}-dev.log`);
37+
const persistedLog = NodePath.join(outDir, "persisted.log");
3838
const base = `${row}-${label}`;
3939

4040
function persisted() {
4141
try {
42-
return execFileSync("jq", ["-c", ".defaultModelSelection", settingsFile], {
42+
return NodeChildProcess.execFileSync("jq", ["-c", ".defaultModelSelection", settingsFile], {
4343
encoding: "utf8",
4444
}).trim();
4545
} catch {
4646
return JSON.stringify(
47-
JSON.parse(readFileSync(settingsFile, "utf8")).defaultModelSelection ?? null,
47+
JSON.parse(NodeFS.readFileSync(settingsFile, "utf8")).defaultModelSelection ?? null,
4848
);
4949
}
5050
}
5151
function logPersisted(phase) {
5252
const line = `${new Date().toISOString()} ${label} ${row} ${phase} ${persisted()}`;
53-
appendFileSync(persistedLog, `${line}\n`);
53+
NodeFS.appendFileSync(persistedLog, `${line}\n`);
5454
console.log(line);
5555
}
5656

5757
await stopServer(pidFile);
58-
execFileSync("node", [join(import.meta.dirname, "seed.mjs"), worktree, spec.seed], {
59-
stdio: "inherit",
60-
});
58+
NodeChildProcess.execFileSync(
59+
"node",
60+
[NodePath.join(import.meta.dirname, "seed.mjs"), worktree, spec.seed],
61+
{
62+
stdio: "inherit",
63+
},
64+
);
6165
logPersisted(`seeded:${spec.seed}`);
6266
await startServer({ worktree, pidFile, logFile });
6367
logPersisted("server-started");
6468

65-
const videoDir = join(outDir, `video-${base}`);
69+
const videoDir = NodePath.join(outDir, `video-${base}`);
6670
const app = await openApp({
6771
worktree,
68-
authFile: join(outDir, `auth-${label}.json`),
72+
authFile: NodePath.join(outDir, `auth-${label}.json`),
6973
webOrigin: webOriginFromLog(logFile),
7074
videoDir,
7175
});
@@ -77,14 +81,14 @@ const say = async (text, hold = 1800) => {
7781
`${String(Math.floor(s / 60)).padStart(2, "0")}:${String(s % 60).padStart(2, "0")} ${text}`,
7882
);
7983
await caption(app.page, `[${label}] ${row}: ${text}`).catch(() => {});
80-
await sleep(hold);
84+
await NodeTimersPromises.setTimeout(hold);
8185
};
8286
const ctx = {
8387
...app,
8488
say,
8589
persisted,
8690
logPersisted,
87-
shot: (n) => app.page.screenshot({ path: join(outDir, `${base}-${n}.png`) }),
91+
shot: (n) => app.page.screenshot({ path: NodePath.join(outDir, `${base}-${n}.png`) }),
8892
};
8993

9094
try {
@@ -99,7 +103,7 @@ try {
99103
await app.context.close();
100104
await app.browser.close();
101105
const webm = await video.path();
102-
execFileSync("ffmpeg", [
106+
NodeChildProcess.execFileSync("ffmpeg", [
103107
"-y",
104108
"-loglevel",
105109
"error",
@@ -111,14 +115,12 @@ try {
111115
"yuv420p",
112116
"-movflags",
113117
"+faststart",
114-
join(outDir, `${base}.mp4`),
118+
NodePath.join(outDir, `${base}.mp4`),
115119
]);
116-
writeFileSync(join(outDir, `${base}.steps.txt`), `${steps.join("\n")}\n`);
120+
NodeFS.writeFileSync(NodePath.join(outDir, `${base}.steps.txt`), `${steps.join("\n")}\n`);
117121
console.log(steps.join("\n"));
118122
}
119123

120-
// ---- scenarios ----
121-
122124
function traitsTrigger(page) {
123125
// Every row button that is not the model picker, the source badge, or the reset button.
124126
return modelRow(page).locator(
@@ -149,7 +151,7 @@ async function reloadAndShow({ page, say, persisted }) {
149151
{ timeout: 30_000 },
150152
)
151153
.catch(() => {});
152-
await sleep(2000);
154+
await NodeTimersPromises.setTimeout(2000);
153155
await say(
154156
`After reload. Row: "${(await modelRow(page).innerText()).split("\n").filter(Boolean).slice(2).join(" · ")}"`,
155157
2500,
@@ -171,11 +173,11 @@ function settingsEffort(target) {
171173
600,
172174
);
173175
await trigger.first().click();
174-
await sleep(1200);
176+
await NodeTimersPromises.setTimeout(1200);
175177
await shot("menu");
176178
await say(`Choose ${target}`, 600);
177179
await page.getByRole("menuitemradio", { name: target, exact: true }).click();
178-
await sleep(1500);
180+
await NodeTimersPromises.setTimeout(1500);
179181
await say(`Picked ${target}. settings.json now ${ctx.persisted()}`, 2500);
180182
}
181183
await reloadAndShow(ctx);
@@ -193,11 +195,11 @@ async function composerEffort(ctx) {
193195
600,
194196
);
195197
await effort.click();
196-
await sleep(1200);
198+
await NodeTimersPromises.setTimeout(1200);
197199
await shot("composer-menu");
198200
await say("Choose Low", 600);
199201
await page.getByRole("menuitemradio", { name: "Low", exact: true }).click();
200-
await sleep(1500);
202+
await NodeTimersPromises.setTimeout(1500);
201203
await say(
202204
`Composer effort now "${(await effort.innerText()).trim()}". settings.json = ${ctx.persisted()}`,
203205
2500,
@@ -208,7 +210,7 @@ async function composerEffort(ctx) {
208210
.locator('[data-composer-shortcut="composer.effort"]')
209211
.first()
210212
.waitFor({ timeout: 90_000 });
211-
await sleep(2500);
213+
await NodeTimersPromises.setTimeout(2500);
212214
await say(
213215
`After reload composer effort "${(await page.locator('[data-composer-shortcut="composer.effort"]').first().innerText()).trim()}"`,
214216
2000,
@@ -232,39 +234,39 @@ async function unsupportedEffort(ctx) {
232234
600,
233235
);
234236
await trigger.click();
235-
await sleep(1200);
237+
await NodeTimersPromises.setTimeout(1200);
236238
await shot("menu");
237239
await say("Close it with Escape (no choice made)", 600);
238240
await page.keyboard.press("Escape");
239-
await sleep(1000);
241+
await NodeTimersPromises.setTimeout(1000);
240242
await say(`settings.json = ${ctx.persisted()}`, 2500);
241243
await reloadAndShow(ctx);
242244
await say("Now choose Max", 600);
243245
await traitsTrigger(page).first().click();
244-
await sleep(1000);
246+
await NodeTimersPromises.setTimeout(1000);
245247
await page.getByRole("menuitemradio", { name: "Max", exact: true }).click();
246-
await sleep(1500);
248+
await NodeTimersPromises.setTimeout(1500);
247249
await reloadAndShow(ctx);
248250
await shot("after-reload");
249251
}
250252

251253
async function pickModel(page, say, query, optionName) {
252254
await modelRow(page).locator('[data-chat-provider-model-picker="true"]').click();
253-
await sleep(1000);
255+
await NodeTimersPromises.setTimeout(1000);
254256
const claudeTab = page
255257
.locator("[data-model-picker-content]")
256258
.getByRole("toolbar")
257259
.getByRole("button", { name: /claude/i });
258260
if (await claudeTab.count()) await claudeTab.first().click();
259261
await page.locator("[data-model-picker-content]").getByRole("combobox").fill(query);
260-
await sleep(1000);
262+
await NodeTimersPromises.setTimeout(1000);
261263
await say(`Model picker: search "${query}"`, 1200);
262264
await page
263265
.locator("[data-model-picker-content]")
264266
.getByRole("option", { name: optionName })
265267
.first()
266268
.click();
267-
await sleep(1500);
269+
await NodeTimersPromises.setTimeout(1500);
268270
}
269271

270272
async function noEffortModel(ctx) {

‎.evidence/pr-22193/seed.mjs‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Writes a matrix seed into <worktree>/.t3/userdata/settings.json.
33
// Only run while that worktree's dev server is stopped.
44
// Usage: node seed.mjs <worktree> <seed>
5-
import { mkdirSync, readFileSync, writeFileSync, existsSync } from "node:fs";
6-
import { join } from "node:path";
5+
import * as NodeFS from "node:fs";
6+
import * as NodePath from "node:path";
77

88
const [worktree, seedName] = process.argv.slice(2);
99
if (!worktree || !seedName) {
@@ -55,12 +55,12 @@ if (!seed) {
5555
process.exit(2);
5656
}
5757

58-
const dir = join(worktree, ".t3", "userdata");
59-
const file = join(dir, "settings.json");
60-
mkdirSync(dir, { recursive: true });
61-
const current = existsSync(file) ? JSON.parse(readFileSync(file, "utf8")) : {};
58+
const dir = NodePath.join(worktree, ".t3", "userdata");
59+
const file = NodePath.join(dir, "settings.json");
60+
NodeFS.mkdirSync(dir, { recursive: true });
61+
const current = NodeFS.existsSync(file) ? JSON.parse(NodeFS.readFileSync(file, "utf8")) : {};
6262
for (const key of ["defaultModelSelection", "providerInstances", "providerModelPreferences"]) {
6363
delete current[key];
6464
}
65-
writeFileSync(file, `${JSON.stringify({ ...current, ...seed }, null, 2)}\n`);
65+
NodeFS.writeFileSync(file, `${JSON.stringify({ ...current, ...seed }, null, 2)}\n`);
6666
console.log(`${seedName} -> ${file}`);

‎.evidence/pr-22193/server.mjs‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// Starts and stops one worktree's `vp run dev`, tracking only the process group it spawned.
2-
import { spawn } from "node:child_process";
3-
import { existsSync, openSync, readFileSync, rmSync, writeFileSync } from "node:fs";
4-
import { setTimeout as sleep } from "node:timers/promises";
2+
import * as NodeChildProcess from "node:child_process";
3+
import * as NodeFS from "node:fs";
4+
import * as NodeTimersPromises from "node:timers/promises";
55

66
export async function stopServer(pidFile) {
7-
if (!existsSync(pidFile)) return;
8-
const pgid = Number(readFileSync(pidFile, "utf8"));
7+
if (!NodeFS.existsSync(pidFile)) return;
8+
const pgid = Number(NodeFS.readFileSync(pidFile, "utf8"));
99
try {
1010
process.kill(-pgid, "SIGTERM");
1111
} catch {}
@@ -15,34 +15,34 @@ export async function stopServer(pidFile) {
1515
} catch {
1616
break;
1717
}
18-
await sleep(250);
18+
await NodeTimersPromises.setTimeout(250);
1919
}
2020
try {
2121
process.kill(-pgid, "SIGKILL");
2222
} catch {}
23-
rmSync(pidFile);
23+
NodeFS.rmSync(pidFile);
2424
}
2525

2626
export async function startServer({ worktree, pidFile, logFile }) {
27-
const out = openSync(logFile, "w");
28-
const child = spawn("vp", ["run", "dev"], {
27+
const out = NodeFS.openSync(logFile, "w");
28+
const child = NodeChildProcess.spawn("vp", ["run", "dev"], {
2929
cwd: worktree,
3030
detached: true,
3131
stdio: ["ignore", out, out],
3232
});
33-
writeFileSync(pidFile, String(child.pid));
33+
NodeFS.writeFileSync(pidFile, String(child.pid));
3434
child.unref();
3535
for (let i = 0; i < 240; i++) {
36-
const log = readFileSync(logFile, "utf8");
36+
const log = NodeFS.readFileSync(logFile, "utf8");
3737
if (
3838
/Listening on http/.test(log) &&
3939
/webPort=\d+/.test(log) &&
4040
/VITE|ready in|Local:/i.test(log)
4141
)
4242
break;
4343
if (/Listening on http/.test(log) && i > 20) break;
44-
await sleep(500);
44+
await NodeTimersPromises.setTimeout(500);
4545
}
4646
// Vite and provider snapshots settle a little after the server listens.
47-
await sleep(4000);
47+
await NodeTimersPromises.setTimeout(4000);
4848
}

0 commit comments

Comments
 (0)