// Regression coverage for Task 474. Pins the per-file `../` math used by
// the installer bundler to rewrite a premium plugin's compiled
// `platform/lib/*` imports for the deployed layout.
//
// Without the intra-dist depth in the math, a file at `dist/sub/foo.js`
// produces an import that resolves one level too shallow — symptom:
// ERR_MODULE_NOT_FOUND on plugin spawn, which Claude Code surfaces as a
// `deferred_tools_delta` removing every tool the plugin contributes.
import test from "node:test";
import assert from "node:assert/strict";
import { resolve, dirname } from "node:path";
import { rewritePlatformLibImports } from "../bundler-rewrite-platform-lib.js";
// Source-side import shape preserved verbatim by tsc into compiled output.
// The leading `../` count is whatever the source-tree relative path
// required; the rewriter collapses it to the deploy-correct prefix.
const sourceImport = "../../../../../../platform/lib/foo/dist/index.js";
const sourceContent = `import { x } from "${sourceImport}";\n`;
function extractRewrittenImport(rewritten) {
    const m = rewritten.match(/"((?:\.\.\/)+lib\/foo\/dist\/index\.js)"/);
    assert.ok(m, `rewritten content has no matching import: ${rewritten}`);
    return m[1];
}
test("intra-dist depth 0, deployDepth 2 (standalone, dist/index.js)", () => {
    const out = rewritePlatformLibImports("index.js", sourceContent, 2);
    assert.equal(extractRewrittenImport(out), "../../lib/foo/dist/index.js");
});
test("intra-dist depth 1, deployDepth 2 (standalone, dist/tools/x.js)", () => {
    const out = rewritePlatformLibImports("tools/x.js", sourceContent, 2);
    assert.equal(extractRewrittenImport(out), "../../../lib/foo/dist/index.js");
});
test("intra-dist depth 0, deployDepth 4 (bundle-sub, dist/index.js)", () => {
    const out = rewritePlatformLibImports("index.js", sourceContent, 4);
    assert.equal(extractRewrittenImport(out), "../../../../lib/foo/dist/index.js");
});
test("intra-dist depth 2, deployDepth 4 (bundle-sub, dist/a/b/c.js)", () => {
    const out = rewritePlatformLibImports("a/b/c.js", sourceContent, 4);
    assert.equal(extractRewrittenImport(out), "../../../../../../lib/foo/dist/index.js");
});
test("standalone roundtrip: rewritten import resolves to bundle/lib/", () => {
    const entry = "tools/voice.js";
    const out = rewritePlatformLibImports(entry, sourceContent, 2);
    const rewritten = extractRewrittenImport(out);
    const deployedFile = resolve("/payload/premium-plugins/writer-craft/mcp/dist", entry);
    const resolved = resolve(dirname(deployedFile), rewritten);
    assert.equal(resolved, "/payload/premium-plugins/writer-craft/lib/foo/dist/index.js");
});
test("bundle-sub roundtrip: rewritten import resolves to bundle/lib/ from nested dist", () => {
    const entry = "a/b/c.js";
    const out = rewritePlatformLibImports(entry, sourceContent, 4);
    const rewritten = extractRewrittenImport(out);
    const deployedFile = resolve("/payload/premium-plugins/some-bundle/plugins/sub/mcp/dist", entry);
    const resolved = resolve(dirname(deployedFile), rewritten);
    assert.equal(resolved, "/payload/premium-plugins/some-bundle/lib/foo/dist/index.js");
});
test("multiple imports in one file all collapse to the same prefix", () => {
    const content = [
        'import a from "../../../../platform/lib/foo/dist/index.js";',
        'import b from "../../../../platform/lib/bar/dist/index.js";',
    ].join("\n");
    const out = rewritePlatformLibImports("tools/x.js", content, 2);
    assert.ok(out.includes('"../../../lib/foo/dist/index.js"'));
    assert.ok(out.includes('"../../../lib/bar/dist/index.js"'));
});
test("content without platform/lib imports is unchanged", () => {
    const content = 'import { local } from "./util.js";\n';
    const out = rewritePlatformLibImports("tools/x.js", content, 2);
    assert.equal(out, content);
});
