Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodeChildProcess from "node:child_process";
3import * as NodeFSP from "node:fs/promises";
4import * as NodeFS from "node:fs";
5import * as NodeModule from "node:module";
6import * as NodeOS from "node:os";
7import * as NodePath from "node:path";
8import * as NodeURL from "node:url";
9import { assert, it } from "@effect/vitest";
10import * as Schema from "effect/Schema";
11import { build } from "vite-plus/pack";
12
13import serverPackage from "../../apps/server/package.json" with { type: "json" };
14import {
15 isExternalCliDependency,
16 selectCliRuntimeExternalDependencies,
17 shouldBundleCliDependency,
18} from "./cli-external-packages.ts";
19import { findEsmImportsOfExternalPackages } from "./cli-executable-imports.ts";
20
21const decodeManifest = Schema.decodeUnknownSync(
22 Schema.fromJsonString(
23 Schema.Struct({
24 name: Schema.optionalKey(Schema.String),
25 dependencies: Schema.optionalKey(Schema.Record(Schema.String, Schema.String)),
26 peerDependencies: Schema.optionalKey(Schema.Record(Schema.String, Schema.String)),
27 }),
28 ),
29);
30
31const repoRoot = NodeURL.fileURLToPath(new URL("../..", import.meta.url));
32
33async function stagePackage(name: string, from: string, destination: string): Promise<void> {
34 const require = NodeModule.createRequire(from);
35 let source = NodePath.dirname(require.resolve(name));
36 while (!NodeFS.existsSync(NodePath.join(source, "package.json"))) {
37 const parent = NodePath.dirname(source);
38 if (parent === source) throw new Error(`Cannot locate ${name}`);
39 source = parent;
40 }
41 let manifest = decodeManifest(
42 await NodeFSP.readFile(NodePath.join(source, "package.json"), "utf8"),
43 );
44 while (manifest.name !== name) {
45 source = NodePath.dirname(source);
46 if (source === NodePath.dirname(source)) throw new Error(`Cannot locate ${name}`);
47 if (NodeFS.existsSync(NodePath.join(source, "package.json"))) {
48 manifest = decodeManifest(
49 await NodeFSP.readFile(NodePath.join(source, "package.json"), "utf8"),
50 );
51 }
52 }
53 const target = NodePath.join(destination, "node_modules", name);
54 await NodeFSP.mkdir(NodePath.dirname(target), { recursive: true });
55 await NodeFSP.cp(source, target, {
56 recursive: true,
57 filter: (entry) => NodePath.basename(entry) !== "node_modules",
58 });
59 const dependencies = { ...manifest.dependencies, ...manifest.peerDependencies };
60 for (const dependency of Object.keys(dependencies)) {
61 await stagePackage(dependency, NodePath.join(source, "package.json"), target);
62 }
63}
64
65it("loads packaged Cursor catalog chunks without credentials or checkout dependencies", async () => {
66 const scratch = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-cursor-package-"));
67 try {
68 for (let parent = NodePath.dirname(scratch); ; parent = NodePath.dirname(parent)) {
69 assert.isFalse(NodeFS.existsSync(NodePath.join(parent, "node_modules")));
70 if (parent === NodePath.dirname(parent)) break;
71 }
72 const entry = NodePath.join(scratch, "probe.mjs");
73 const output = NodePath.join(scratch, "package");
74 await NodeFSP.writeFile(
75 entry,
76 `
77 import assert from 'node:assert/strict';
78 import { Cursor } from ${JSON.stringify(NodePath.join(repoRoot, "packages/provider-cursor/src/server/sdk.ts"))};
79 for (const [operation, request] of [
80 ['Cursor.models.list', () => Cursor.models.list({ apiKey: '' })],
81 ['Cursor.me', () => Cursor.me({ apiKey: '' })],
82 ]) {
83 await assert.rejects(request, error => {
84 assert.equal(error.name, 'ConfigurationError');
85 assert.equal(error.operation, operation);
86 assert.match(error.message, /empty apiKey explicitly/);
87 return true;
88 });
89 }
90 console.log('Cursor catalog chunks loaded; empty keys rejected locally');
91 `,
92 );
93 await build({
94 config: false,
95 entry: [entry],
96 outDir: output,
97 platform: "node",
98 format: "esm",
99 dts: false,
100 logLevel: "error",
101 deps: {
102 alwaysBundle: shouldBundleCliDependency,
103 neverBundle: isExternalCliDependency,
104 onlyBundle: false,
105 },
106 });
107 const roots = selectCliRuntimeExternalDependencies({
108 "@cursor/sdk": serverPackage.dependencies["@cursor/sdk"],
109 });
110 for (const name of Object.keys(roots)) {
111 await stagePackage(name, NodePath.join(repoRoot, "apps/server/package.json"), output);
112 }
113 const probe = NodePath.join(output, "probe.mjs");
114 assert.deepEqual(findEsmImportsOfExternalPackages(await NodeFSP.readFile(probe, "utf8")), []);
115 const stdout = NodeChildProcess.execFileSync(
116 process.execPath,
117 ["--no-global-search-paths", probe],
118 {
119 cwd: output,
120 env: {
121 HOME: scratch,
122 USERPROFILE: scratch,
123 PATH: "",
124 SystemRoot: process.env.SystemRoot ?? "",
125 },
126 encoding: "utf8",
127 timeout: 30_000,
128 },
129 );
130 assert.include(stdout, "Cursor catalog chunks loaded; empty keys rejected locally");
131 } finally {
132 await NodeFSP.rm(scratch, { recursive: true, force: true });
133 }
134}, 60_000);