The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import { afterEach, describe, expect, it } from "bun:test"; |
| 2 | import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from "node:fs"; |
| 3 | import os from "node:os"; |
| 4 | import path from "node:path"; |
| 5 | import { trackBunImports } from "../src/watch/bun-import-tracker.ts"; |
| 6 | |
| 7 | const temporaryDirectories: string[] = []; |
| 8 | |
| 9 | afterEach(() => { |
| 10 | for (const directory of temporaryDirectories.splice(0)) { |
| 11 | rmSync(directory, { force: true, recursive: true }); |
| 12 | } |
| 13 | }); |
| 14 | |
| 15 | describe("trackBunImports", () => { |
| 16 | it("records project-local modules and reports changes to them", async () => { |
| 17 | const temporaryDirectory = realpathSync( |
| 18 | mkdtempSync(path.join(os.tmpdir(), "alchemy-import-bun-")), |
| 19 | ); |
| 20 | temporaryDirectories.push(temporaryDirectory); |
| 21 | const directory = path.join(temporaryDirectory, "project"); |
| 22 | mkdirSync(directory); |
| 23 | const entry = path.join(directory, "entry.ts"); |
| 24 | const dependency = path.join(directory, "dependency.ts"); |
| 25 | const external = path.join(temporaryDirectory, "external.ts"); |
| 26 | writeFileSync( |
| 27 | entry, |
| 28 | [ |
| 29 | 'import { text } from "./dependency.ts";', |
| 30 | 'import { externalText } from "../external.ts";', |
| 31 | "interface Result { text: string; externalText: string }", |
| 32 | "export const result: Result = { text, externalText };", |
| 33 | ].join("\n"), |
| 34 | ); |
| 35 | writeFileSync(dependency, 'export const text: string = "original";\n'); |
| 36 | writeFileSync(external, 'export const externalText: string = "external";\n'); |
| 37 | |
| 38 | // The project root is wider than the entrypoint directory, as it is in a |
| 39 | // stack with `infra/alchemy.run.ts` importing from sibling `src/` trees. |
| 40 | const tracker = trackBunImports({ |
| 41 | root: temporaryDirectory, |
| 42 | debounceMs: 10, |
| 43 | }); |
| 44 | try { |
| 45 | const module = (await import(entry)) as { |
| 46 | result: { text: string; externalText: string }; |
| 47 | }; |
| 48 | expect(module.result).toEqual({ |
| 49 | text: "original", |
| 50 | externalText: "external", |
| 51 | }); |
| 52 | expect(tracker.dependencies).toEqual(new Set([entry, dependency, external])); |
| 53 | |
| 54 | const unchanged: ReadonlySet<string>[] = []; |
| 55 | const unsubscribeUnchanged = tracker.subscribe(({ paths }) => unchanged.push(paths)); |
| 56 | // Give chokidar a moment to arm the file watchers before writing. |
| 57 | await new Promise((resolve) => setTimeout(resolve, 200)); |
| 58 | writeFileSync(dependency, 'export const text: string = "original";\n'); |
| 59 | await new Promise((resolve) => setTimeout(resolve, 100)); |
| 60 | unsubscribeUnchanged(); |
| 61 | expect(unchanged).toEqual([]); |
| 62 | |
| 63 | const changed = new Promise<ReadonlySet<string>>((resolve) => |
| 64 | tracker.subscribe(({ paths }) => resolve(paths)), |
| 65 | ); |
| 66 | writeFileSync(dependency, 'export const text: string = "changed";\n'); |
| 67 | const paths = await Promise.race([ |
| 68 | changed, |
| 69 | new Promise<never>((_, reject) => |
| 70 | setTimeout(() => reject(new Error("watch timed out")), 3000), |
| 71 | ), |
| 72 | ]); |
| 73 | expect(paths).toEqual(new Set([dependency])); |
| 74 | } finally { |
| 75 | await tracker.close(); |
| 76 | } |
| 77 | }); |
| 78 | |
| 79 | it("leaves dependencies in the root's own node_modules alone", async () => { |
| 80 | const temporaryDirectory = realpathSync( |
| 81 | mkdtempSync(path.join(os.tmpdir(), "alchemy-import-bun-nm-")), |
| 82 | ); |
| 83 | temporaryDirectories.push(temporaryDirectory); |
| 84 | const packageDirectory = path.join(temporaryDirectory, "node_modules", "cjs-dep"); |
| 85 | mkdirSync(packageDirectory, { recursive: true }); |
| 86 | const dependency = path.join(packageDirectory, "index.js"); |
| 87 | writeFileSync( |
| 88 | path.join(packageDirectory, "package.json"), |
| 89 | '{"name":"cjs-dep","version":"1.0.0","main":"index.js"}\n', |
| 90 | ); |
| 91 | // CommonJS: Bun cannot hand this back through `onLoad` without losing its |
| 92 | // exports (oven-sh/bun#19279), so intercepting it breaks the import. |
| 93 | writeFileSync(dependency, 'function hi() { return "hi"; }\nmodule.exports = hi;\n'); |
| 94 | const entry = path.join(temporaryDirectory, "entry.ts"); |
| 95 | writeFileSync( |
| 96 | entry, |
| 97 | ['import hi from "cjs-dep";', "export const greeting: string = hi();"].join("\n"), |
| 98 | ); |
| 99 | |
| 100 | const tracker = trackBunImports({ |
| 101 | root: temporaryDirectory, |
| 102 | debounceMs: 10, |
| 103 | }); |
| 104 | try { |
| 105 | const module = (await import(entry)) as { greeting: string }; |
| 106 | expect(module.greeting).toBe("hi"); |
| 107 | expect(tracker.dependencies).toEqual(new Set([entry])); |
| 108 | } finally { |
| 109 | await tracker.close(); |
| 110 | } |
| 111 | }); |
| 112 | }); |