import { spawnSync } from "node:child_process";
import { existsSync } from "node:fs";
import { resolve } from "node:path";
import { pathToFileURL } from "node:url";
import { describe, expect, it } from "vitest";
const LIB_BUNDLE = resolve("dist/index.js");
const CLI_BUNDLE = resolve("dist/cli/index.js");
describe("bundled dist — tokenizer path resolution", () => {
const libExists = existsSync(LIB_BUNDLE);
const cliExists = existsSync(CLI_BUNDLE);
(libExists ? it : it.skip)(
"dist/index.js resolves the tokenizer data file at package-root data/",
() => {
const libUrl = pathToFileURL(LIB_BUNDLE).href;
const result = spawnSync(
"node",
[
"--input-type=module",
"-e",
`import { truncateForModelByTokens } from "${libUrl}";
const s = "hello world ".repeat(500);
const out = truncateForModelByTokens(s, 100);
console.log(JSON.stringify({ ok: true, len: out.length }));`,
],
{ encoding: "utf8", timeout: 30_000 },
);
expect(result.status).toBe(0);
expect(result.stderr).not.toMatch(/deepseek-tokenizer\.json\.gz/);
expect(result.stderr).not.toMatch(/ENOENT/);
expect(result.stdout).toMatch(/"ok":true/);
},
);
(cliExists ? it : it.skip)(
"dist/cli/* inlines runtime deps so the desktop sidecar can run without node_modules",
async () => {
const { readdirSync, readFileSync } = await import("node:fs");
const distDir = resolve("dist/cli");
const jsFiles = readdirSync(distDir).filter((f) => f.endsWith(".js"));
const leakedImports = jsFiles.flatMap((f) => {
const body = readFileSync(resolve(distDir, f), "utf8");
const hits: string[] = [];
for (const pkg of ["commander", "ink", "undici"]) {
if (new RegExp(`from\\s*["']${pkg}["']`).test(body)) hits.push(`${f}:${pkg}`);
}
return hits;
});
expect(
leakedImports,
`dist/cli/*.js still imports runtime deps from node_modules: ${leakedImports.join(", ")}`,
).toEqual([]);
},
);
(cliExists ? it : it.skip)("dist/cli/index.js loads tokenizer before the first API fetch", () => {
const result = spawnSync("node", [CLI_BUNDLE, "run", "--no-config", "hi"], {
encoding: "utf8",
timeout: 10_000,
env: {
...process.env,
DEEPSEEK_API_KEY: "sk-smoke-test-bogus",
DEEPSEEK_BASE_URL: "http://127.0.0.1:1",
},
});
const combined = `${result.stdout}\n${result.stderr}`;
expect(combined).not.toMatch(/deepseek-tokenizer\.json\.gz/);
expect(combined).not.toMatch(/ENOENT.*tokenizer/i);
});
});