tokenusage-cli 0.1.0 → 0.2.1

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.
Files changed (131) hide show
  1. package/CHANGELOG.md +46 -19
  2. package/LICENSE +22 -22
  3. package/README.md +123 -94
  4. package/THIRD_PARTY_NOTICES.md +140 -140
  5. package/bin/tokenusage.js +69 -69
  6. package/package.json +59 -59
  7. package/src/cli.js +146 -134
  8. package/src/commands/device-login.js +205 -205
  9. package/src/commands/diagnostics.js +38 -38
  10. package/src/commands/doctor.js +110 -110
  11. package/src/commands/init.js +2484 -2436
  12. package/src/commands/pause.js +77 -0
  13. package/src/commands/sessions.js +29 -29
  14. package/src/commands/status.js +1758 -1747
  15. package/src/commands/sync.js +8048 -7934
  16. package/src/commands/uninstall.js +322 -312
  17. package/src/lib/account-usage-dedup.js +267 -267
  18. package/src/lib/antigravity-paths.js +48 -48
  19. package/src/lib/ark-agent-plan-limits.js +438 -438
  20. package/src/lib/ark-coding-plan-limits.js +537 -537
  21. package/src/lib/async-queue-reader.js +138 -138
  22. package/src/lib/atomic-json.js +27 -27
  23. package/src/lib/bearer-token.js +11 -11
  24. package/src/lib/categorizer-utils.js +232 -232
  25. package/src/lib/claude-categorizer.js +1343 -1343
  26. package/src/lib/claude-config.js +279 -279
  27. package/src/lib/claude-user-identity.js +56 -56
  28. package/src/lib/cli-ui.js +179 -179
  29. package/src/lib/cloud-account.js +541 -541
  30. package/src/lib/cloud-device-token.js +141 -141
  31. package/src/lib/cloud-sync-prefs.js +14 -14
  32. package/src/lib/cloud-usage-reset.js +115 -115
  33. package/src/lib/codex-config.js +595 -595
  34. package/src/lib/codex-context-breakdown.js +806 -806
  35. package/src/lib/codex-model-attribution.js +128 -128
  36. package/src/lib/codex-rollout-parser.js +1471 -1471
  37. package/src/lib/codex-service-tier.js +49 -49
  38. package/src/lib/codex-token-refresh.js +140 -140
  39. package/src/lib/codex-token-usage.js +195 -195
  40. package/src/lib/codex-usage-record.js +195 -195
  41. package/src/lib/collection-pause.js +77 -0
  42. package/src/lib/command-runner.js +313 -313
  43. package/src/lib/commandcode-limits.js +317 -317
  44. package/src/lib/context-health.js +124 -124
  45. package/src/lib/cursor-config.js +605 -605
  46. package/src/lib/cursor-store.js +1918 -1918
  47. package/src/lib/debug-flags.js +7 -7
  48. package/src/lib/devin-limits.js +249 -249
  49. package/src/lib/devin-usage.js +212 -212
  50. package/src/lib/diagnostics.js +383 -383
  51. package/src/lib/doctor.js +385 -385
  52. package/src/lib/dumate.js +95 -95
  53. package/src/lib/fs.js +455 -455
  54. package/src/lib/function-url.js +14 -14
  55. package/src/lib/gemini-config.js +291 -291
  56. package/src/lib/git-outcomes.js +243 -243
  57. package/src/lib/grok-context-breakdown.js +890 -890
  58. package/src/lib/grok-hook.js +360 -360
  59. package/src/lib/grok-limits.js +865 -865
  60. package/src/lib/grok-usage.js +98 -98
  61. package/src/lib/heatmap-compact.js +114 -114
  62. package/src/lib/ingest-sanitize.js +41 -41
  63. package/src/lib/init-flow.js +42 -42
  64. package/src/lib/install-resolver.js +170 -170
  65. package/src/lib/jsonl-lines.js +134 -134
  66. package/src/lib/kimi-model-repair.js +173 -173
  67. package/src/lib/kimi-profile.js +61 -61
  68. package/src/lib/kimi-session.js +216 -216
  69. package/src/lib/local-api.js +3588 -3588
  70. package/src/lib/machine-id.js +238 -238
  71. package/src/lib/multi-install-parser.js +113 -113
  72. package/src/lib/notify-command.js +38 -38
  73. package/src/lib/omp-hook.js +589 -589
  74. package/src/lib/openclaw-cli.js +104 -104
  75. package/src/lib/openclaw-hook.js +375 -375
  76. package/src/lib/openclaw-session-plugin.js +560 -560
  77. package/src/lib/opencode-config.js +121 -121
  78. package/src/lib/opencode-go-limits.js +807 -807
  79. package/src/lib/outcomes-engine.js +253 -253
  80. package/src/lib/passive-mode.js +219 -219
  81. package/src/lib/pet-packages.js +722 -722
  82. package/src/lib/pricing/curated-overrides.json +822 -822
  83. package/src/lib/pricing/index.js +379 -379
  84. package/src/lib/pricing/litellm-fetcher.js +172 -172
  85. package/src/lib/pricing/matcher.js +456 -456
  86. package/src/lib/pricing/seed-snapshot.json +1 -1
  87. package/src/lib/progress.js +76 -76
  88. package/src/lib/project-usage-purge.js +117 -117
  89. package/src/lib/prompt.js +24 -24
  90. package/src/lib/provider-status.js +104 -104
  91. package/src/lib/proxy-env.js +489 -489
  92. package/src/lib/proxy-settings.js +163 -163
  93. package/src/lib/qoder-cn-cli-limits.js +119 -119
  94. package/src/lib/qoder-limits.js +880 -880
  95. package/src/lib/quiet-warnings.js +26 -26
  96. package/src/lib/rollout.js +26005 -25966
  97. package/src/lib/runtime-config.js +9 -4
  98. package/src/lib/scan-roots.js +277 -277
  99. package/src/lib/scheduled-task.js +145 -0
  100. package/src/lib/sentry.js +231 -231
  101. package/src/lib/session-analytics.js +2972 -2972
  102. package/src/lib/session-performance.js +237 -237
  103. package/src/lib/session-upload.js +379 -379
  104. package/src/lib/skill-usage.js +267 -267
  105. package/src/lib/skills-manager.js +1591 -1591
  106. package/src/lib/source-metadata.js +48 -48
  107. package/src/lib/sqlite-reader.js +215 -215
  108. package/src/lib/star-cta.js +73 -73
  109. package/src/lib/subscription-manager.js +293 -293
  110. package/src/lib/subscriptions.js +508 -508
  111. package/src/lib/sync-report.js +138 -0
  112. package/src/lib/telemetry.js +193 -193
  113. package/src/lib/tracker-paths.js +16 -16
  114. package/src/lib/trae-cn-config.js +548 -548
  115. package/src/lib/trae-db.js +127 -127
  116. package/src/lib/trae-snapshot.js +230 -230
  117. package/src/lib/trae-sqlite.js +143 -143
  118. package/src/lib/trae-usage.js +121 -121
  119. package/src/lib/upload-queue.js +134 -134
  120. package/src/lib/upload-throttle.js +171 -171
  121. package/src/lib/usage-limits.js +4552 -4552
  122. package/src/lib/wrapped-aggregator.js +236 -236
  123. package/src/lib/wsl-probe.js +281 -281
  124. package/src/lib/zcode-limits.js +1172 -1172
  125. package/src/lib/zcode-reset-credits.js +66 -66
  126. package/test/audit-fixes.test.js +200 -0
  127. package/test/cli-smoke.test.js +53 -53
  128. package/test/collection-controls.test.js +210 -0
  129. package/test/ingest-sanitize.test.js +162 -162
  130. package/test/quiet-warnings.test.js +53 -53
  131. package/test/session-upload.test.js +427 -427
@@ -1,66 +1,66 @@
1
- const ZCODE_RESET_STATUS_URL = "https://zcode.z.ai/api/v1/coding-plan/reset/status";
2
-
3
- function normalizeZcodeResetCredits(body, { nowMs = Date.now() } = {}) {
4
- if (body?.code !== 0 || !body.data) {
5
- throw new Error("Could not parse ZCode reset credits: invalid response");
6
- }
7
- const result = {};
8
- for (const [key, field] of [
9
- ["five_hour", "available_five_hour_resets"],
10
- ["weekly", "available_week_resets"],
11
- ]) {
12
- if (!Array.isArray(body.data[field])) {
13
- throw new Error("Could not parse ZCode reset credits: missing card list");
14
- }
15
- result[key] = body.data[field].map((card) => {
16
- const expiresMs = card?.expire_at;
17
- if (typeof expiresMs !== "number" || !Number.isFinite(expiresMs) || expiresMs <= 0
18
- || !Number.isFinite(new Date(expiresMs).getTime())) {
19
- throw new Error("Could not parse ZCode reset credits: invalid expiry");
20
- }
21
- return { expires_at: new Date(expiresMs).toISOString() };
22
- }).filter((card) => Date.parse(card.expires_at) > nowMs)
23
- .sort((a, b) => Date.parse(a.expires_at) - Date.parse(b.expires_at));
24
- }
25
- return result;
26
- }
27
-
28
- /** Read inventory only; send the two login tokens exclusively to ZCode's fixed status endpoint. */
29
- async function fetchZcodeResetCredits({
30
- zcodeToken, codingPlanToken, teamContext, fetchImpl = fetch, nowMs = Date.now(), timeoutMs = 2000,
31
- }) {
32
- const headers = {
33
- Authorization: /^Bearer\s/i.test(zcodeToken) ? zcodeToken : `Bearer ${zcodeToken}`,
34
- "X-Bigmodel-Authorization": codingPlanToken,
35
- "Bigmodel-Target-Type": teamContext ? "TEAM" : "PERSONAL",
36
- Accept: "application/json",
37
- };
38
- if (teamContext) {
39
- headers["Bigmodel-Organization"] = teamContext.organizationId;
40
- headers["Bigmodel-Project"] = teamContext.projectId;
41
- }
42
- const controller = new AbortController();
43
- let timer;
44
- try {
45
- const request = Promise.resolve().then(async () => {
46
- const response = await fetchImpl(ZCODE_RESET_STATUS_URL, {
47
- // Custom auth headers survive cross-origin redirects; never forward them.
48
- method: "GET", headers, signal: controller.signal, redirect: "error",
49
- });
50
- if (!response.ok) throw new Error(`ZCode reset credits API returned HTTP ${response.status}`);
51
- const body = await response.json();
52
- if (body?.code !== 0) throw new Error(`ZCode reset credits API error: code=${body?.code ?? "unknown"}`);
53
- return normalizeZcodeResetCredits(body, { nowMs });
54
- });
55
- return await Promise.race([request, new Promise((_, reject) => {
56
- timer = setTimeout(() => {
57
- controller.abort();
58
- reject(new Error("ZCode reset credits request timed out"));
59
- }, timeoutMs);
60
- })]);
61
- } finally {
62
- clearTimeout(timer);
63
- }
64
- }
65
-
66
- module.exports = { fetchZcodeResetCredits, normalizeZcodeResetCredits };
1
+ const ZCODE_RESET_STATUS_URL = "https://zcode.z.ai/api/v1/coding-plan/reset/status";
2
+
3
+ function normalizeZcodeResetCredits(body, { nowMs = Date.now() } = {}) {
4
+ if (body?.code !== 0 || !body.data) {
5
+ throw new Error("Could not parse ZCode reset credits: invalid response");
6
+ }
7
+ const result = {};
8
+ for (const [key, field] of [
9
+ ["five_hour", "available_five_hour_resets"],
10
+ ["weekly", "available_week_resets"],
11
+ ]) {
12
+ if (!Array.isArray(body.data[field])) {
13
+ throw new Error("Could not parse ZCode reset credits: missing card list");
14
+ }
15
+ result[key] = body.data[field].map((card) => {
16
+ const expiresMs = card?.expire_at;
17
+ if (typeof expiresMs !== "number" || !Number.isFinite(expiresMs) || expiresMs <= 0
18
+ || !Number.isFinite(new Date(expiresMs).getTime())) {
19
+ throw new Error("Could not parse ZCode reset credits: invalid expiry");
20
+ }
21
+ return { expires_at: new Date(expiresMs).toISOString() };
22
+ }).filter((card) => Date.parse(card.expires_at) > nowMs)
23
+ .sort((a, b) => Date.parse(a.expires_at) - Date.parse(b.expires_at));
24
+ }
25
+ return result;
26
+ }
27
+
28
+ /** Read inventory only; send the two login tokens exclusively to ZCode's fixed status endpoint. */
29
+ async function fetchZcodeResetCredits({
30
+ zcodeToken, codingPlanToken, teamContext, fetchImpl = fetch, nowMs = Date.now(), timeoutMs = 2000,
31
+ }) {
32
+ const headers = {
33
+ Authorization: /^Bearer\s/i.test(zcodeToken) ? zcodeToken : `Bearer ${zcodeToken}`,
34
+ "X-Bigmodel-Authorization": codingPlanToken,
35
+ "Bigmodel-Target-Type": teamContext ? "TEAM" : "PERSONAL",
36
+ Accept: "application/json",
37
+ };
38
+ if (teamContext) {
39
+ headers["Bigmodel-Organization"] = teamContext.organizationId;
40
+ headers["Bigmodel-Project"] = teamContext.projectId;
41
+ }
42
+ const controller = new AbortController();
43
+ let timer;
44
+ try {
45
+ const request = Promise.resolve().then(async () => {
46
+ const response = await fetchImpl(ZCODE_RESET_STATUS_URL, {
47
+ // Custom auth headers survive cross-origin redirects; never forward them.
48
+ method: "GET", headers, signal: controller.signal, redirect: "error",
49
+ });
50
+ if (!response.ok) throw new Error(`ZCode reset credits API returned HTTP ${response.status}`);
51
+ const body = await response.json();
52
+ if (body?.code !== 0) throw new Error(`ZCode reset credits API error: code=${body?.code ?? "unknown"}`);
53
+ return normalizeZcodeResetCredits(body, { nowMs });
54
+ });
55
+ return await Promise.race([request, new Promise((_, reject) => {
56
+ timer = setTimeout(() => {
57
+ controller.abort();
58
+ reject(new Error("ZCode reset credits request timed out"));
59
+ }, timeoutMs);
60
+ })]);
61
+ } finally {
62
+ clearTimeout(timer);
63
+ }
64
+ }
65
+
66
+ module.exports = { fetchZcodeResetCredits, normalizeZcodeResetCredits };
@@ -0,0 +1,200 @@
1
+ "use strict";
2
+
3
+ // Regression tests for the five audit P2 fixes shipped in 0.2.0
4
+ // (docs/cli-release.md §四 "正确性修复"):
5
+ // 1. init.js dead constant www.tokenusage.cc (stale domain gone);
6
+ // 2. zcode restoration rows carry billable_total_tokens / total_cost_usd;
7
+ // 3. bucket-key `|` round-trip with model names that contain `|`;
8
+ // 4. OpenCode signed-delta chain never leaves negative bucket totals;
9
+ // 5. runtime-config env names TOKENUSAGE_BASE_URL / TOKENUSAGE_ANON_KEY
10
+ // (INSFORGE_* stay as legacy fallbacks).
11
+
12
+ const assert = require("node:assert/strict");
13
+ const fs = require("node:fs");
14
+ const path = require("node:path");
15
+ const { test } = require("node:test");
16
+
17
+ const {
18
+ zcodeRowTotals,
19
+ buildZcodeRestorationRow,
20
+ } = require("../src/commands/sync");
21
+ const {
22
+ parseBucketKey,
23
+ bucketKey,
24
+ diffOpencodeTotals,
25
+ clampTotalsNonNegative,
26
+ } = require("../src/lib/rollout");
27
+ const {
28
+ DEFAULT_BASE_URL,
29
+ DEFAULT_DASHBOARD_URL,
30
+ resolveRuntimeConfig,
31
+ } = require("../src/lib/runtime-config");
32
+
33
+ test("fix 1: init.js no longer ships the stale www.tokenusage.cc constant", () => {
34
+ const source = fs.readFileSync(
35
+ path.join(__dirname, "..", "src", "commands", "init.js"),
36
+ "utf8",
37
+ );
38
+ assert.ok(!source.includes("www.tokenusage.cc"), "stale domain must not reappear in init.js");
39
+ // The effective default stays the production deployment in runtime-config.
40
+ assert.equal(DEFAULT_BASE_URL, "https://token-usage.baiwumm.com");
41
+ assert.equal(DEFAULT_DASHBOARD_URL, DEFAULT_BASE_URL);
42
+ });
43
+
44
+ test("fix 2: zcode totals fill billable/cost so restoration rows match the queue schema", () => {
45
+ // A legacy backup row without the two columns…
46
+ const legacy = {
47
+ input_tokens: 100,
48
+ cached_input_tokens: 0,
49
+ cache_creation_input_tokens: 0,
50
+ output_tokens: 50,
51
+ reasoning_output_tokens: 0,
52
+ total_tokens: 150,
53
+ conversation_count: 1,
54
+ };
55
+ const totals = zcodeRowTotals(legacy);
56
+ // …restores with billable = total (zcode plan-included usage counts) and a
57
+ // defined cost column (read-side pricing recomputes it).
58
+ assert.equal(totals.billable_total_tokens, 150);
59
+ assert.equal(totals.total_cost_usd, 0);
60
+
61
+ const row = buildZcodeRestorationRow({
62
+ model: "glm-4.6",
63
+ hour_start: "2026-10-27T09:30:00.000Z",
64
+ totals,
65
+ });
66
+ assert.equal(row.source, "zcode");
67
+ assert.equal(row.model, "glm-4.6");
68
+ assert.equal(row.hour_start, "2026-10-27T09:30:00.000Z");
69
+ assert.equal(row.billable_total_tokens, 150);
70
+ assert.equal(row.total_cost_usd, 0);
71
+ assert.equal(row.conversation_count, 1);
72
+
73
+ // Rows that DO carry the columns keep their values.
74
+ const modern = zcodeRowTotals({
75
+ total_tokens: 10,
76
+ billable_total_tokens: 4,
77
+ total_cost_usd: 0.25,
78
+ });
79
+ assert.equal(modern.billable_total_tokens, 4);
80
+ assert.equal(modern.total_cost_usd, 0.25);
81
+ });
82
+
83
+ test("fix 3: bucket keys round-trip model names containing the | separator", () => {
84
+ const model = "deepseek|v3.2";
85
+ const hour = "2026-10-27T09:30:00.000Z";
86
+ const key = bucketKey("opencode", model, hour);
87
+ assert.equal(key, `opencode|${model}|${hour}`);
88
+
89
+ const parsed = parseBucketKey(key);
90
+ assert.equal(parsed.source, "opencode");
91
+ assert.equal(parsed.model, model);
92
+ assert.equal(parsed.hourStart, hour);
93
+
94
+ // Group keys (source|hour) keep parsing without a model segment…
95
+ const group = parseBucketKey("zcode|2026-10-27T09:30:00.000Z");
96
+ assert.equal(group.source, "zcode");
97
+ assert.equal(group.model, "unknown");
98
+ assert.equal(group.hourStart, "2026-10-27T09:30:00.000Z");
99
+
100
+ // …and the zcode rebuild helper in sync.js shares the last-segment rule.
101
+ // (zcodeHourFromMainBucketKey is covered indirectly via the exported pure
102
+ // helpers; the semantics are identical: hour = after the LAST separator.)
103
+ const zcodeKey = `zcode|glm|4.5|${hour}`;
104
+ assert.equal(parseBucketKey(zcodeKey).hourStart, hour);
105
+ assert.equal(parseBucketKey(zcodeKey).model, "glm|4.5");
106
+ });
107
+
108
+ test("fix 4: signed OpenCode deltas never leave negative bucket totals", () => {
109
+ const previous = {
110
+ input_tokens: 100,
111
+ cached_input_tokens: 10,
112
+ cache_creation_input_tokens: 0,
113
+ output_tokens: 60,
114
+ reasoning_output_tokens: 20,
115
+ total_tokens: 190,
116
+ };
117
+ const corrected = {
118
+ input_tokens: 10,
119
+ cached_input_tokens: 0,
120
+ cache_creation_input_tokens: 0,
121
+ output_tokens: 5,
122
+ reasoning_output_tokens: 0,
123
+ total_tokens: 15,
124
+ };
125
+ const delta = diffOpencodeTotals(corrected, previous);
126
+ assert.ok(delta, "a correction produces a signed delta");
127
+ assert.equal(delta.total_tokens, -175);
128
+
129
+ // A correction whose prior contribution is NOT in the bucket anymore (lost
130
+ // cursor, fork-dedup removal) drives the bucket negative without the clamp.
131
+ const bucketTotals = {
132
+ input_tokens: 60,
133
+ cached_input_tokens: 0,
134
+ cache_creation_input_tokens: 0,
135
+ output_tokens: 40,
136
+ reasoning_output_tokens: 0,
137
+ total_tokens: 100,
138
+ billable_total_tokens: 100,
139
+ total_cost_usd: 0.2,
140
+ conversation_count: 2,
141
+ };
142
+ for (const key of Object.keys(delta)) {
143
+ bucketTotals[key] = (bucketTotals[key] || 0) + delta[key];
144
+ }
145
+ clampTotalsNonNegative(bucketTotals);
146
+ for (const key of ["input_tokens", "cached_input_tokens", "cache_creation_input_tokens", "output_tokens", "reasoning_output_tokens", "total_tokens", "billable_total_tokens", "total_cost_usd", "conversation_count"]) {
147
+ assert.ok(bucketTotals[key] >= 0, `${key} must stay non-negative (got ${bucketTotals[key]})`);
148
+ }
149
+ assert.equal(bucketTotals.total_tokens, 0);
150
+
151
+ // A correction that stays positive passes through untouched (no clamp).
152
+ const healthy = {
153
+ input_tokens: 150,
154
+ cached_input_tokens: 10,
155
+ cache_creation_input_tokens: 0,
156
+ output_tokens: 80,
157
+ reasoning_output_tokens: 20,
158
+ total_tokens: 260,
159
+ };
160
+ for (const key of Object.keys(delta)) {
161
+ healthy[key] = (healthy[key] || 0) + delta[key];
162
+ }
163
+ clampTotalsNonNegative(healthy);
164
+ assert.equal(healthy.total_tokens, 85);
165
+ assert.equal(healthy.input_tokens, 60);
166
+ });
167
+
168
+ test("fix 5: TOKENUSAGE_BASE_URL/TOKENUSAGE_ANON_KEY win; INSFORGE_* remain legacy fallbacks", () => {
169
+ // New primary name.
170
+ const primary = resolveRuntimeConfig({
171
+ env: { TOKENUSAGE_BASE_URL: "https://self-hosted.example" },
172
+ });
173
+ assert.equal(primary.baseUrl, "https://self-hosted.example");
174
+ assert.equal(primary.sources.baseUrl, "env");
175
+
176
+ // New anon-key name resolves alongside the new base-url name.
177
+ const withKey = resolveRuntimeConfig({
178
+ env: {
179
+ TOKENUSAGE_BASE_URL: "https://self-hosted.example",
180
+ TOKENUSAGE_ANON_KEY: "anon_" + "a".repeat(64),
181
+ },
182
+ });
183
+ assert.equal(withKey.anonKey, "anon_" + "a".repeat(64));
184
+ assert.equal(withKey.configurationError, null);
185
+
186
+ // Legacy INSFORGE_* names keep working (existing scripts/installs).
187
+ const legacy = resolveRuntimeConfig({
188
+ env: { TOKENUSAGE_INSFORGE_BASE_URL: "https://legacy.example" },
189
+ });
190
+ assert.equal(legacy.baseUrl, "https://legacy.example");
191
+
192
+ // The new name wins when both are set.
193
+ const both = resolveRuntimeConfig({
194
+ env: {
195
+ TOKENUSAGE_BASE_URL: "https://new.example",
196
+ TOKENUSAGE_INSFORGE_BASE_URL: "https://old.example",
197
+ },
198
+ });
199
+ assert.equal(both.baseUrl, "https://new.example");
200
+ });
@@ -1,53 +1,53 @@
1
- "use strict";
2
-
3
- // Smoke: the vendored CLI boots against the token-usage contract — the entry
4
- // loads, help/version print, and the device-flow authorize request matches
5
- // docs/cli-contract.md §2.1 (client_info + machine_id, no credentials).
6
-
7
- const assert = require("node:assert/strict");
8
- const { test } = require("node:test");
9
-
10
- test("cli entry loads and prints version + help", async () => {
11
- const { run } = require("../src/cli.js");
12
-
13
- let out = "";
14
- const originalWrite = process.stdout.write;
15
- process.stdout.write = (chunk) => {
16
- out += String(chunk);
17
- return true;
18
- };
19
- try {
20
- await run(["--version"]);
21
- assert.match(out, /^v\d+\.\d+\.\d+\n$/);
22
- out = "";
23
- await run(["--help"]);
24
- assert.match(out, /tokenusage/);
25
- assert.match(out, /--watch/);
26
- assert.ok(!out.includes("serve"));
27
- } finally {
28
- process.stdout.write = originalWrite;
29
- }
30
- });
31
-
32
- test("device-flow authorize request matches the contract shape", async () => {
33
- const captured = [];
34
- // Narrow seam: device-login reads fetchFunctionResponse through lib.
35
- const { functionUrlFor } = require("../src/lib/function-url");
36
-
37
- assert.equal(
38
- functionUrlFor("https://token-usage.baiwumm.com", "token-usage-device-flow-authorize"),
39
- "https://token-usage.baiwumm.com/api/functions/token-usage-device-flow-authorize",
40
- );
41
-
42
- const { resolveRuntimeConfig } = require("../src/lib/runtime-config");
43
- const runtime = resolveRuntimeConfig({});
44
- assert.equal(runtime.baseUrl, "https://token-usage.baiwumm.com");
45
- // Pre-GA deployments fall back to the production domain.
46
- assert.equal(
47
- resolveRuntimeConfig({ config: { baseUrl: "https://token-usage-delta.vercel.app" } }).baseUrl,
48
- "https://token-usage.baiwumm.com",
49
- );
50
- assert.ok(runtime.anonKey && runtime.anonKey.startsWith("sb_publishable_"));
51
- assert.equal(runtime.configurationError, null);
52
- assert.ok(captured.length === 0);
53
- });
1
+ "use strict";
2
+
3
+ // Smoke: the vendored CLI boots against the token-usage contract — the entry
4
+ // loads, help/version print, and the device-flow authorize request matches
5
+ // docs/cli-contract.md §2.1 (client_info + machine_id, no credentials).
6
+
7
+ const assert = require("node:assert/strict");
8
+ const { test } = require("node:test");
9
+
10
+ test("cli entry loads and prints version + help", async () => {
11
+ const { run } = require("../src/cli.js");
12
+
13
+ let out = "";
14
+ const originalWrite = process.stdout.write;
15
+ process.stdout.write = (chunk) => {
16
+ out += String(chunk);
17
+ return true;
18
+ };
19
+ try {
20
+ await run(["--version"]);
21
+ assert.match(out, /^v\d+\.\d+\.\d+\n$/);
22
+ out = "";
23
+ await run(["--help"]);
24
+ assert.match(out, /tokenusage/);
25
+ assert.match(out, /--watch/);
26
+ assert.ok(!out.includes("serve"));
27
+ } finally {
28
+ process.stdout.write = originalWrite;
29
+ }
30
+ });
31
+
32
+ test("device-flow authorize request matches the contract shape", async () => {
33
+ const captured = [];
34
+ // Narrow seam: device-login reads fetchFunctionResponse through lib.
35
+ const { functionUrlFor } = require("../src/lib/function-url");
36
+
37
+ assert.equal(
38
+ functionUrlFor("https://token-usage.baiwumm.com", "token-usage-device-flow-authorize"),
39
+ "https://token-usage.baiwumm.com/api/functions/token-usage-device-flow-authorize",
40
+ );
41
+
42
+ const { resolveRuntimeConfig } = require("../src/lib/runtime-config");
43
+ const runtime = resolveRuntimeConfig({});
44
+ assert.equal(runtime.baseUrl, "https://token-usage.baiwumm.com");
45
+ // Pre-GA deployments fall back to the production domain.
46
+ assert.equal(
47
+ resolveRuntimeConfig({ config: { baseUrl: "https://token-usage-delta.vercel.app" } }).baseUrl,
48
+ "https://token-usage.baiwumm.com",
49
+ );
50
+ assert.ok(runtime.anonKey && runtime.anonKey.startsWith("sb_publishable_"));
51
+ assert.equal(runtime.configurationError, null);
52
+ assert.ok(captured.length === 0);
53
+ });
@@ -0,0 +1,210 @@
1
+ "use strict";
2
+
3
+ // Regression tests for the 0.2.0 collection controls (docs/cli-contract.md
4
+ // §2.11 client semantics): the pause flag + run-report preference in
5
+ // ~/.tokenusage, the §2.11 telemetry builder (trigger resolution, idempotency
6
+ // run_id shape, best-effort report), and the scheduled-task takeover's pure
7
+ // parts (VBS wrapper, task naming).
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, beforeEach, afterEach } = require("node:test");
14
+
15
+ const {
16
+ isCollectionPaused,
17
+ isRunReportEnabled,
18
+ setCollectionPaused,
19
+ pauseFilePath,
20
+ } = require("../src/lib/collection-pause");
21
+ const {
22
+ buildRunId,
23
+ resolveSyncTrigger,
24
+ createSyncRunTelemetry,
25
+ } = require("../src/lib/sync-report");
26
+ const {
27
+ TASK_NAME,
28
+ buildHiddenRunnerVbs,
29
+ } = require("../src/lib/scheduled-task");
30
+
31
+ let tmpDir;
32
+
33
+ beforeEach(() => {
34
+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "tokenusage-collection-controls-"));
35
+ });
36
+
37
+ afterEach(() => {
38
+ fs.rmSync(tmpDir, { recursive: true, force: true });
39
+ });
40
+
41
+ test("pause flag round-trips and defaults to unpaused on missing/corrupt state", () => {
42
+ assert.equal(isCollectionPaused(tmpDir), false);
43
+
44
+ setCollectionPaused(tmpDir, true);
45
+ assert.equal(isCollectionPaused(tmpDir), true);
46
+ const raw = JSON.parse(fs.readFileSync(pauseFilePath(tmpDir), "utf8"));
47
+ assert.equal(raw.paused, true);
48
+ assert.ok(typeof raw.at === "string");
49
+
50
+ setCollectionPaused(tmpDir, false);
51
+ assert.equal(isCollectionPaused(tmpDir), false);
52
+ assert.equal(fs.existsSync(pauseFilePath(tmpDir)), false);
53
+
54
+ // A corrupt flag must never silently stop collection.
55
+ fs.mkdirSync(tmpDir, { recursive: true });
56
+ fs.writeFileSync(pauseFilePath(tmpDir), "{not json");
57
+ assert.equal(isCollectionPaused(tmpDir), false);
58
+ });
59
+
60
+ test("run-report preference defaults to on and honors runReport:false in config.json", () => {
61
+ assert.equal(isRunReportEnabled(tmpDir), true);
62
+
63
+ fs.writeFileSync(
64
+ path.join(tmpDir, "config.json"),
65
+ JSON.stringify({ runReport: false }),
66
+ );
67
+ assert.equal(isRunReportEnabled(tmpDir), false);
68
+
69
+ fs.writeFileSync(
70
+ path.join(tmpDir, "config.json"),
71
+ JSON.stringify({ runReport: true }),
72
+ );
73
+ assert.equal(isRunReportEnabled(tmpDir), true);
74
+
75
+ // Corrupt config must not disable reporting (default on).
76
+ fs.writeFileSync(path.join(tmpDir, "config.json"), "{broken");
77
+ assert.equal(isRunReportEnabled(tmpDir), true);
78
+ });
79
+
80
+ test("trigger resolution folds the auto-ish flavors into the §2.11 enum", () => {
81
+ assert.equal(resolveSyncTrigger({}), "manual");
82
+ assert.equal(resolveSyncTrigger({ auto: true }), "scheduled");
83
+ assert.equal(resolveSyncTrigger({ auto: true, background: true }), "scheduled");
84
+ assert.equal(resolveSyncTrigger({ auto: true, fromRetry: true }), "scheduled");
85
+ assert.equal(resolveSyncTrigger({ auto: true, fromNotify: true }), "notify");
86
+ assert.equal(resolveSyncTrigger({ auto: true, fromOpenclaw: true }), "notify");
87
+ assert.equal(resolveSyncTrigger({}, { trigger: "watch" }), "watch");
88
+ });
89
+
90
+ test("run_id follows the <started_at epoch ms>-<4hex> idempotency shape", () => {
91
+ const runId = buildRunId(1730000000000);
92
+ assert.match(runId, /^1730000000000-[0-9a-f]{4}$/);
93
+ const other = buildRunId(1730000000000);
94
+ assert.notEqual(runId, other);
95
+ });
96
+
97
+ test("telemetry snapshots carry exactly the §2.11 whitelist shape", () => {
98
+ const telemetry = createSyncRunTelemetry({
99
+ trigger: "scheduled",
100
+ startedAtMs: 1730000000000,
101
+ runId: "1730000000000-ab12",
102
+ cliVersion: "0.2.0",
103
+ osLabel: "win32 10.0.26200",
104
+ });
105
+ telemetry.noteParsedFiles(12);
106
+ telemetry.noteUpload({ inserted: 3, tokens: 4500 });
107
+ telemetry.noteSessions({ upserted: 2, skipped: 1 });
108
+ telemetry.noteQueueBytes(512);
109
+
110
+ const snapshot = telemetry.snapshot();
111
+ assert.deepEqual(snapshot, {
112
+ run_id: "1730000000000-ab12",
113
+ started_at: new Date(1730000000000).toISOString(),
114
+ ended_at: snapshot.ended_at,
115
+ trigger: "scheduled",
116
+ result: "ok",
117
+ parsed_files: 12,
118
+ buckets_uploaded: 3,
119
+ tokens_ingested: 4500,
120
+ sessions_uploaded: 2,
121
+ sessions_skipped: 1,
122
+ queue_bytes: 512,
123
+ cli_version: "0.2.0",
124
+ os: "win32 10.0.26200",
125
+ });
126
+
127
+ // An empty run derives exactly like the server-side rule expects clients
128
+ // NOT to send: no empty field, derived by the server from zero counters.
129
+ const empty = createSyncRunTelemetry({ trigger: "manual", cliVersion: "0.2.0" });
130
+ empty.noteUpload({ inserted: 0, tokens: 0 });
131
+ empty.noteSessions({ upserted: 0, skipped: 0 });
132
+ assert.equal(empty.snapshot().tokens_ingested, 0);
133
+ assert.equal(empty.snapshot().sessions_uploaded, 0);
134
+ });
135
+
136
+ test("report() is strictly best-effort: never throws, skips unpaired runs, logs failures", async () => {
137
+ const logs = [];
138
+ const telemetry = createSyncRunTelemetry({
139
+ trigger: "manual",
140
+ cliVersion: "0.2.0",
141
+ log: (line) => logs.push(line),
142
+ });
143
+
144
+ // Unpaired: no token/baseUrl — silent skip, no log noise.
145
+ assert.equal(await telemetry.report({}), false);
146
+ assert.equal(logs.length, 0);
147
+
148
+ // Paired but unreachable: logs locally, resolves false, never throws.
149
+ const failing = createSyncRunTelemetry({
150
+ trigger: "manual",
151
+ cliVersion: "0.2.0",
152
+ log: (line) => logs.push(line),
153
+ });
154
+ assert.equal(
155
+ await failing.report({ baseUrl: "http://127.0.0.1:9", deviceToken: "fake-token" }),
156
+ false,
157
+ );
158
+ assert.equal(logs.length, 1);
159
+ assert.match(logs[0], /Run report failed/);
160
+
161
+ // Only the first report() of a telemetry object ever attempts the network;
162
+ // later calls are silent no-ops (a run reports at most once).
163
+ assert.equal(
164
+ await failing.report({ baseUrl: "http://127.0.0.1:9", deviceToken: "fake-token" }),
165
+ false,
166
+ );
167
+ assert.equal(logs.length, 1);
168
+ });
169
+
170
+ test("hidden-runner VBS quotes paths individually and propagates the exit code", () => {
171
+ const vbs = buildHiddenRunnerVbs({
172
+ nodeExe: "C:\\Program Files\\nodejs\\node.exe",
173
+ cliJs: "C:\\Users\\me\\AppData\\tokenusage-cli\\src\\cli.js",
174
+ });
175
+ assert.match(vbs, /WScript\.Quit shell\.Run/);
176
+ assert.match(vbs, /""C:\\Program Files\\nodejs\\node\.exe"" ""C:\\Users\\me\\AppData\\tokenusage-cli\\src\\cli\.js"" sync/);
177
+ // Hidden window (0) + wait for completion (True).
178
+ assert.match(vbs, /, 0, True\)/);
179
+ // The task name stays stable — pause/resume and uninstall toggle by name.
180
+ assert.equal(TASK_NAME, "TokenUsageSync");
181
+ });
182
+
183
+ // Runtime-require depth guard: a `require("../package.json")` inside src/commands
184
+ // or src/lib resolves to a nonexistent src/package.json and only explodes when
185
+ // the lazy call site runs (0.2.0 shipped exactly this in sync.js's report tail —
186
+ // the smoke tests never execute it). Only src/cli.js sits at the depth where the
187
+ // one-level form is valid; everything nested deeper must use ../../.
188
+ test("no nested module resolves package.json at the wrong depth", () => {
189
+ const srcDir = path.join(__dirname, "..", "src");
190
+ const offenders = [];
191
+
192
+ const walk = (dir) => {
193
+ for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
194
+ const full = path.join(dir, entry.name);
195
+ if (entry.isDirectory()) {
196
+ walk(full);
197
+ } else if (entry.name.endsWith(".js")) {
198
+ const source = fs.readFileSync(full, "utf8");
199
+ if (source.includes('require("../package.json")')) {
200
+ const relative = path.relative(srcDir, full).split(path.sep).join("/");
201
+
202
+ if (relative !== "cli.js") offenders.push(relative);
203
+ }
204
+ }
205
+ }
206
+ };
207
+
208
+ walk(srcDir);
209
+ assert.deepEqual(offenders, []);
210
+ });