tokenusage-cli 0.2.3 → 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +51 -0
- package/README.md +16 -2
- package/package.json +1 -3
- package/src/cli.js +15 -9
- package/src/commands/init.js +288 -42
- package/src/commands/sync.js +39 -6
- package/src/commands/uninstall.js +3 -1
- package/src/lib/cli-ui.js +7 -1
- package/src/lib/command-runner.js +3 -0
- package/src/lib/doctor.js +70 -0
- package/src/lib/local-api.js +3 -0
- package/src/lib/omp-hook.js +2 -0
- package/src/lib/openclaw-hook.js +20 -7
- package/src/lib/openclaw-session-plugin.js +20 -6
- package/src/lib/rollout.js +19 -22
- package/src/lib/runtime-entry.js +35 -0
- package/src/lib/scheduled-task.js +47 -13
- package/test/audit-fixes.test.js +19 -1
- package/test/cli-options.test.js +11 -0
- package/test/cli-ui.test.js +47 -0
- package/test/collection-controls.test.js +27 -3
- package/test/packaging.test.js +29 -0
- package/test/runtime-copy.test.js +111 -0
- package/test/zstd-native.test.js +36 -0
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// init summary rows: skipped call-sites already include a "Skipped (...)"
|
|
4
|
+
// detail, while formatSummaryLine added its own "Skipped - " prefix. The
|
|
5
|
+
// result was "Skipped - Skipped (--no-integrations)" (0.2.4 acceptance).
|
|
6
|
+
// Keep the double prefix out without changing the success rows.
|
|
7
|
+
|
|
8
|
+
const assert = require("node:assert/strict");
|
|
9
|
+
const { test } = require("node:test");
|
|
10
|
+
|
|
11
|
+
const { formatSummaryLine } = require("../src/lib/cli-ui");
|
|
12
|
+
|
|
13
|
+
test("a detail that already starts with Skipped is not prefixed twice", () => {
|
|
14
|
+
const line = formatSummaryLine({
|
|
15
|
+
label: "Integrations",
|
|
16
|
+
status: "skipped",
|
|
17
|
+
detail: "Skipped (--no-integrations); tool configs stay untouched",
|
|
18
|
+
});
|
|
19
|
+
assert.ok(line.includes("[Skipped (--no-integrations); tool configs stay untouched]"), line);
|
|
20
|
+
assert.ok(!line.includes("Skipped - Skipped"), line);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test("a plain skipped detail still gets the Skipped - prefix", () => {
|
|
24
|
+
const line = formatSummaryLine({
|
|
25
|
+
label: "Runtime copy",
|
|
26
|
+
status: "skipped",
|
|
27
|
+
detail: "not installed",
|
|
28
|
+
});
|
|
29
|
+
assert.ok(line.includes("[Skipped - not installed]"), line);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("skipped rows without a detail keep the bare label", () => {
|
|
33
|
+
const line = formatSummaryLine({
|
|
34
|
+
label: "Scheduled task",
|
|
35
|
+
status: "skipped",
|
|
36
|
+
});
|
|
37
|
+
assert.ok(line.includes("[Skipped]"), line);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("success rows keep their detail text untouched", () => {
|
|
41
|
+
const line = formatSummaryLine({
|
|
42
|
+
label: "Claude Code",
|
|
43
|
+
status: "installed",
|
|
44
|
+
detail: "hook -> ~/.claude/settings.json",
|
|
45
|
+
});
|
|
46
|
+
assert.ok(line.includes("[hook -> ~/.claude/settings.json]"), line);
|
|
47
|
+
});
|
|
@@ -173,14 +173,38 @@ test("hidden-runner VBS quotes paths individually and propagates the exit code",
|
|
|
173
173
|
nodeExe: "C:\\Program Files\\nodejs\\node.exe",
|
|
174
174
|
cliJs: "C:\\Users\\me\\AppData\\tokenusage-cli\\bin\\tokenusage.js",
|
|
175
175
|
});
|
|
176
|
-
|
|
177
|
-
|
|
176
|
+
// Chr(34) builds the quotes: no VBS quote-escaping, no cmd /c layer, and
|
|
177
|
+
// paths with spaces stay intact.
|
|
178
|
+
assert.match(vbs, /q = Chr\(34\)/);
|
|
179
|
+
assert.ok(vbs.includes('nodeExe = "C:\\Program Files\\nodejs\\node.exe"'));
|
|
180
|
+
assert.ok(vbs.includes('cliJs = "C:\\Users\\me\\AppData\\tokenusage-cli\\bin\\tokenusage.js"'));
|
|
181
|
+
assert.ok(vbs.includes('cmd = q & nodeExe & q & " " & q & cliJs & q & " sync --from-scheduled"'));
|
|
178
182
|
// Hidden window (0) + wait for completion (True).
|
|
179
|
-
assert.match(vbs,
|
|
183
|
+
assert.match(vbs, /WScript\.Quit shell\.Run\(cmd, 0, True\)/);
|
|
180
184
|
// The task name stays stable — pause/resume and uninstall toggle by name.
|
|
181
185
|
assert.equal(TASK_NAME, "TokenUsageSync");
|
|
182
186
|
});
|
|
183
187
|
|
|
188
|
+
// nvm-windows installs npm globals into a version-specific prefix, so the
|
|
189
|
+
// recorded package path vanishes after `nvm use` and the hourly run failed
|
|
190
|
+
// every time with "file not found". The VBS now falls back to the
|
|
191
|
+
// init-copied runtime, which lives under the data dir.
|
|
192
|
+
test("hidden-runner VBS falls back to the init runtime when the package path disappears", () => {
|
|
193
|
+
const fallback = "C:\\Users\\me\\.tokenusage\\tracker\\app\\bin\\tracker.js";
|
|
194
|
+
const vbs = buildHiddenRunnerVbs({
|
|
195
|
+
nodeExe: "C:\\Program Files\\nodejs\\node.exe",
|
|
196
|
+
cliJs: "C:\\Users\\me\\AppData\\Roaming\\npm\\node_modules\\tokenusage-cli\\bin\\tokenusage.js",
|
|
197
|
+
fallbackCliJs: fallback,
|
|
198
|
+
});
|
|
199
|
+
assert.ok(
|
|
200
|
+
vbs.includes(`If Not fso.FileExists(cliJs) Then cliJs = "${fallback}"`),
|
|
201
|
+
"the runtime copy must be the fallback target",
|
|
202
|
+
);
|
|
203
|
+
// Non-nvm layouts have nothing to fall back to; the line stays out.
|
|
204
|
+
const plain = buildHiddenRunnerVbs({ nodeExe: "node.exe", cliJs: "C:\\a\\b.js" });
|
|
205
|
+
assert.ok(!plain.includes("FileExists"), "no fallback probe without a fallback path");
|
|
206
|
+
});
|
|
207
|
+
|
|
184
208
|
// 0.2.2 regression: init registered src/cli.js instead of the bin entry. That
|
|
185
209
|
// file only exports run(), so the hidden task exited 0 within ~0.1 s without
|
|
186
210
|
// collecting or logging anything — a silent, permanently "successful" task.
|
package/test/packaging.test.js
CHANGED
|
@@ -52,3 +52,32 @@ test("the unported install heartbeat stays removed", () => {
|
|
|
52
52
|
assert.ok(!sync.includes("maybeSendHeartbeat"));
|
|
53
53
|
assert.ok(!sync.includes('require("../lib/telemetry")'));
|
|
54
54
|
});
|
|
55
|
+
|
|
56
|
+
test("every auto-sync spawn point gates on the runtime-copy readiness marker", () => {
|
|
57
|
+
// 0.2.4: a runtime copy without its marker is a half copy (missing deps) and
|
|
58
|
+
// dies with MODULE_NOT_FOUND inside an ignored stdio stream — collection
|
|
59
|
+
// looks alive while nothing is collected. Each generated/hidden launcher must
|
|
60
|
+
// check the marker before choosing it.
|
|
61
|
+
const init = readSource("src", "commands", "init.js");
|
|
62
|
+
assert.ok(init.includes("runtimeReadyMarkerPath"), "init.js must write and probe the marker");
|
|
63
|
+
|
|
64
|
+
const notify = readSource("src", "commands", "init.js");
|
|
65
|
+
assert.ok(
|
|
66
|
+
notify.includes("const hasLocalRuntime = fs.existsSync(runtimeReadyPath) && fs.existsSync(trackerBinPath);"),
|
|
67
|
+
"the notify handler must require both the launcher and the marker",
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
for (const file of [["src", "lib", "openclaw-hook.js"], ["src", "lib", "openclaw-session-plugin.js"]]) {
|
|
71
|
+
const source = readSource(...file);
|
|
72
|
+
assert.ok(
|
|
73
|
+
source.includes("runtimeReadyPath") && source.includes("packageEntryPath"),
|
|
74
|
+
`${file.join("/")} must bake the marker + installed-package fallback`,
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const sync = readSource("src", "commands", "sync.js");
|
|
79
|
+
assert.ok(
|
|
80
|
+
sync.includes("runtimeReadyPath") && sync.includes("packageEntryPath"),
|
|
81
|
+
"the auto-retry script must use the same launcher selection",
|
|
82
|
+
);
|
|
83
|
+
});
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// The init-copied runtime (`<trackerDir>/app`) is the launcher behind notify
|
|
4
|
+
// hooks, the hidden scheduled task and the auto-retry child. 0.2.4 made its
|
|
5
|
+
// readiness marker the single gate: a copy that never booted must never be
|
|
6
|
+
// spawned (it dies with MODULE_NOT_FOUND inside an ignored stdio stream, which
|
|
7
|
+
// is indistinguishable from "collection is running"), and `doctor` must say so.
|
|
8
|
+
|
|
9
|
+
const assert = require("node:assert/strict");
|
|
10
|
+
const fs = require("node:fs");
|
|
11
|
+
const os = require("node:os");
|
|
12
|
+
const path = require("node:path");
|
|
13
|
+
const { test } = require("node:test");
|
|
14
|
+
|
|
15
|
+
const { buildNotifyHandler } = require("../src/commands/init");
|
|
16
|
+
const { buildDoctorReport } = require("../src/lib/doctor");
|
|
17
|
+
const { runtimeAppDir, runtimeBinPath, runtimeReadyMarkerPath, RUNTIME_READY_MARKER } = require("../src/lib/runtime-entry");
|
|
18
|
+
|
|
19
|
+
const CLI_VERSION = require("../package.json").version;
|
|
20
|
+
|
|
21
|
+
async function withTrackerDir(fn) {
|
|
22
|
+
const home = fs.mkdtempSync(path.join(os.tmpdir(), "tu-runtime-copy-"));
|
|
23
|
+
const trackerDir = path.join(home, "tracker");
|
|
24
|
+
fs.mkdirSync(trackerDir, { recursive: true });
|
|
25
|
+
try {
|
|
26
|
+
return await fn({ home, trackerDir });
|
|
27
|
+
} finally {
|
|
28
|
+
fs.rmSync(home, { recursive: true, force: true });
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function runtimeCopyCheck(trackerDir) {
|
|
33
|
+
const report = await buildDoctorReport({
|
|
34
|
+
runtime: { baseUrl: "https://example.test", deviceToken: "present", dashboardUrl: "https://example.test" },
|
|
35
|
+
diagnostics: null,
|
|
36
|
+
fetch: async () => ({ ok: true, status: 200 }),
|
|
37
|
+
paths: { trackerDir, configPath: path.join(trackerDir, "config.json") },
|
|
38
|
+
});
|
|
39
|
+
return report.checks.find((check) => check.id === "runtime.copy");
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
test("runtime-entry: the marker lives next to the copy, not inside bin/", () => {
|
|
43
|
+
const trackerDir = path.join("home", ".tokenusage", "tracker");
|
|
44
|
+
assert.equal(runtimeAppDir(trackerDir), path.join(trackerDir, "app"));
|
|
45
|
+
assert.equal(runtimeBinPath(trackerDir), path.join(trackerDir, "app", "bin", "tracker.js"));
|
|
46
|
+
assert.equal(
|
|
47
|
+
runtimeReadyMarkerPath(runtimeAppDir(trackerDir)),
|
|
48
|
+
path.join(trackerDir, "app", RUNTIME_READY_MARKER),
|
|
49
|
+
);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test("doctor: no runtime copy is fine (hooks use the installed CLI)", async () => {
|
|
53
|
+
const check = await withTrackerDir(({ trackerDir }) => runtimeCopyCheck(trackerDir));
|
|
54
|
+
assert.equal(check.status, "ok");
|
|
55
|
+
assert.match(check.detail, /installed CLI/);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("doctor: a copy without its marker is reported as incomplete", async () => {
|
|
59
|
+
await withTrackerDir(async ({ trackerDir }) => {
|
|
60
|
+
const appDir = runtimeAppDir(trackerDir);
|
|
61
|
+
fs.mkdirSync(path.join(appDir, "bin"), { recursive: true });
|
|
62
|
+
fs.writeFileSync(path.join(appDir, "bin", "tracker.js"), "// half copy\n");
|
|
63
|
+
const check = await runtimeCopyCheck(trackerDir);
|
|
64
|
+
assert.equal(check.status, "warn");
|
|
65
|
+
assert.match(check.detail, /incomplete/);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test("doctor: a ready copy reports its version", async () => {
|
|
70
|
+
await withTrackerDir(async ({ trackerDir }) => {
|
|
71
|
+
const appDir = runtimeAppDir(trackerDir);
|
|
72
|
+
fs.mkdirSync(path.join(appDir, "bin"), { recursive: true });
|
|
73
|
+
fs.writeFileSync(path.join(appDir, "bin", "tracker.js"), "// copy\n");
|
|
74
|
+
fs.writeFileSync(
|
|
75
|
+
runtimeReadyMarkerPath(appDir),
|
|
76
|
+
JSON.stringify({ version: CLI_VERSION, installedAt: "2026-10-28T00:00:00.000Z" }),
|
|
77
|
+
);
|
|
78
|
+
const check = await runtimeCopyCheck(trackerDir);
|
|
79
|
+
assert.equal(check.status, "ok");
|
|
80
|
+
assert.match(check.detail, new RegExp(`v${CLI_VERSION.replace(/\./g, "\\.")}`));
|
|
81
|
+
assert.equal(check.meta.version, CLI_VERSION);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("doctor: a copy from an older CLI is flagged for a re-run", async () => {
|
|
86
|
+
await withTrackerDir(async ({ trackerDir }) => {
|
|
87
|
+
const appDir = runtimeAppDir(trackerDir);
|
|
88
|
+
fs.mkdirSync(path.join(appDir, "bin"), { recursive: true });
|
|
89
|
+
fs.writeFileSync(path.join(appDir, "bin", "tracker.js"), "// copy\n");
|
|
90
|
+
fs.writeFileSync(runtimeReadyMarkerPath(appDir), JSON.stringify({ version: "0.0.1" }));
|
|
91
|
+
const check = await runtimeCopyCheck(trackerDir);
|
|
92
|
+
assert.equal(check.status, "warn");
|
|
93
|
+
assert.match(check.detail, /older than the CLI/);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test("notify handler: marker + installed-package fallback baked into the template", () => {
|
|
98
|
+
const source = buildNotifyHandler({ trackerDir: path.join("C:", "home", ".tokenusage", "tracker") });
|
|
99
|
+
assert.ok(source.includes("runtimeReadyPath"), "the template must probe the readiness marker");
|
|
100
|
+
assert.ok(source.includes("packageEntryPath"), "the template must carry the installed CLI entry");
|
|
101
|
+
assert.ok(
|
|
102
|
+
source.includes("const hasLocalRuntime = fs.existsSync(runtimeReadyPath) && fs.existsSync(trackerBinPath);"),
|
|
103
|
+
"both the launcher and the marker are required",
|
|
104
|
+
);
|
|
105
|
+
assert.ok(
|
|
106
|
+
source.includes(": hasPackageEntry\n ? [process.execPath, packageEntryPath, ...syncArgs]"),
|
|
107
|
+
"the middle tier runs the installed package without npx",
|
|
108
|
+
);
|
|
109
|
+
// The generated file must stay dependency-free: only node built-ins.
|
|
110
|
+
assert.ok(!/require\(['"](?!node:)/.test(source), "notify.cjs must only require node: built-ins");
|
|
111
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// 0.2.5 dropped @mongodb-js/zstd: the package engine floor is >=22.19, so
|
|
4
|
+
// node:zlib can always decode zstd. These tests exercise the two real decode
|
|
5
|
+
// paths (Zed thread blobs and concatenated DeepSeek Harness frames) so a
|
|
6
|
+
// future dependency/engine change cannot silently remove zstd support.
|
|
7
|
+
|
|
8
|
+
const assert = require("node:assert/strict");
|
|
9
|
+
const zlib = require("node:zlib");
|
|
10
|
+
const { test } = require("node:test");
|
|
11
|
+
|
|
12
|
+
const { decodeDshZstd, decodeZedThreadBlob } = require("../src/lib/rollout");
|
|
13
|
+
|
|
14
|
+
test("the supported Node runtime has a built-in zstd decoder", () => {
|
|
15
|
+
assert.equal(typeof zlib.zstdDecompressSync, "function");
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test("Zed zstd thread blobs decode through the built-in decoder", async () => {
|
|
19
|
+
const text = JSON.stringify({ tokens: { input: 10, output: 4 }, model: "zed-test" });
|
|
20
|
+
const decoded = await decodeZedThreadBlob({
|
|
21
|
+
dataType: "zstd",
|
|
22
|
+
data: zlib.zstdCompressSync(Buffer.from(text)),
|
|
23
|
+
});
|
|
24
|
+
assert.equal(decoded, text);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test("concatenated DeepSeek Harness zstd frames are decoded one by one", async () => {
|
|
28
|
+
const first = "{\"seq\":1}\n";
|
|
29
|
+
const second = "{\"seq\":2}\n";
|
|
30
|
+
const payload = Buffer.concat([
|
|
31
|
+
zlib.zstdCompressSync(Buffer.from(first)),
|
|
32
|
+
zlib.zstdCompressSync(Buffer.from(second)),
|
|
33
|
+
]);
|
|
34
|
+
const decoded = await decodeDshZstd(payload, { maxOutputBytes: 4096 });
|
|
35
|
+
assert.equal(decoded.toString("utf8"), first + second);
|
|
36
|
+
});
|