Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions pm-stack/opencode-usage-export/opencode-usage-export.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
Expand Down
59 changes: 57 additions & 2 deletions pm-stack/opencode-usage-export/opencode-usage-export.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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, "");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Allow SQLite experimental warnings in the import test

On supported Node 22 releases, importing node:sqlite emits an ExperimentalWarning to stderr even when the exporter correctly skips its main routine. Since the exporter imports DatabaseSync unconditionally, this assertion makes the new library-import test fail with default warning settings on a runtime explicitly supported by the README. Suppress that known warning in the child process or distinguish it from exporter output rather than requiring stderr to be completely empty.

Useful? React with 👍 / 👎.

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 () => {
Expand Down