| 1 | // Run: tsx src/__tests__/workspace-path-copy.test.tsx |
| 2 | |
| 3 | import { workspacePathCopyMenuItems } from "../lib/workspacePathCopyMenuItems"; |
| 4 | |
| 5 | let passed = 0; |
| 6 | let failed = 0; |
| 7 | |
| 8 | function ok(value: boolean, label: string) { |
| 9 | process.stdout.write(` ${value ? "PASS" : "FAIL"} ${label}\n`); |
| 10 | if (value) passed += 1; |
| 11 | else failed += 1; |
| 12 | } |
| 13 | |
| 14 | const clipboardWrites: string[] = []; |
| 15 | Object.defineProperty(globalThis, "navigator", { |
| 16 | configurable: true, |
| 17 | value: { |
| 18 | clipboard: { |
| 19 | writeText: async (value: string) => { |
| 20 | clipboardWrites.push(value); |
| 21 | }, |
| 22 | }, |
| 23 | }, |
| 24 | }); |
| 25 | |
| 26 | let closeCalls = 0; |
| 27 | let resolveCalls = 0; |
| 28 | let scopeCurrent = true; |
| 29 | const items = workspacePathCopyMenuItems({ |
| 30 | path: "src/", |
| 31 | resolveAbsolutePath: async () => { |
| 32 | resolveCalls += 1; |
| 33 | return "/repo/src"; |
| 34 | }, |
| 35 | isScopeCurrent: () => scopeCurrent, |
| 36 | close: () => { |
| 37 | closeCalls += 1; |
| 38 | }, |
| 39 | relativeLabel: "Copy relative path", |
| 40 | absoluteLabel: "Copy absolute path", |
| 41 | }); |
| 42 | |
| 43 | console.log("\nworkspace path copy menu"); |
| 44 | ok(items.length === 2, "builds both path-copy actions"); |
| 45 | ok(items[0]?.label === "Copy relative path", "exposes the localized relative-path label"); |
| 46 | ok(items[1]?.label === "Copy absolute path", "exposes the localized absolute-path label"); |
| 47 | |
| 48 | items[0]?.onSelect(); |
| 49 | await new Promise((resolve) => setTimeout(resolve, 0)); |
| 50 | ok(clipboardWrites[0] === "src", "folder relative paths omit the tree-only trailing slash"); |
| 51 | ok(closeCalls === 1, "relative-path selection closes the menu"); |
| 52 | |
| 53 | items[1]?.onSelect(); |
| 54 | await new Promise((resolve) => setTimeout(resolve, 0)); |
| 55 | ok(resolveCalls === 1, "absolute-path selection uses backend resolution"); |
| 56 | ok(clipboardWrites[1] === "/repo/src", "copies the backend-resolved absolute path"); |
| 57 | |
| 58 | scopeCurrent = false; |
| 59 | items[1]?.onSelect(); |
| 60 | await new Promise((resolve) => setTimeout(resolve, 0)); |
| 61 | ok(resolveCalls === 2 && clipboardWrites.length === 2, "drops stale absolute-path results after the workspace scope changes"); |
| 62 | |
| 63 | console.log(`\n${passed} passed, ${failed} failed, ${passed + failed} total`); |
| 64 | if (failed > 0) process.exit(1); |
| 65 |