|
1 | 1 | // @effect-diagnostics nodeBuiltinImport:off - exercises the real native runner and local HTTP collector. |
2 | 2 | import * as NodeChildProcess from "node:child_process"; |
| 3 | +import * as NodeDiagnosticsChannel from "node:diagnostics_channel"; |
3 | 4 | import * as NodeFSP from "node:fs/promises"; |
4 | 5 | import * as NodeHttp from "node:http"; |
5 | 6 | import * as NodeOS from "node:os"; |
6 | 7 | import * as NodePath from "node:path"; |
7 | | -import { expect, it } from "vite-plus/test"; |
| 8 | +import { expect, it, vi } from "vite-plus/test"; |
| 9 | +import startRunHook from "./runHook.ts"; |
8 | 10 |
|
9 | 11 | it("uses one machine hook across fresh worktrees without modifying Vite+", async () => { |
10 | 12 | const root = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "task-cache-hook-")); |
11 | 13 | const unrelated = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "unrelated-project-")); |
12 | 14 | const reports: { authorization: string | undefined; body: string }[] = []; |
13 | | - let firstExit!: () => void; |
14 | | - const firstExited = new Promise<void>((resolve) => { |
15 | | - firstExit = resolve; |
16 | | - }); |
17 | | - let completedReports = 0; |
18 | | - let notified: (() => void) | undefined; |
19 | | - let firstDisconnected = false; |
20 | | - const report = (count: number) => |
21 | | - completedReports >= count |
22 | | - ? Promise.resolve() |
23 | | - : new Promise<void>((resolve) => { |
24 | | - notified = resolve; |
25 | | - }); |
26 | 15 | const server = NodeHttp.createServer(async (request, response) => { |
27 | 16 | if (!request.url?.endsWith("/telemetry")) { |
28 | 17 | response.writeHead(404).end(); |
@@ -31,14 +20,7 @@ it("uses one machine hook across fresh worktrees without modifying Vite+", async |
31 | 20 | let body = ""; |
32 | 21 | for await (const chunk of request) body += chunk; |
33 | 22 | reports.push({ authorization: request.headers.authorization, body }); |
34 | | - if (reports.length === 1) { |
35 | | - await firstExited; |
36 | | - firstDisconnected = response.destroyed; |
37 | | - } |
38 | 23 | response.writeHead(204).end(); |
39 | | - completedReports++; |
40 | | - notified?.(); |
41 | | - notified = undefined; |
42 | 24 | }); |
43 | 25 | try { |
44 | 26 | await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve)); |
@@ -110,7 +92,7 @@ it("uses one machine hook across fresh worktrees without modifying Vite+", async |
110 | 92 | }), |
111 | 93 | ); |
112 | 94 | const originalEntrypoint = await NodeFSP.readFile(entrypoint, "utf8"); |
113 | | - const run = (task: string, cwd = root, ci = false, cache = "--cache") => |
| 95 | + const run = (task: string, cwd = root, cache = "--cache") => |
114 | 96 | new Promise<number | null>((resolve, reject) => { |
115 | 97 | const child = NodeChildProcess.spawn( |
116 | 98 | process.execPath, |
@@ -120,7 +102,8 @@ it("uses one machine hook across fresh worktrees without modifying Vite+", async |
120 | 102 | env: { |
121 | 103 | ...process.env, |
122 | 104 | HOME: root, |
123 | | - GITHUB_ACTIONS: ci ? "true" : undefined, |
| 105 | + // Flush inline so no detached flusher can write under root while it is removed. |
| 106 | + GITHUB_ACTIONS: "true", |
124 | 107 | T3_TASK_CACHE_TELEMETRY: undefined, |
125 | 108 | T3_TASK_CACHE_HOST: undefined, |
126 | 109 | VITE_CACHE_PATH: undefined, |
@@ -139,32 +122,30 @@ it("uses one machine hook across fresh worktrees without modifying Vite+", async |
139 | 122 | }); |
140 | 123 | child.once("error", reject); |
141 | 124 | child.once("close", (code) => { |
142 | | - firstExit(); |
143 | 125 | if (code !== 0 && task !== "fail") reject(new Error(stderr)); |
144 | 126 | else resolve(code); |
145 | 127 | }); |
146 | 128 | }); |
147 | 129 | expect(await run("build")).toBe(0); |
148 | | - await report(1); |
| 130 | + expect(reports).toHaveLength(1); |
149 | 131 | expect(await run("--last-details")).toBe(0); |
150 | 132 | expect( |
151 | 133 | JSON.parse(await NodeFSP.readFile(NodePath.join(root, "environment.json"), "utf8")), |
152 | 134 | ).toMatchObject({ url: null }); |
153 | | - expect(firstDisconnected).toBe(false); |
154 | 135 | expect(await run("build")).toBe(0); |
155 | | - await report(2); |
| 136 | + expect(reports).toHaveLength(2); |
156 | 137 | expect(await run("fail")).toBe(7); |
157 | | - await report(3); |
| 138 | + expect(reports).toHaveLength(3); |
158 | 139 | const secondWorktree = NodePath.join(root, "second-worktree"); |
159 | 140 | await NodeFSP.mkdir(secondWorktree); |
160 | 141 | for (const file of ["package.json", ".task-cache.json", "vite.config.ts", "build.cjs"]) |
161 | 142 | await NodeFSP.copyFile(NodePath.join(root, file), NodePath.join(secondWorktree, file)); |
162 | 143 | expect(await run("build", secondWorktree)).toBe(0); |
163 | | - await report(4); |
164 | | - expect(await run("build", secondWorktree, true)).toBe(0); |
165 | | - await report(5); |
| 144 | + expect(reports).toHaveLength(4); |
| 145 | + expect(await run("build", secondWorktree)).toBe(0); |
| 146 | + expect(reports).toHaveLength(5); |
166 | 147 | expect(await NodeFSP.readFile(entrypoint, "utf8")).toBe(originalEntrypoint); |
167 | | - expect(await run("nested", root, false, "--no-cache")).toBe(0); |
| 148 | + expect(await run("nested", root, "--no-cache")).toBe(0); |
168 | 149 | expect(await run("build", nested)).toBe(0); |
169 | 150 | expect(reports).toHaveLength(5); |
170 | 151 | expect( |
@@ -187,6 +168,73 @@ it("uses one machine hook across fresh worktrees without modifying Vite+", async |
187 | 168 | } |
188 | 169 | }); |
189 | 170 |
|
| 171 | +it("sends telemetry from a detached flusher that the task runner does not wait for", async () => { |
| 172 | + const root = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "task-cache-flusher-")); |
| 173 | + const reports: string[] = []; |
| 174 | + let finishReturned!: () => void; |
| 175 | + const returned = new Promise<void>((resolve) => { |
| 176 | + finishReturned = resolve; |
| 177 | + }); |
| 178 | + let disconnected = false; |
| 179 | + const server = NodeHttp.createServer(async (request, response) => { |
| 180 | + let body = ""; |
| 181 | + for await (const chunk of request) body += chunk; |
| 182 | + reports.push(body); |
| 183 | + await returned; |
| 184 | + disconnected = response.destroyed; |
| 185 | + response.writeHead(204).end(); |
| 186 | + }); |
| 187 | + // The hook detaches and unrefs its flusher, so only this channel lets cleanup wait for it. |
| 188 | + const flushers: Promise<void>[] = []; |
| 189 | + const onSpawn = (message: unknown) => { |
| 190 | + const { process: flusher } = message as { process: NodeChildProcess.ChildProcess }; |
| 191 | + flushers.push(new Promise((resolve) => flusher.once("close", () => resolve()))); |
| 192 | + }; |
| 193 | + NodeDiagnosticsChannel.subscribe("child_process", onSpawn); |
| 194 | + try { |
| 195 | + await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve)); |
| 196 | + const address = server.address(); |
| 197 | + if (!address || typeof address === "string") throw new Error("Missing HTTP address"); |
| 198 | + const endpoint = `http://127.0.0.1:${address.port}/projects/test`; |
| 199 | + await NodeFSP.writeFile(NodePath.join(root, ".task-cache.json"), JSON.stringify({ endpoint })); |
| 200 | + await NodeFSP.mkdir(NodePath.join(root, ".config/t3-task-cache"), { recursive: true }); |
| 201 | + await NodeFSP.writeFile( |
| 202 | + NodePath.join(root, ".config/t3-task-cache/credentials.json"), |
| 203 | + JSON.stringify({ [endpoint]: { name: "maria", token: "secret" } }), |
| 204 | + ); |
| 205 | + vi.stubEnv("HOME", root); |
| 206 | + vi.stubEnv("GITHUB_ACTIONS", undefined); |
| 207 | + vi.stubEnv("T3_TASK_CACHE_TELEMETRY", undefined); |
| 208 | + vi.stubEnv("VITE_CACHE_PATH", undefined); |
| 209 | + const finish = startRunHook({ root }); |
| 210 | + const summary = NodePath.join(root, "node_modules/.vite/task-cache/run/last-summary.json"); |
| 211 | + await NodeFSP.mkdir(NodePath.dirname(summary), { recursive: true }); |
| 212 | + await NodeFSP.writeFile( |
| 213 | + summary, |
| 214 | + JSON.stringify({ |
| 215 | + tasks: [{ package_name: "fixture", task_name: "build", result: { Spawned: {} } }], |
| 216 | + }), |
| 217 | + ); |
| 218 | + await finish?.(); |
| 219 | + finishReturned(); |
| 220 | + await Promise.all(flushers); |
| 221 | + expect(flushers).toHaveLength(1); |
| 222 | + expect(reports.map((report) => JSON.parse(report).tasks)).toEqual([ |
| 223 | + [{ package: "fixture", task: "build", source: "executed", savedMs: 0 }], |
| 224 | + ]); |
| 225 | + expect(disconnected).toBe(false); |
| 226 | + } finally { |
| 227 | + finishReturned(); |
| 228 | + await Promise.all(flushers); |
| 229 | + NodeDiagnosticsChannel.unsubscribe("child_process", onSpawn); |
| 230 | + vi.unstubAllEnvs(); |
| 231 | + await new Promise<void>((resolve, reject) => |
| 232 | + server.close((error) => (error ? reject(error) : resolve())), |
| 233 | + ); |
| 234 | + await NodeFSP.rm(root, { recursive: true, force: true }); |
| 235 | + } |
| 236 | +}); |
| 237 | + |
190 | 238 | it("rejects invalid setup credentials before installing anything and never prints the token", async () => { |
191 | 239 | const home = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "task-cache-setup-")); |
192 | 240 | const token = "private-credential-that-must-not-appear-in-output"; |
|