tokenusage-cli 0.2.0 → 0.2.2
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 +22 -0
- package/README.md +37 -7
- package/package.json +1 -1
- package/src/commands/init.js +37 -15
- package/src/commands/sync.js +3 -2
- package/src/lib/scheduled-task.js +4 -1
- package/src/lib/sync-report.js +4 -1
- package/test/audit-fixes.test.js +31 -0
- package/test/collection-controls.test.js +38 -1
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,28 @@
|
|
|
3
3
|
本包(`tokenusage-cli`)的用户可见变更记录。发版时在顶部新增一节,
|
|
4
4
|
`## <版本号>` 行会被 release workflow 取作 GitHub Release 正文。
|
|
5
5
|
|
|
6
|
+
## 0.2.2
|
|
7
|
+
|
|
8
|
+
新机初始化修复与采集口径修正。
|
|
9
|
+
|
|
10
|
+
- 修复严重初始化缺陷:从 npm 安装后 `tokenusage init` 必崩(包内缺少
|
|
11
|
+
`bin/tracker.js`——fork 改名 bin 后安装器没跟着改,0.1.0 起所有已发布版本
|
|
12
|
+
受影响;单测因跳过运行时拷贝未覆盖)。新增防回归测试直接执行运行时拷贝。
|
|
13
|
+
- init 新增 `--no-integrations`:只做扫描根配置、计划任务注册与运行上报,
|
|
14
|
+
**不改动任何 AI 工具的配置**(不装 hooks/notify/插件)。只要定时采集、
|
|
15
|
+
不想被触碰各工具配置的机器终于可以安全重跑 init。
|
|
16
|
+
- init 的欢迎屏与完成屏改为显示真实站点地址(原先写死一个本地端口残留)。
|
|
17
|
+
- 计划任务运行的口径修正:init 注册的隐藏任务改跑 `sync --from-scheduled`,
|
|
18
|
+
运行记录正确标记为"定时"(此前记成"手动"),并写入滚动日志(此前隐藏
|
|
19
|
+
运行无任何日志痕迹)。已在 0.2.0/0.2.1 注册过任务的机器重跑一次 init 即可升级。
|
|
20
|
+
|
|
21
|
+
## 0.2.1
|
|
22
|
+
|
|
23
|
+
修复 0.2.0 的严重回归:运行上报的版本号读取用了错误的相对路径(`Cannot find
|
|
24
|
+
module '../package.json'`),导致每轮 `sync` 启动即崩溃——定时与手动采集全部
|
|
25
|
+
中断。本地包实测通过后发布;并新增防回归测试,锁定嵌套模块的 package.json
|
|
26
|
+
require 深度,此类问题不再可能静默通过测试。
|
|
27
|
+
|
|
6
28
|
## 0.2.0
|
|
7
29
|
|
|
8
30
|
采集控制与运行记录:采集器从此可暂停、可审计。
|
package/README.md
CHANGED
|
@@ -7,7 +7,8 @@
|
|
|
7
7
|
## 快速开始(三步)
|
|
8
8
|
|
|
9
9
|
```bash
|
|
10
|
-
# 1)
|
|
10
|
+
# 1) 初始化:扫描根配置、各工具用量 hook、并注册每小时自动采集的计划任务
|
|
11
|
+
# (Windows 经 VBS 隐藏窗口运行,不弹黑框;--no-task 可跳过任务注册)
|
|
11
12
|
tokenusage init
|
|
12
13
|
|
|
13
14
|
# 2) 配对:输出一个 user_code,浏览器打开提示的链接批准
|
|
@@ -20,7 +21,8 @@ tokenusage sync # 手动一轮:解析 → 入队 → 上传
|
|
|
20
21
|
tokenusage sync --drain # 首次/积压补传(更大的批处理预算)
|
|
21
22
|
```
|
|
22
23
|
|
|
23
|
-
|
|
24
|
+
完成。init 注册的计划任务每小时自动采集一次;也可以随时手动 `tokenusage sync`,
|
|
25
|
+
或用 `--watch` 常驻自动上报(见下)。不想自动采集就 `tokenusage pause`(见下)。
|
|
24
26
|
|
|
25
27
|
**谁批准配对?** 批准者就是配对时登录站点的那个账号——设备与其全部数据绑定到该账号,账号之间数据完全隔离。自托管用户批准自己的设备即可;站点管理员不需要、也无法替别人审批。陌生人可以对公开站点发起配对码,但没有账号就批不了(注册邀请制 / `token_allowed_emails` 白名单),码 15 分钟自动过期。
|
|
26
28
|
|
|
@@ -28,17 +30,18 @@ tokenusage sync --drain # 首次/积压补传(更大的批处理预算)
|
|
|
28
30
|
|
|
29
31
|
| 命令 | 说明 |
|
|
30
32
|
|---|---|
|
|
31
|
-
| `tokenusage init` |
|
|
33
|
+
| `tokenusage init` | 本地初始化:扫描根配置、各工具 hook 安装、注册每小时计划任务(Windows 隐藏窗口;`--no-task` 跳过任务;`--run-report=off` 关闭运行记录上报;`--no-integrations` 不装任何 hook/notify/插件,只做纯定时采集)。不含配对 |
|
|
32
34
|
| `tokenusage device-login` | 设备流配对:输出 `user_code`,在站点 `/device` 页批准后本机拿到设备令牌 |
|
|
33
35
|
| `tokenusage sync` | 解析本地 AI 工具日志 → 上报小时桶 + 会话元数据 |
|
|
34
36
|
| `tokenusage sync --watch` | 常驻模式,默认每 15 分钟增量一轮(`--interval <ms>` 可调,≥60000) |
|
|
35
37
|
| `tokenusage sync --no-sessions` | 只上报小时桶,跳过会话元数据 |
|
|
36
38
|
| `tokenusage sync --drain` | 补传模式:更大的批处理预算,适合首次接入/长期积压 |
|
|
37
|
-
| `tokenusage
|
|
39
|
+
| `tokenusage pause` / `resume` | 暂停/恢复自动采集:定时任务、hook、`--watch` 全部静默停手,手动 `sync` 不受影响(Windows 上同步启停计划任务) |
|
|
40
|
+
| `tokenusage status` | 本地状态:Base URL、令牌、采集状态、运行上报开关、队列积压、最近解析/上传时间 |
|
|
38
41
|
| `tokenusage doctor` | 体检(运行时 / 网络 / 各工具配置),`--json` 输出 |
|
|
39
42
|
| `tokenusage diagnostics` | 诊断信息导出 |
|
|
40
43
|
| `tokenusage sessions` | 本地导出会话分析(JSON/CSV,元数据级) |
|
|
41
|
-
| `tokenusage uninstall [--purge]` | 移除 hook
|
|
44
|
+
| `tokenusage uninstall [--purge]` | 移除 hook 与计划任务;`--purge` 连数据目录一起删 |
|
|
42
45
|
|
|
43
46
|
配置优先级:CLI 参数 > `~/.tokenusage/.../config.json` > 环境变量(`TOKENUSAGE_BASE_URL` 等)> 内置默认(你的部署地址 + 其公开 publishable key)。
|
|
44
47
|
|
|
@@ -54,6 +57,32 @@ tokenusage sync --watch --interval 3600000 --no-sessions # 每小时,只传
|
|
|
54
57
|
- **停止**:在运行的终端 Ctrl+C 即可;`--watch` 只是前台进程,不注册任何系统计划任务/守护。随时终止都安全——队列与水位都已落盘,下次 `sync` 从断点继续。
|
|
55
58
|
- 每轮增量:首轮解析全部历史日志(可能较慢),之后只重扫有变化的文件;上报侧队列 offset + 会话水位双断点,多轮之间不重不漏。
|
|
56
59
|
|
|
60
|
+
## 采集控制:pause / resume 与计划任务
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
tokenusage pause # 暂停自动采集(定时任务 / hook / --watch 全部静默停手)
|
|
64
|
+
tokenusage resume # 恢复
|
|
65
|
+
tokenusage status # 查看当前采集状态
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
- 暂停是持久标志:`sync --auto`、hook/notify 触发、`--watch`、计划任务都会静默
|
|
69
|
+
退出(不报错、不上报、不留运行记录);**手动 `tokenusage sync` 永远可用**。
|
|
70
|
+
- Windows 上 init 会注册每小时计划任务 `TokenUsageSync`(隐藏窗口,无黑框),
|
|
71
|
+
pause/resume 顺势 disable/enable 它;`uninstall` 一并删除任务与脚本。
|
|
72
|
+
任务以 `--from-scheduled` 运行:运行记录标记为"定时"并写入滚动日志。
|
|
73
|
+
- 自动采集的日志落在 `~/.tokenusage/tracker/logs/sync.log`(1 MiB 滚动一代),
|
|
74
|
+
不依赖终端重定向。
|
|
75
|
+
|
|
76
|
+
## 运行记录上报
|
|
77
|
+
|
|
78
|
+
每轮 sync 结束后(**空跑也算**),CLI 向服务端上报一条聚合运行记录:时间、触发
|
|
79
|
+
方式(定时/手动/常驻/通知)、结果、耗时、解析文件数与上报量。仪表盘的
|
|
80
|
+
"采集记录"页用它展示采集器健康度——空跑记录就是"活着"的心跳。
|
|
81
|
+
|
|
82
|
+
- 内容只有聚合计数与版本字符串,无任何路径/标题/正文。
|
|
83
|
+
- 上报尽力而为:失败只记本地日志,绝不影响用量数据采集。
|
|
84
|
+
- 不想上报:`tokenusage init --run-report=off`(重跑 init 修改,配对不受影响)。
|
|
85
|
+
|
|
57
86
|
## 未配对时会发生什么
|
|
58
87
|
|
|
59
88
|
CLI 未配对也能正常用,只是**降级为“只采集、不上报”**:
|
|
@@ -76,7 +105,7 @@ CLI 未配对也能正常用,只是**降级为“只采集、不上报”**:
|
|
|
76
105
|
|
|
77
106
|
## 数据目录
|
|
78
107
|
|
|
79
|
-
`~/.tokenusage/`(`0700` 目录 + `0600`
|
|
108
|
+
`~/.tokenusage/`(`0700` 目录 + `0600` 文件):`tracker/config.json`(配对与运行配置)、上传队列(`queue.jsonl` + offset)、会话分析 sidecar、`session-upload-watermarks.json`(增量水位)、`tracker/logs/sync.log`(自动采集滚动日志)、暂停标志。与旧版 tokentracker CLI 的 `~/.tokentracker/` 互不干扰,迁移期两套 CLI 可在同一台机器共存。
|
|
80
109
|
|
|
81
110
|
## provider 支持面
|
|
82
111
|
|
|
@@ -91,4 +120,5 @@ pnpm install # 仓库根
|
|
|
91
120
|
pnpm --filter tokenusage-cli test
|
|
92
121
|
```
|
|
93
122
|
|
|
94
|
-
|
|
123
|
+
发布走仓库的 `cli-v*` tag 触发的 GitHub Actions(Trusted Publishing,见
|
|
124
|
+
`docs/cli-release.md`)。
|
package/package.json
CHANGED
package/src/commands/init.js
CHANGED
|
@@ -82,7 +82,7 @@ const {
|
|
|
82
82
|
resolveMuseHome,
|
|
83
83
|
resolveTraeStoragePath,
|
|
84
84
|
} = require("../lib/rollout");
|
|
85
|
-
const { resolveRuntimeConfig, DEFAULT_BASE_URL, normalizeInstanceBaseUrl, assertInsforgeRuntime,
|
|
85
|
+
const { resolveRuntimeConfig, DEFAULT_BASE_URL, DEFAULT_DASHBOARD_URL, normalizeInstanceBaseUrl, assertInsforgeRuntime,
|
|
86
86
|
clearDeviceIdentity, resetInstanceState } = require("../lib/runtime-config");
|
|
87
87
|
const { registerWindowsScheduledTask, TASK_NAME } = require("../lib/scheduled-task");
|
|
88
88
|
const {
|
|
@@ -193,7 +193,7 @@ async function cmdInit(argv, options = {}) {
|
|
|
193
193
|
const appDir = path.join(trackerDir, "app");
|
|
194
194
|
const trackerBinPath = path.join(appDir, "bin", "tracker.js");
|
|
195
195
|
|
|
196
|
-
renderWelcome();
|
|
196
|
+
renderWelcome(runtime.dashboardUrl || DEFAULT_DASHBOARD_URL);
|
|
197
197
|
|
|
198
198
|
if (opts.dryRun) {
|
|
199
199
|
process.stdout.write(`${color("Dry run: preview only (no changes applied).", DIM)}\n\n`);
|
|
@@ -273,11 +273,11 @@ async function cmdInit(argv, options = {}) {
|
|
|
273
273
|
ahaSpinner.stop();
|
|
274
274
|
}
|
|
275
275
|
|
|
276
|
-
renderLocalSuccess({ firstSync });
|
|
276
|
+
renderLocalSuccess({ firstSync, dashboardUrl: runtime.dashboardUrl || DEFAULT_DASHBOARD_URL });
|
|
277
277
|
await maybeShowStarCta({ trackerDir });
|
|
278
278
|
}
|
|
279
279
|
|
|
280
|
-
function renderWelcome() {
|
|
280
|
+
function renderWelcome(dashboardUrl = DEFAULT_DASHBOARD_URL) {
|
|
281
281
|
const providerCount = SUPPORTED_PROVIDERS.length;
|
|
282
282
|
// Show first 5 by name for grounding, then "+N more" so the line stays one row.
|
|
283
283
|
const previewNames = SUPPORTED_PROVIDERS.slice(0, 5).join(", ");
|
|
@@ -296,13 +296,13 @@ function renderWelcome() {
|
|
|
296
296
|
DIVIDER,
|
|
297
297
|
"",
|
|
298
298
|
` Tracks: ${providerLine}`,
|
|
299
|
-
` Dashboard:
|
|
299
|
+
` Dashboard: ${dashboardUrl}`,
|
|
300
300
|
"",
|
|
301
301
|
].join("\n"),
|
|
302
302
|
);
|
|
303
303
|
}
|
|
304
304
|
|
|
305
|
-
function renderLocalSuccess({ firstSync } = {}) {
|
|
305
|
+
function renderLocalSuccess({ firstSync, dashboardUrl = DEFAULT_DASHBOARD_URL } = {}) {
|
|
306
306
|
const lines = ["", `${BOLD}Setup complete!${RESET}`, ""];
|
|
307
307
|
|
|
308
308
|
if (firstSync && firstSync.totalTokens > 0) {
|
|
@@ -320,7 +320,7 @@ function renderLocalSuccess({ firstSync } = {}) {
|
|
|
320
320
|
|
|
321
321
|
lines.push(
|
|
322
322
|
"",
|
|
323
|
-
` Dashboard: ${CYAN}
|
|
323
|
+
` Dashboard: ${CYAN}${dashboardUrl}${RESET}`,
|
|
324
324
|
"",
|
|
325
325
|
);
|
|
326
326
|
process.stdout.write(lines.join("\n"));
|
|
@@ -351,6 +351,17 @@ function shouldUseBrowserAuth({ deviceToken, opts }) {
|
|
|
351
351
|
async function buildDryRunSummary({ opts, home, trackerDir, notifyPath, runtime }) {
|
|
352
352
|
const deviceToken = runtime?.deviceToken || null;
|
|
353
353
|
const pendingBrowserAuth = shouldUseBrowserAuth({ deviceToken, opts });
|
|
354
|
+
if (opts.noIntegrations) {
|
|
355
|
+
return {
|
|
356
|
+
summary: [{
|
|
357
|
+
label: "Integrations",
|
|
358
|
+
status: "skipped",
|
|
359
|
+
detail: "Skipped (--no-integrations); tool configs stay untouched",
|
|
360
|
+
}],
|
|
361
|
+
pendingBrowserAuth,
|
|
362
|
+
deviceToken,
|
|
363
|
+
};
|
|
364
|
+
}
|
|
354
365
|
const context = buildIntegrationTargets({ home, trackerDir, notifyPath });
|
|
355
366
|
const summary = await previewIntegrations({ context });
|
|
356
367
|
return { summary, pendingBrowserAuth, deviceToken };
|
|
@@ -415,13 +426,22 @@ async function runSetup({
|
|
|
415
426
|
|
|
416
427
|
await writeNotifyHandler({ trackerDir, notifyPath });
|
|
417
428
|
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
429
|
+
// --no-integrations (0.2.2): machines that only want scheduled scanning can
|
|
430
|
+
// take over the task without the CLI touching any tool's config — hooks,
|
|
431
|
+
// notify entries and the opencode plugin all stay exactly as they are.
|
|
432
|
+
const summary = opts.noIntegrations
|
|
433
|
+
? [{
|
|
434
|
+
label: "Integrations",
|
|
435
|
+
status: "skipped",
|
|
436
|
+
detail: "Skipped (--no-integrations); tool configs stay untouched",
|
|
437
|
+
}]
|
|
438
|
+
: await applyIntegrationSetup({
|
|
439
|
+
home,
|
|
440
|
+
trackerDir,
|
|
441
|
+
notifyPath,
|
|
442
|
+
notifyOriginalPath,
|
|
443
|
+
dryRun: Boolean(opts.dryRun),
|
|
444
|
+
});
|
|
425
445
|
|
|
426
446
|
// Windows scheduled task takeover (0.2.0): hourly auto sync behind a hidden
|
|
427
447
|
// VBS runner — no conhost flash, no shell redirect (sync self-logs to the
|
|
@@ -1453,6 +1473,7 @@ function parseArgs(argv) {
|
|
|
1453
1473
|
dryRun: false,
|
|
1454
1474
|
runReport: null,
|
|
1455
1475
|
noTask: false,
|
|
1476
|
+
noIntegrations: false,
|
|
1456
1477
|
};
|
|
1457
1478
|
|
|
1458
1479
|
for (let i = 0; i < argv.length; i++) {
|
|
@@ -1468,6 +1489,7 @@ function parseArgs(argv) {
|
|
|
1468
1489
|
else if (a === "--run-report") out.runReport = normalizeRunReportFlag(argv[++i]);
|
|
1469
1490
|
else if (a.startsWith("--run-report=")) out.runReport = normalizeRunReportFlag(a.slice("--run-report=".length));
|
|
1470
1491
|
else if (a === "--no-task") out.noTask = true;
|
|
1492
|
+
else if (a === "--no-integrations") out.noIntegrations = true;
|
|
1471
1493
|
else if (a === "--no-auth") out.noAuth = true;
|
|
1472
1494
|
else if (a === "--no-open") out.noOpen = true;
|
|
1473
1495
|
else if (a === "--yes") out.yes = true;
|
|
@@ -2329,7 +2351,7 @@ async function installLocalTrackerApp({ appDir }) {
|
|
|
2329
2351
|
// Copy the current package's runtime (bin + src) into ~/.tokenusage so notify can run sync without npx.
|
|
2330
2352
|
const packageRoot = path.resolve(__dirname, "../..");
|
|
2331
2353
|
const srcFrom = path.join(packageRoot, "src");
|
|
2332
|
-
const binFrom = path.join(packageRoot, "bin", "
|
|
2354
|
+
const binFrom = path.join(packageRoot, "bin", "tokenusage.js");
|
|
2333
2355
|
const packageJsonFrom = path.join(packageRoot, "package.json");
|
|
2334
2356
|
const nodeModulesFrom = path.join(packageRoot, "node_modules");
|
|
2335
2357
|
const dashboardDistFrom = path.join(packageRoot, "dashboard", "dist");
|
package/src/commands/sync.js
CHANGED
|
@@ -593,7 +593,7 @@ async function cmdSync(argv, context = {}) {
|
|
|
593
593
|
// log line, no run record. A manual sync is the user's explicit request
|
|
594
594
|
// and runs regardless of the flag.
|
|
595
595
|
const autoishSync = opts.auto || opts.background || opts.fromRetry || opts.fromNotify ||
|
|
596
|
-
opts.fromOpenclaw || context.trigger === "watch";
|
|
596
|
+
opts.fromOpenclaw || opts.fromScheduled || context.trigger === "watch";
|
|
597
597
|
if (autoishSync && isCollectionPaused(trackerDir)) return;
|
|
598
598
|
// Auto-flavored runs self-log to the rolling file so neither the scheduled
|
|
599
599
|
// task nor a TTY-less hook run loses its trail.
|
|
@@ -603,7 +603,7 @@ async function cmdSync(argv, context = {}) {
|
|
|
603
603
|
const syncRun = isRunReportEnabled(trackerDir)
|
|
604
604
|
? createSyncRunTelemetry({
|
|
605
605
|
trigger: resolveSyncTrigger(opts, context),
|
|
606
|
-
cliVersion: require("
|
|
606
|
+
cliVersion: require("../../package.json").version,
|
|
607
607
|
})
|
|
608
608
|
: null;
|
|
609
609
|
// Manual CLI sync is a one-time upload request, without changing the toggle.
|
|
@@ -3965,6 +3965,7 @@ function parseArgs(argv) {
|
|
|
3965
3965
|
else if (a === "--from-notify") out.fromNotify = true;
|
|
3966
3966
|
else if (a === "--from-retry") out.fromRetry = true;
|
|
3967
3967
|
else if (a === "--from-openclaw") out.fromOpenclaw = true;
|
|
3968
|
+
else if (a === "--from-scheduled") out.fromScheduled = true;
|
|
3968
3969
|
else if (a === "--source") {
|
|
3969
3970
|
out.source = normalizeSyncSource(argv[i + 1]);
|
|
3970
3971
|
i += 1;
|
|
@@ -37,6 +37,9 @@ function vbsPathFor(binDir) {
|
|
|
37
37
|
* pitfall (paths with spaces) never applies.
|
|
38
38
|
*/
|
|
39
39
|
function buildHiddenRunnerVbs({ nodeExe, cliJs }) {
|
|
40
|
+
// --from-scheduled: the run reports trigger=scheduled, self-logs via autoLog
|
|
41
|
+
// and uploads without consulting the cloud-sync preference — the registered
|
|
42
|
+
// task is the default collection path, not an opt-in flavor.
|
|
40
43
|
return [
|
|
41
44
|
"' Token Usage — hidden sync runner.",
|
|
42
45
|
"' The scheduled task launches this script (GUI subsystem: no conhost",
|
|
@@ -45,7 +48,7 @@ function buildHiddenRunnerVbs({ nodeExe, cliJs }) {
|
|
|
45
48
|
"' own rolling logs/sync.log — no shell redirect involved.",
|
|
46
49
|
"Dim shell",
|
|
47
50
|
"Set shell = CreateObject(\"WScript.Shell\")",
|
|
48
|
-
`WScript.Quit shell.Run("""${nodeExe}"" ""${cliJs}"" sync", 0, True)`,
|
|
51
|
+
`WScript.Quit shell.Run("""${nodeExe}"" ""${cliJs}"" sync --from-scheduled", 0, True)`,
|
|
49
52
|
"",
|
|
50
53
|
].join("\n");
|
|
51
54
|
}
|
package/src/lib/sync-report.js
CHANGED
|
@@ -23,10 +23,13 @@ function buildRunId(startedAtMs = Date.now(), randomBytes = 2) {
|
|
|
23
23
|
// background flavors (retry, publish, openclaw signal) fold into the four
|
|
24
24
|
// allowed values: hook/signal spawns are notify, everything scheduled or
|
|
25
25
|
// retrying is scheduled, the --watch loop is watch, a plain CLI run is manual.
|
|
26
|
+
// --from-scheduled marks the init-registered scheduled task's run: unlike
|
|
27
|
+
// --auto it bypasses the cloud-sync preference gate (the task must upload on
|
|
28
|
+
// default installs), which is why it needs its own flag.
|
|
26
29
|
function resolveSyncTrigger(opts = {}, context = {}) {
|
|
27
30
|
if (context?.trigger === "watch") return "watch";
|
|
28
31
|
if (opts?.fromNotify || opts?.fromOpenclaw) return "notify";
|
|
29
|
-
if (opts?.auto || opts?.background || opts?.fromRetry) return "scheduled";
|
|
32
|
+
if (opts?.fromScheduled || opts?.auto || opts?.background || opts?.fromRetry) return "scheduled";
|
|
30
33
|
return "manual";
|
|
31
34
|
}
|
|
32
35
|
|
package/test/audit-fixes.test.js
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
|
|
12
12
|
const assert = require("node:assert/strict");
|
|
13
13
|
const fs = require("node:fs");
|
|
14
|
+
const os = require("node:os");
|
|
14
15
|
const path = require("node:path");
|
|
15
16
|
const { test } = require("node:test");
|
|
16
17
|
|
|
@@ -18,6 +19,9 @@ const {
|
|
|
18
19
|
zcodeRowTotals,
|
|
19
20
|
buildZcodeRestorationRow,
|
|
20
21
|
} = require("../src/commands/sync");
|
|
22
|
+
const {
|
|
23
|
+
installLocalTrackerApp,
|
|
24
|
+
} = require("../src/commands/init");
|
|
21
25
|
const {
|
|
22
26
|
parseBucketKey,
|
|
23
27
|
bucketKey,
|
|
@@ -198,3 +202,30 @@ test("fix 5: TOKENUSAGE_BASE_URL/TOKENUSAGE_ANON_KEY win; INSFORGE_* remain lega
|
|
|
198
202
|
});
|
|
199
203
|
assert.equal(both.baseUrl, "https://new.example");
|
|
200
204
|
});
|
|
205
|
+
|
|
206
|
+
test("no stale dashboard constants in init output", () => {
|
|
207
|
+
const source = fs.readFileSync(
|
|
208
|
+
path.join(__dirname, "..", "src", "commands", "init.js"),
|
|
209
|
+
"utf8",
|
|
210
|
+
);
|
|
211
|
+
// The welcome/success banners print the configured dashboard URL; the old
|
|
212
|
+
// hardcoded TokenTracker default must not reappear in either renderer.
|
|
213
|
+
assert.ok(!source.includes("localhost:7680"), "stale localhost dashboard must not reappear in init.js");
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
test("init's runtime copy works from the published layout (bin/tokenusage.js)", async () => {
|
|
217
|
+
// 0.1.0–0.2.1 shipped without bin/tracker.js (the fork renamed the bin to
|
|
218
|
+
// tokenusage.js but the installer kept the old name), so `tokenusage init`
|
|
219
|
+
// crashed with ENOENT on every fresh npm install. The unit suite never ran
|
|
220
|
+
// this path: init tests set TOKENUSAGE_SKIP_LOCAL_RUNTIME_COPY=1.
|
|
221
|
+
const home = fs.mkdtempSync(path.join(os.tmpdir(), "tu-init-copy-"));
|
|
222
|
+
const appDir = path.join(home, "tracker", "app");
|
|
223
|
+
try {
|
|
224
|
+
await installLocalTrackerApp({ appDir });
|
|
225
|
+
assert.ok(fs.existsSync(path.join(appDir, "bin", "tracker.js")), "app/bin/tracker.js copied");
|
|
226
|
+
assert.ok(fs.existsSync(path.join(appDir, "src", "cli.js")), "app/src copied");
|
|
227
|
+
assert.ok(fs.existsSync(path.join(appDir, "package.json")), "app/package.json copied");
|
|
228
|
+
} finally {
|
|
229
|
+
fs.rmSync(home, { recursive: true, force: true });
|
|
230
|
+
}
|
|
231
|
+
});
|
|
@@ -173,9 +173,46 @@ test("hidden-runner VBS quotes paths individually and propagates the exit code",
|
|
|
173
173
|
cliJs: "C:\\Users\\me\\AppData\\tokenusage-cli\\src\\cli.js",
|
|
174
174
|
});
|
|
175
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/);
|
|
176
|
+
assert.match(vbs, /""C:\\Program Files\\nodejs\\node\.exe"" ""C:\\Users\\me\\AppData\\tokenusage-cli\\src\\cli\.js"" sync --from-scheduled/);
|
|
177
177
|
// Hidden window (0) + wait for completion (True).
|
|
178
178
|
assert.match(vbs, /, 0, True\)/);
|
|
179
179
|
// The task name stays stable — pause/resume and uninstall toggle by name.
|
|
180
180
|
assert.equal(TASK_NAME, "TokenUsageSync");
|
|
181
181
|
});
|
|
182
|
+
|
|
183
|
+
test("--from-scheduled reports as scheduled, unlike a plain manual run", () => {
|
|
184
|
+
assert.equal(resolveSyncTrigger({ fromScheduled: true }), "scheduled");
|
|
185
|
+
// The scheduled task must upload on default installs, so --from-scheduled
|
|
186
|
+
// deliberately does NOT map onto the cloud-sync-gated auto/background flags.
|
|
187
|
+
assert.equal(resolveSyncTrigger({ auto: true }), "scheduled");
|
|
188
|
+
assert.equal(resolveSyncTrigger({}), "manual");
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
// Runtime-require depth guard: a `require("../package.json")` inside src/commands
|
|
192
|
+
// or src/lib resolves to a nonexistent src/package.json and only explodes when
|
|
193
|
+
// the lazy call site runs (0.2.0 shipped exactly this in sync.js's report tail —
|
|
194
|
+
// the smoke tests never execute it). Only src/cli.js sits at the depth where the
|
|
195
|
+
// one-level form is valid; everything nested deeper must use ../../.
|
|
196
|
+
test("no nested module resolves package.json at the wrong depth", () => {
|
|
197
|
+
const srcDir = path.join(__dirname, "..", "src");
|
|
198
|
+
const offenders = [];
|
|
199
|
+
|
|
200
|
+
const walk = (dir) => {
|
|
201
|
+
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
202
|
+
const full = path.join(dir, entry.name);
|
|
203
|
+
if (entry.isDirectory()) {
|
|
204
|
+
walk(full);
|
|
205
|
+
} else if (entry.name.endsWith(".js")) {
|
|
206
|
+
const source = fs.readFileSync(full, "utf8");
|
|
207
|
+
if (source.includes('require("../package.json")')) {
|
|
208
|
+
const relative = path.relative(srcDir, full).split(path.sep).join("/");
|
|
209
|
+
|
|
210
|
+
if (relative !== "cli.js") offenders.push(relative);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
walk(srcDir);
|
|
217
|
+
assert.deepEqual(offenders, []);
|
|
218
|
+
});
|