|
| 1 | +import fs from "node:fs"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | + |
| 5 | +import { afterEach, describe, expect, it } from "vitest"; |
| 6 | + |
| 7 | +import type { AgentNativeEjectManifest } from "../package-lifecycle/eject-manifest.js"; |
| 8 | +import { runEject, type EjectIO, type LoadedEjectManifest } from "./eject.js"; |
| 9 | + |
| 10 | +const tempDirs: string[] = []; |
| 11 | + |
| 12 | +afterEach(() => { |
| 13 | + for (const directory of tempDirs.splice(0)) { |
| 14 | + fs.rmSync(directory, { recursive: true, force: true }); |
| 15 | + } |
| 16 | +}); |
| 17 | + |
| 18 | +function fixture() { |
| 19 | + const root = fs.mkdtempSync(path.join(os.tmpdir(), "eject-test-")); |
| 20 | + tempDirs.push(root); |
| 21 | + fs.mkdirSync(path.join(root, "app"), { recursive: true }); |
| 22 | + fs.writeFileSync( |
| 23 | + path.join(root, "package.json"), |
| 24 | + `${JSON.stringify({ name: "example-app", dependencies: {} }, null, 2)}\n`, |
| 25 | + ); |
| 26 | + fs.writeFileSync( |
| 27 | + path.join(root, "app", "page.tsx"), |
| 28 | + [ |
| 29 | + 'import { ChatHistoryList } from "@agent-native/toolkit/chat-history";', |
| 30 | + 'import "@agent-native/toolkit/chat-history.css";', |
| 31 | + 'const runtimeValue = "@agent-native/toolkit/chat-history";', |
| 32 | + "export { ChatHistoryList };", |
| 33 | + "", |
| 34 | + ].join("\n"), |
| 35 | + ); |
| 36 | + |
| 37 | + const packageDir = path.join(root, "fixture-toolkit"); |
| 38 | + fs.mkdirSync(path.join(packageDir, "src", "chat-history"), { |
| 39 | + recursive: true, |
| 40 | + }); |
| 41 | + fs.writeFileSync( |
| 42 | + path.join(packageDir, "src", "chat-history", "index.ts"), |
| 43 | + 'export { ChatHistoryList } from "./ChatHistoryList.js";\n', |
| 44 | + ); |
| 45 | + fs.writeFileSync( |
| 46 | + path.join(packageDir, "src", "chat-history", "ChatHistoryList.tsx"), |
| 47 | + 'import { cn } from "../utils.js";\nexport const ChatHistoryList = () => cn("chat");\n', |
| 48 | + ); |
| 49 | + fs.writeFileSync( |
| 50 | + path.join(packageDir, "src", "chat-history", "ignored.spec.tsx"), |
| 51 | + "throw new Error('do not copy');\n", |
| 52 | + ); |
| 53 | + fs.writeFileSync( |
| 54 | + path.join(packageDir, "src", "utils.ts"), |
| 55 | + "export const cn = (...values: string[]) => values.join(' ');\n", |
| 56 | + ); |
| 57 | + fs.writeFileSync( |
| 58 | + path.join(packageDir, "src", "chat-history.css"), |
| 59 | + '@import "./chat-history/theme.css";\n.chat { color: red; }\n', |
| 60 | + ); |
| 61 | + fs.writeFileSync( |
| 62 | + path.join(packageDir, "src", "chat-history", "theme.css"), |
| 63 | + ".theme { color: blue; }\n", |
| 64 | + ); |
| 65 | + fs.writeFileSync( |
| 66 | + path.join(packageDir, "package.json"), |
| 67 | + `${JSON.stringify( |
| 68 | + { |
| 69 | + name: "@agent-native/toolkit", |
| 70 | + version: "1.2.3", |
| 71 | + dependencies: { clsx: "^2.1.0" }, |
| 72 | + }, |
| 73 | + null, |
| 74 | + 2, |
| 75 | + )}\n`, |
| 76 | + ); |
| 77 | + const manifest: AgentNativeEjectManifest = { |
| 78 | + manifestVersion: 1, |
| 79 | + package: "@agent-native/toolkit", |
| 80 | + catalogs: ["toolkit-ui"], |
| 81 | + units: [ |
| 82 | + { |
| 83 | + id: "toolkit/chat-history", |
| 84 | + label: "Chat history", |
| 85 | + catalog: "toolkit-ui", |
| 86 | + catalogItems: ["./chat-history", "./chat-history/*"], |
| 87 | + entrypoints: ["./chat-history", "./chat-history/*"], |
| 88 | + strategy: "source-copy", |
| 89 | + sourceEntries: ["src/chat-history"], |
| 90 | + targetRoot: "app/ejected/toolkit", |
| 91 | + styles: [ |
| 92 | + { |
| 93 | + entrypoint: "./chat-history.css", |
| 94 | + source: "src/chat-history.css", |
| 95 | + }, |
| 96 | + ], |
| 97 | + protectedImports: ["@agent-native/core/runtime"], |
| 98 | + verification: ["pnpm typecheck"], |
| 99 | + }, |
| 100 | + ], |
| 101 | + }; |
| 102 | + const loaded: LoadedEjectManifest = { |
| 103 | + manifest, |
| 104 | + manifestDigest: "manifest-sha256", |
| 105 | + packageDir, |
| 106 | + packageVersion: "1.2.3", |
| 107 | + }; |
| 108 | + return { root, loaded }; |
| 109 | +} |
| 110 | + |
| 111 | +function capture() { |
| 112 | + let out = ""; |
| 113 | + let err = ""; |
| 114 | + const io: EjectIO = { |
| 115 | + out(message) { |
| 116 | + out += `${message}\n`; |
| 117 | + }, |
| 118 | + err(message) { |
| 119 | + err += `${message}\n`; |
| 120 | + }, |
| 121 | + }; |
| 122 | + return { |
| 123 | + io, |
| 124 | + get out() { |
| 125 | + return out; |
| 126 | + }, |
| 127 | + get err() { |
| 128 | + return err; |
| 129 | + }, |
| 130 | + }; |
| 131 | +} |
| 132 | + |
| 133 | +describe("feature eject CLI wildcard traversal", () => { |
| 134 | + it.each([ |
| 135 | + "../utils", |
| 136 | + "nested/../utils", |
| 137 | + "/outside", |
| 138 | + "..\\outside", |
| 139 | + "C:outside", |
| 140 | + "C:/outside", |
| 141 | + ".", |
| 142 | + "nested//file", |
| 143 | + ])("rejects unsafe wildcard import %s before applying", async (wildcard) => { |
| 144 | + const { root, loaded } = fixture(); |
| 145 | + const consumer = path.join(root, "app", "page.tsx"); |
| 146 | + const original = `import "@agent-native/toolkit/chat-history/${wildcard}";\n`; |
| 147 | + fs.writeFileSync(consumer, original); |
| 148 | + const output = capture(); |
| 149 | + const result = await runEject(["toolkit/chat-history", "--apply"], { |
| 150 | + cwd: root, |
| 151 | + io: output.io, |
| 152 | + loadManifests: async () => [loaded], |
| 153 | + }); |
| 154 | + expect(fs.readFileSync(consumer, "utf8")).toBe(original); |
| 155 | + expect(result).toBe(1); |
| 156 | + expect(output.err).toContain("Unsafe wildcard"); |
| 157 | + expect(fs.existsSync(path.join(root, "app/ejected/toolkit"))).toBe(false); |
| 158 | + expect(fs.existsSync(path.join(root, "agent-native.ejections.json"))).toBe( |
| 159 | + false, |
| 160 | + ); |
| 161 | + }); |
| 162 | + |
| 163 | + it("rewrites safe nested wildcard imports into the ejected unit", async () => { |
| 164 | + const { root, loaded } = fixture(); |
| 165 | + const source = path.join(loaded.packageDir, "src/chat-history/nested"); |
| 166 | + fs.mkdirSync(source, { recursive: true }); |
| 167 | + fs.writeFileSync(path.join(source, "item.ts"), "export const item = 1;\n"); |
| 168 | + const consumer = path.join(root, "app", "page.tsx"); |
| 169 | + fs.writeFileSync( |
| 170 | + consumer, |
| 171 | + 'import { item } from "@agent-native/toolkit/chat-history/nested/item";\n', |
| 172 | + ); |
| 173 | + const output = capture(); |
| 174 | + expect( |
| 175 | + await runEject(["toolkit/chat-history", "--apply"], { |
| 176 | + cwd: root, |
| 177 | + io: output.io, |
| 178 | + loadManifests: async () => [loaded], |
| 179 | + }), |
| 180 | + ).toBe(0); |
| 181 | + expect(fs.readFileSync(consumer, "utf8")).toBe( |
| 182 | + 'import { item } from "./ejected/toolkit/chat-history/nested/item";\n', |
| 183 | + ); |
| 184 | + expect( |
| 185 | + fs.readFileSync( |
| 186 | + path.join(root, "app/ejected/toolkit/chat-history/nested/item.ts"), |
| 187 | + "utf8", |
| 188 | + ), |
| 189 | + ).toBe("export const item = 1;\n"); |
| 190 | + }); |
| 191 | +}); |
0 commit comments