diff --git a/pm-stack/opencode-usage-export/opencode-usage-export.mjs b/pm-stack/opencode-usage-export/opencode-usage-export.mjs index 32935bf434..4db4c93cc8 100755 --- a/pm-stack/opencode-usage-export/opencode-usage-export.mjs +++ b/pm-stack/opencode-usage-export/opencode-usage-export.mjs @@ -59,10 +59,10 @@ * read-failed any other read error. Exit 6. */ import { appendFile, mkdir, readFile, rename, writeFile } from "node:fs/promises"; -import { existsSync } from "node:fs"; +import fs, { existsSync } from "node:fs"; import { homedir } from "node:os"; import { join } from "node:path"; -import { pathToFileURL } from "node:url"; +import { fileURLToPath } from "node:url"; import { DatabaseSync } from "node:sqlite"; export const SOURCE = "file-export/opencode-usage-laptop"; @@ -380,7 +380,15 @@ export async function run({ } } -if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { +const isMain = (() => { + try { + if (!process.argv[1]) return false; + return fs.realpathSync(process.argv[1]) === fs.realpathSync(fileURLToPath(import.meta.url)); + } catch { + return false; + } +})(); +if (isMain) { const { exitCode, doc } = await run({ argv: process.argv.slice(2) }); console.log(JSON.stringify({ status: doc.status, exitCode })); process.exit(exitCode); diff --git a/pm-stack/opencode-usage-export/opencode-usage-export.test.mjs b/pm-stack/opencode-usage-export/opencode-usage-export.test.mjs index 8dfe251632..741feaf92f 100644 --- a/pm-stack/opencode-usage-export/opencode-usage-export.test.mjs +++ b/pm-stack/opencode-usage-export/opencode-usage-export.test.mjs @@ -4,15 +4,70 @@ // live managed-instance store carries (probed 2026-11-02, task 6610). // Run: node --test pm-stack/opencode-usage-export/ import assert from "node:assert/strict"; +import { spawnSync } from "node:child_process"; +import fs from "node:fs"; import { mkdir, mkdtemp, readFile, writeFile } from "node:fs/promises"; -import { tmpdir } from "node:os"; -import { dirname, join } from "node:path"; +import os, { tmpdir } from "node:os"; +import path, { dirname, join } from "node:path"; import { test } from "node:test"; import { DatabaseSync } from "node:sqlite"; +import { fileURLToPath, pathToFileURL } from "node:url"; import { run } from "./opencode-usage-export.mjs"; const NOW = Date.parse("2026-11-02T18:00:00Z"); const HOUR = 3600000; +const EXPORTER_PATH = fileURLToPath(new URL("./opencode-usage-export.mjs", import.meta.url)); + +test("CLI: runs main via direct, file and directory symlinks", () => { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "opencode-usage-export-entrypoint-")); + const links = path.join(home, "links"); + fs.mkdirSync(links); + const fileLink = path.join(links, "export.mjs"); + const dirLink = path.join(links, "export-dir"); + fs.symlinkSync(EXPORTER_PATH, fileLink); + fs.symlinkSync(path.dirname(EXPORTER_PATH), dirLink, "dir"); + const env = { ...process.env, HOME: path.join(home, "empty-home") }; + + try { + for (const [name, entry] of [ + ["direct", EXPORTER_PATH], + ["file-symlink", fileLink], + ["directory-symlink", path.join(dirLink, path.basename(EXPORTER_PATH))], + ]) { + const outDir = path.join(home, name); + const result = spawnSync(process.execPath, [entry, "--out-dir", outDir], { + env, + encoding: "utf8", + timeout: 10000, + }); + assert.equal(result.status, 3, `${name}: ${result.stderr || result.stdout}`); + assert.deepEqual(JSON.parse(result.stdout), { status: "store-missing", exitCode: 3 }); + const doc = JSON.parse(fs.readFileSync(path.join(outDir, "latest.json"), "utf8")); + assert.equal(doc.status, "store-missing"); + } + } finally { + fs.rmSync(home, { recursive: true, force: true }); + } +}); + +test("importing opencode-usage-export.mjs as a library does not run main", () => { + const home = fs.mkdtempSync(path.join(os.tmpdir(), "opencode-usage-export-import-")); + try { + const result = spawnSync(process.execPath, [ + "--input-type=module", "-e", `await import(${JSON.stringify(pathToFileURL(EXPORTER_PATH).href)});`, + ], { + env: { ...process.env, HOME: home }, + encoding: "utf8", + timeout: 5000, + }); + assert.equal(result.status, 0, result.stderr); + assert.equal(result.stdout, ""); + assert.equal(result.stderr, ""); + assert.equal(fs.existsSync(path.join(home, "atlas", "opencode-usage-export")), false); + } finally { + fs.rmSync(home, { recursive: true, force: true }); + } +}); function makeStore(path, { sessions = [], messages = [] } = {}) { return (async () => {