tokenusage-cli 0.1.0
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 +19 -0
- package/LICENSE +22 -0
- package/README.md +94 -0
- package/THIRD_PARTY_NOTICES.md +140 -0
- package/bin/tokenusage.js +69 -0
- package/package.json +59 -0
- package/src/cli.js +134 -0
- package/src/commands/device-login.js +205 -0
- package/src/commands/diagnostics.js +38 -0
- package/src/commands/doctor.js +110 -0
- package/src/commands/init.js +2436 -0
- package/src/commands/sessions.js +29 -0
- package/src/commands/status.js +1747 -0
- package/src/commands/sync.js +7934 -0
- package/src/commands/uninstall.js +312 -0
- package/src/lib/account-usage-dedup.js +267 -0
- package/src/lib/antigravity-paths.js +48 -0
- package/src/lib/ark-agent-plan-limits.js +438 -0
- package/src/lib/ark-coding-plan-limits.js +537 -0
- package/src/lib/async-queue-reader.js +138 -0
- package/src/lib/atomic-json.js +27 -0
- package/src/lib/bearer-token.js +11 -0
- package/src/lib/categorizer-utils.js +232 -0
- package/src/lib/claude-categorizer.js +1343 -0
- package/src/lib/claude-config.js +279 -0
- package/src/lib/claude-user-identity.js +56 -0
- package/src/lib/cli-ui.js +179 -0
- package/src/lib/cloud-account.js +541 -0
- package/src/lib/cloud-device-token.js +141 -0
- package/src/lib/cloud-sync-prefs.js +14 -0
- package/src/lib/cloud-usage-reset.js +115 -0
- package/src/lib/codex-config.js +595 -0
- package/src/lib/codex-context-breakdown.js +806 -0
- package/src/lib/codex-model-attribution.js +128 -0
- package/src/lib/codex-rollout-parser.js +1471 -0
- package/src/lib/codex-service-tier.js +49 -0
- package/src/lib/codex-token-refresh.js +140 -0
- package/src/lib/codex-token-usage.js +195 -0
- package/src/lib/codex-usage-record.js +195 -0
- package/src/lib/command-runner.js +313 -0
- package/src/lib/commandcode-limits.js +317 -0
- package/src/lib/context-health.js +124 -0
- package/src/lib/cursor-config.js +605 -0
- package/src/lib/cursor-store.js +1918 -0
- package/src/lib/debug-flags.js +7 -0
- package/src/lib/devin-limits.js +249 -0
- package/src/lib/devin-usage.js +212 -0
- package/src/lib/diagnostics.js +383 -0
- package/src/lib/doctor.js +385 -0
- package/src/lib/dumate.js +95 -0
- package/src/lib/fs.js +455 -0
- package/src/lib/function-url.js +14 -0
- package/src/lib/gemini-config.js +291 -0
- package/src/lib/git-outcomes.js +243 -0
- package/src/lib/grok-context-breakdown.js +890 -0
- package/src/lib/grok-hook.js +360 -0
- package/src/lib/grok-limits.js +865 -0
- package/src/lib/grok-usage.js +98 -0
- package/src/lib/heatmap-compact.js +114 -0
- package/src/lib/ingest-sanitize.js +41 -0
- package/src/lib/init-flow.js +42 -0
- package/src/lib/install-resolver.js +170 -0
- package/src/lib/jsonl-lines.js +134 -0
- package/src/lib/kimi-model-repair.js +173 -0
- package/src/lib/kimi-profile.js +61 -0
- package/src/lib/kimi-session.js +216 -0
- package/src/lib/local-api.js +3588 -0
- package/src/lib/machine-id.js +238 -0
- package/src/lib/multi-install-parser.js +113 -0
- package/src/lib/notify-command.js +38 -0
- package/src/lib/omp-hook.js +589 -0
- package/src/lib/openclaw-cli.js +104 -0
- package/src/lib/openclaw-hook.js +375 -0
- package/src/lib/openclaw-session-plugin.js +560 -0
- package/src/lib/opencode-config.js +121 -0
- package/src/lib/opencode-go-limits.js +807 -0
- package/src/lib/outcomes-engine.js +253 -0
- package/src/lib/passive-mode.js +219 -0
- package/src/lib/pet-packages.js +722 -0
- package/src/lib/pricing/curated-overrides.json +822 -0
- package/src/lib/pricing/index.js +379 -0
- package/src/lib/pricing/litellm-fetcher.js +172 -0
- package/src/lib/pricing/matcher.js +456 -0
- package/src/lib/pricing/seed-snapshot.json +1 -0
- package/src/lib/progress.js +76 -0
- package/src/lib/project-usage-purge.js +117 -0
- package/src/lib/prompt.js +24 -0
- package/src/lib/provider-status.js +104 -0
- package/src/lib/proxy-env.js +489 -0
- package/src/lib/proxy-settings.js +163 -0
- package/src/lib/qoder-cn-cli-limits.js +119 -0
- package/src/lib/qoder-limits.js +880 -0
- package/src/lib/quiet-warnings.js +26 -0
- package/src/lib/rollout.js +25966 -0
- package/src/lib/runtime-config.js +259 -0
- package/src/lib/scan-roots.js +277 -0
- package/src/lib/sentry.js +231 -0
- package/src/lib/session-analytics.js +2972 -0
- package/src/lib/session-performance.js +237 -0
- package/src/lib/session-upload.js +379 -0
- package/src/lib/skill-usage.js +267 -0
- package/src/lib/skills-manager.js +1591 -0
- package/src/lib/source-metadata.js +48 -0
- package/src/lib/sqlite-reader.js +215 -0
- package/src/lib/star-cta.js +73 -0
- package/src/lib/subscription-manager.js +293 -0
- package/src/lib/subscriptions.js +508 -0
- package/src/lib/telemetry.js +193 -0
- package/src/lib/tracker-paths.js +16 -0
- package/src/lib/trae-cn-config.js +548 -0
- package/src/lib/trae-db.js +127 -0
- package/src/lib/trae-snapshot.js +230 -0
- package/src/lib/trae-sqlite.js +143 -0
- package/src/lib/trae-usage.js +121 -0
- package/src/lib/upload-queue.js +134 -0
- package/src/lib/upload-throttle.js +171 -0
- package/src/lib/usage-limits.js +4552 -0
- package/src/lib/wrapped-aggregator.js +236 -0
- package/src/lib/wsl-probe.js +281 -0
- package/src/lib/zcode-limits.js +1172 -0
- package/src/lib/zcode-reset-credits.js +66 -0
- package/test/cli-smoke.test.js +53 -0
- package/test/ingest-sanitize.test.js +162 -0
- package/test/quiet-warnings.test.js +53 -0
- package/test/session-upload.test.js +427 -0
|
@@ -0,0 +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 };
|
|
@@ -0,0 +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
|
+
});
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// Regression tests for the upload-path poison-row defense. The ingest edge
|
|
4
|
+
// validates every row before writing and rejects the WHOLE batch on the
|
|
5
|
+
// first contract-invalid row; the queue offset only advances on success, so
|
|
6
|
+
// one bad row would otherwise stall every source behind it forever. These
|
|
7
|
+
// tests pin: sanitizeBucketForUpload coerces buckets into the contract shape
|
|
8
|
+
// (docs/cli-contract.md §2.4), readQueueBatch drops rows it cannot repair,
|
|
9
|
+
// and a dropped row never occupies its dedup key.
|
|
10
|
+
|
|
11
|
+
const assert = require("node:assert/strict");
|
|
12
|
+
const fs = require("node:fs");
|
|
13
|
+
const os = require("node:os");
|
|
14
|
+
const path = require("node:path");
|
|
15
|
+
const { test, beforeEach, afterEach } = require("node:test");
|
|
16
|
+
|
|
17
|
+
const { sanitizeBucketForUpload } = require("../src/lib/ingest-sanitize");
|
|
18
|
+
const { readQueueBatch } = require("../src/commands/sync");
|
|
19
|
+
const { validSessionId } = require("../src/lib/session-upload");
|
|
20
|
+
|
|
21
|
+
let tmpDir;
|
|
22
|
+
|
|
23
|
+
beforeEach(() => {
|
|
24
|
+
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "tokenusage-ingest-sanitize-"));
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
afterEach(() => {
|
|
28
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
function writeQueue(lines) {
|
|
32
|
+
const queuePath = path.join(tmpDir, "queue.jsonl");
|
|
33
|
+
fs.writeFileSync(queuePath, lines.map((line) => JSON.stringify(line)).join("\n") + "\n");
|
|
34
|
+
return queuePath;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function validBucket(overrides = {}) {
|
|
38
|
+
return {
|
|
39
|
+
hour_start: "2026-10-26T01:00:00.000Z",
|
|
40
|
+
source: "claude",
|
|
41
|
+
model: "claude-x",
|
|
42
|
+
input_tokens: 10,
|
|
43
|
+
cached_input_tokens: 2,
|
|
44
|
+
cache_creation_input_tokens: 3,
|
|
45
|
+
output_tokens: 4,
|
|
46
|
+
reasoning_output_tokens: 1,
|
|
47
|
+
total_tokens: 19,
|
|
48
|
+
billable_total_tokens: 19,
|
|
49
|
+
conversation_count: 1,
|
|
50
|
+
total_cost_usd: 0.5,
|
|
51
|
+
...overrides,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function validState(overrides = {}) {
|
|
56
|
+
return {
|
|
57
|
+
kind: "account_session_state",
|
|
58
|
+
source: "trae-cn",
|
|
59
|
+
session_id: "session-1",
|
|
60
|
+
model: "kimi-k2",
|
|
61
|
+
bucket_start: "2026-10-26T01:00:00.000Z",
|
|
62
|
+
input_tokens: 100,
|
|
63
|
+
output_tokens: 20,
|
|
64
|
+
cached_input_tokens: 30,
|
|
65
|
+
cache_creation_input_tokens: 10,
|
|
66
|
+
reasoning_output_tokens: 5,
|
|
67
|
+
total_tokens: 160,
|
|
68
|
+
snapshot_verified_at: "2026-10-26T02:00:00.000Z",
|
|
69
|
+
...overrides,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
test("sanitizeBucketForUpload floors fractional counters and clamps negatives to 0", () => {
|
|
74
|
+
const out = sanitizeBucketForUpload(validBucket({
|
|
75
|
+
input_tokens: 10.75,
|
|
76
|
+
output_tokens: -4,
|
|
77
|
+
reasoning_output_tokens: Number.NaN,
|
|
78
|
+
total_cost_usd: -0.5,
|
|
79
|
+
}));
|
|
80
|
+
assert.equal(out.input_tokens, 10);
|
|
81
|
+
assert.equal(out.output_tokens, 0);
|
|
82
|
+
assert.equal(out.reasoning_output_tokens, 0);
|
|
83
|
+
assert.equal(out.total_cost_usd, 0);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test("sanitizeBucketForUpload keeps a valid row verbatim, incl. explicit billable null", () => {
|
|
87
|
+
const row = validBucket({ billable_total_tokens: null });
|
|
88
|
+
assert.deepEqual(sanitizeBucketForUpload(row), row);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test("sanitizeBucketForUpload drops rows without a parseable hour_start and non-objects", () => {
|
|
92
|
+
assert.equal(sanitizeBucketForUpload(validBucket({ hour_start: "garbage" })), null);
|
|
93
|
+
assert.equal(sanitizeBucketForUpload(validBucket({ hour_start: 17 })), null);
|
|
94
|
+
assert.equal(sanitizeBucketForUpload(null), null);
|
|
95
|
+
assert.equal(sanitizeBucketForUpload([validBucket()]), null);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test("sanitizeBucketForUpload fills missing counters with 0 and cost with 0", () => {
|
|
99
|
+
const out = sanitizeBucketForUpload({
|
|
100
|
+
hour_start: "2026-10-26T01:00:00.000Z",
|
|
101
|
+
source: "x",
|
|
102
|
+
model: "m",
|
|
103
|
+
total_tokens: 5,
|
|
104
|
+
});
|
|
105
|
+
assert.equal(out.input_tokens, 0);
|
|
106
|
+
assert.equal(out.total_tokens, 5);
|
|
107
|
+
assert.equal(out.billable_total_tokens, null);
|
|
108
|
+
assert.equal(out.total_cost_usd, 0);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test("readQueueBatch drops poison account_session_states and keeps the valid one", async () => {
|
|
112
|
+
const tooLong = validState({ session_id: "s".repeat(300) });
|
|
113
|
+
const brokenSum = validState({ session_id: "broken", total_tokens: 99 });
|
|
114
|
+
const good = validState({ session_id: "good" });
|
|
115
|
+
const queuePath = writeQueue([validBucket(), tooLong, brokenSum, good]);
|
|
116
|
+
const result = await readQueueBatch(queuePath, 0, 500);
|
|
117
|
+
assert.equal(result.buckets.length, 1);
|
|
118
|
+
assert.equal(result.sessionStates.length, 1);
|
|
119
|
+
assert.equal(result.sessionStates[0].session_id, "good");
|
|
120
|
+
assert.equal(result.nextOffset, fs.statSync(queuePath).size);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test("a poison row does not occupy its dedup key — a later valid snapshot is admitted", async () => {
|
|
124
|
+
const poison = validState({ total_tokens: 99 });
|
|
125
|
+
const good = validState({ total_tokens: 160 });
|
|
126
|
+
const queuePath = writeQueue([poison, good]);
|
|
127
|
+
const result = await readQueueBatch(queuePath, 0, 500);
|
|
128
|
+
assert.equal(result.sessionStates.length, 1);
|
|
129
|
+
assert.equal(result.sessionStates[0].total_tokens, 160);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test("readQueueBatch drops hourly rows the edge would 400 and floors fractional counters", async () => {
|
|
133
|
+
const badHour = validBucket({ hour_start: "garbage" });
|
|
134
|
+
const fractional = validBucket({ hour_start: "2026-10-26T02:00:00.000Z", total_tokens: 19.5 });
|
|
135
|
+
const good = validBucket({ hour_start: "2026-10-26T03:00:00.000Z" });
|
|
136
|
+
const queuePath = writeQueue([badHour, fractional, good]);
|
|
137
|
+
const result = await readQueueBatch(queuePath, 0, 500);
|
|
138
|
+
assert.equal(result.buckets.length, 2);
|
|
139
|
+
assert.equal(result.buckets.find((b) => b.hour_start === "2026-10-26T02:00:00.000Z").total_tokens, 19);
|
|
140
|
+
assert.equal(result.buckets.find((b) => b.hour_start === "2026-10-26T03:00:00.000Z").total_tokens, 19);
|
|
141
|
+
assert.equal(result.nextOffset, fs.statSync(queuePath).size);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
test("a segment of only-poison rows reads empty with the offset advanced", async () => {
|
|
145
|
+
const queuePath = writeQueue([
|
|
146
|
+
validBucket({ hour_start: "garbage" }),
|
|
147
|
+
validState({ total_tokens: 99 }),
|
|
148
|
+
]);
|
|
149
|
+
const result = await readQueueBatch(queuePath, 0, 500);
|
|
150
|
+
assert.deepEqual(result.buckets, []);
|
|
151
|
+
assert.deepEqual(result.sessionStates, []);
|
|
152
|
+
assert.equal(result.nextOffset, fs.statSync(queuePath).size);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
test("validSessionId rejects the full Unicode Cc range (incl. C1), matching the server", () => {
|
|
156
|
+
assert.equal(validSessionId("ok-id"), true);
|
|
157
|
+
assert.equal(validSessionId("bad\u0085-id"), false); // NEL, C1
|
|
158
|
+
assert.equal(validSessionId("bad\u001f-id"), false); // C0
|
|
159
|
+
assert.equal(validSessionId("bad\u007f-id"), false); // DEL
|
|
160
|
+
assert.equal(validSessionId("with/slash"), false);
|
|
161
|
+
assert.equal(validSessionId("with\\slash"), false);
|
|
162
|
+
});
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// The bin entry installs the node:sqlite ExperimentalWarning filter before
|
|
4
|
+
// any require can pull node:sqlite in; redirected scheduled-task logs grow
|
|
5
|
+
// by one identical warning per cycle otherwise. Pin the filter's contract:
|
|
6
|
+
// drop only that warning, pass everything else through, stay idempotent.
|
|
7
|
+
|
|
8
|
+
const assert = require("node:assert/strict");
|
|
9
|
+
const { test } = require("node:test");
|
|
10
|
+
|
|
11
|
+
const { suppressSqliteExperimentalWarning } = require("../src/lib/quiet-warnings");
|
|
12
|
+
|
|
13
|
+
test("filters the node:sqlite experimental warning and passes everything else through", () => {
|
|
14
|
+
const seen = [];
|
|
15
|
+
const saved = process.emitWarning;
|
|
16
|
+
process.emitWarning = (warning, ...args) => {
|
|
17
|
+
seen.push(warning);
|
|
18
|
+
};
|
|
19
|
+
try {
|
|
20
|
+
suppressSqliteExperimentalWarning();
|
|
21
|
+
|
|
22
|
+
process.emitWarning(
|
|
23
|
+
"SQLite is an experimental feature and might change at any time",
|
|
24
|
+
{ type: "ExperimentalWarning", code: "ExperimentalWarnings" },
|
|
25
|
+
);
|
|
26
|
+
assert.equal(seen.length, 0);
|
|
27
|
+
|
|
28
|
+
const other = new Error("some other experimental feature");
|
|
29
|
+
other.name = "ExperimentalWarning";
|
|
30
|
+
process.emitWarning(other);
|
|
31
|
+
assert.equal(seen.length, 1);
|
|
32
|
+
assert.equal(seen[0], other);
|
|
33
|
+
|
|
34
|
+
process.emitWarning("plain string warning");
|
|
35
|
+
assert.equal(seen.length, 2);
|
|
36
|
+
assert.equal(seen[1], "plain string warning");
|
|
37
|
+
} finally {
|
|
38
|
+
process.emitWarning = saved;
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test("suppressSqliteExperimentalWarning is idempotent (does not stack filters)", () => {
|
|
43
|
+
const saved = process.emitWarning;
|
|
44
|
+
process.emitWarning = () => {};
|
|
45
|
+
try {
|
|
46
|
+
suppressSqliteExperimentalWarning();
|
|
47
|
+
const onceFiltered = process.emitWarning;
|
|
48
|
+
suppressSqliteExperimentalWarning();
|
|
49
|
+
assert.equal(process.emitWarning, onceFiltered);
|
|
50
|
+
} finally {
|
|
51
|
+
process.emitWarning = saved;
|
|
52
|
+
}
|
|
53
|
+
});
|