unbound-cli 1.16.10 → 1.16.11

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unbound-cli",
3
- "version": "1.16.10",
3
+ "version": "1.16.11",
4
4
  "description": "CLI tool for Unbound - AI Gateway management",
5
5
  "main": "src/index.js",
6
6
  "bin": {
@@ -1,4 +1,5 @@
1
- const { spawn, execSync } = require('child_process');
1
+ const childProcess = require('child_process');
2
+ const { spawn } = childProcess;
2
3
  const fs = require('fs');
3
4
  const path = require('path');
4
5
  const os = require('os');
@@ -7,7 +8,9 @@ const output = require('../output');
7
8
  const telemetry = require('../telemetry');
8
9
  const { Option } = require('commander');
9
10
  const { shellEscape, downloadToFile, withSecureTempFile, DISCOVER_BASE_URL } = require('../utils');
10
- const LAUNCH_AGENT_LABEL = 'ai.getunbound.discovery';
11
+ // The scheduled-scan installer (setup-scheduled-scan.sh) registers this LaunchAgent
12
+ // and writes its logs; keep both in sync with that script.
13
+ const LAUNCH_AGENT_LABEL = 'ai.getunbound.scheduled';
11
14
 
12
15
  // install.sh exits with this code when the OS isn't supported for discovery
13
16
  // (e.g. Linux). Treated as a skipped scan, not a failure.
@@ -351,14 +354,14 @@ Examples:
351
354
 
352
355
  const home = os.homedir();
353
356
  const plistPath = path.join(home, 'Library/LaunchAgents', `${LAUNCH_AGENT_LABEL}.plist`);
354
- const logPath = path.join(home, 'Library/Logs/unbound/scan.log');
355
- const errPath = path.join(home, 'Library/Logs/unbound/scan.err');
357
+ const logPath = path.join(home, 'Library/Logs/unbound/scheduled.log');
358
+ const errPath = path.join(home, 'Library/Logs/unbound/scheduled.err');
356
359
 
357
360
  const plistExists = fs.existsSync(plistPath);
358
361
 
359
362
  let isLoaded = false;
360
363
  try {
361
- execSync(`launchctl list "${LAUNCH_AGENT_LABEL}"`, { stdio: 'ignore' });
364
+ childProcess.execSync(`launchctl list "${LAUNCH_AGENT_LABEL}"`, { stdio: 'ignore' });
362
365
  isLoaded = true;
363
366
  } catch {
364
367
  // not loaded
@@ -1,7 +1,53 @@
1
1
  const { test } = require('node:test');
2
2
  const assert = require('node:assert/strict');
3
+ const fs = require('fs');
4
+ const os = require('os');
5
+ const path = require('path');
3
6
  const { Command } = require('commander');
4
7
  const { classifyDiscoveryExit, register } = require('../src/commands/discover');
8
+ const output = require('../src/output');
9
+
10
+ // `discover status` must point at what the scheduled-scan installer actually
11
+ // creates: the LaunchAgent labelled `ai.getunbound.scheduled` and the
12
+ // scheduled.log / scheduled.err files under ~/Library/Logs/unbound. Otherwise a
13
+ // scheduled scan reports "No schedule". Runs the real status action with the
14
+ // platform stubbed to darwin and output.keyValue captured.
15
+ test('discover status: uses the scheduled-scan LaunchAgent label and log paths', async () => {
16
+ const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'unbound-discover-status-'));
17
+ const origHome = process.env.HOME;
18
+ const origPlatform = process.platform;
19
+ const origKeyValue = output.keyValue;
20
+ const origHomedir = os.homedir;
21
+
22
+ os.homedir = () => tmp;
23
+ process.env.HOME = tmp;
24
+ Object.defineProperty(process, 'platform', { value: 'darwin', configurable: true });
25
+
26
+ // Make the plist exist so the "LaunchAgent" row shows its resolved path.
27
+ const plistPath = path.join(tmp, 'Library/LaunchAgents', 'ai.getunbound.scheduled.plist');
28
+ fs.mkdirSync(path.dirname(plistPath), { recursive: true });
29
+ fs.writeFileSync(plistPath, '<plist/>');
30
+
31
+ const captured = {};
32
+ output.keyValue = (pairs) => { for (const [k, v] of pairs) captured[k] = v; };
33
+
34
+ try {
35
+ const program = new Command();
36
+ register(program);
37
+ await program.parseAsync(['node', 'x', 'discover', 'status']);
38
+
39
+ assert.equal(captured['LaunchAgent'], plistPath);
40
+ assert.equal(captured['Log file'], path.join(tmp, 'Library/Logs/unbound/scheduled.log'));
41
+ assert.equal(captured['Error log'], path.join(tmp, 'Library/Logs/unbound/scheduled.err'));
42
+ } finally {
43
+ output.keyValue = origKeyValue;
44
+ os.homedir = origHomedir;
45
+ Object.defineProperty(process, 'platform', { value: origPlatform, configurable: true });
46
+ if (origHome === undefined) delete process.env.HOME;
47
+ else process.env.HOME = origHome;
48
+ fs.rmSync(tmp, { recursive: true, force: true });
49
+ }
50
+ });
5
51
 
6
52
  // Existing crons can still pass a discovery key. With strict option parsing the
7
53
  // CLI would reject an unregistered flag, so discover keeps --discovery-key as an
@@ -0,0 +1,165 @@
1
+ const { test } = require('node:test');
2
+ const assert = require('node:assert/strict');
3
+ const fs = require('fs');
4
+ const os = require('os');
5
+ const path = require('path');
6
+ const { Command } = require('commander');
7
+ const { register } = require('../src/commands/discover');
8
+ const output = require('../src/output');
9
+
10
+ // Hardening for `discover status`: the command must agree with what the
11
+ // scheduled-scan installer (coding-discovery-tool/setup-scheduled-scan.sh)
12
+ // actually creates — the `ai.getunbound.scheduled` LaunchAgent and the
13
+ // scheduled.log / scheduled.err files — and must degrade cleanly off macOS.
14
+
15
+ function withDarwinHome(fn) {
16
+ const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'unbound-discover-status-'));
17
+ const origHome = process.env.HOME;
18
+ const origPlatform = process.platform;
19
+ const origHomedir = os.homedir;
20
+ const origKeyValue = output.keyValue;
21
+ os.homedir = () => tmp;
22
+ process.env.HOME = tmp;
23
+ Object.defineProperty(process, 'platform', { value: 'darwin', configurable: true });
24
+ const captured = {};
25
+ output.keyValue = (pairs) => { for (const [k, v] of pairs) captured[k] = v; };
26
+ return (async () => {
27
+ try {
28
+ const program = new Command();
29
+ register(program);
30
+ await program.parseAsync(['node', 'x', 'discover', 'status']);
31
+ await fn(tmp, captured);
32
+ } finally {
33
+ output.keyValue = origKeyValue;
34
+ os.homedir = origHomedir;
35
+ Object.defineProperty(process, 'platform', { value: origPlatform, configurable: true });
36
+ if (origHome === undefined) delete process.env.HOME;
37
+ else process.env.HOME = origHome;
38
+ fs.rmSync(tmp, { recursive: true, force: true });
39
+ }
40
+ })();
41
+ }
42
+
43
+ // Cross-repo contract. The installer's wrapper writes a run marker to
44
+ // scheduled.log in exactly this shape:
45
+ // log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> scheduled.log; }
46
+ // log "=== Starting Unbound scheduled run ==="
47
+ // `discover status` parses the last such line into "Last scan". If either side
48
+ // drifts, Last scan silently reverts to "-". This seeds a real installer-format
49
+ // line and asserts the timestamp is recovered.
50
+ test('discover status: parses Last scan from the installer\'s real scheduled.log line', async () => {
51
+ await withDarwinHome(async (tmp, captured) => {
52
+ // (setup happens before parseAsync, so write the log then re-run in-band)
53
+ const logDir = path.join(tmp, 'Library/Logs/unbound');
54
+ fs.mkdirSync(logDir, { recursive: true });
55
+ fs.writeFileSync(path.join(logDir, 'scheduled.log'),
56
+ '[2026-09-20 09:00:01] === Starting Unbound scheduled run ===\n' +
57
+ 'discovered 3 tools\n' +
58
+ '[2026-09-21 09:00:02] === Starting Unbound scheduled run ===\n' +
59
+ '[2026-09-21 09:00:04] === Finished ===\n');
60
+ // Re-run status now that the log exists.
61
+ const program = new Command();
62
+ register(program);
63
+ await program.parseAsync(['node', 'x', 'discover', 'status']);
64
+ assert.equal(captured['Last scan'], '2026-09-21 09:00:02', 'must report the latest run marker');
65
+ assert.equal(captured['Log file'], path.join(logDir, 'scheduled.log'));
66
+ });
67
+ });
68
+
69
+ // A machine that still has ONLY the stale `ai.getunbound.discovery` plist (never
70
+ // re-ran the migrating installer) must read as unscheduled — the reader is bound
71
+ // to the new label, not the old one.
72
+ test('discover status: a stale ai.getunbound.discovery plist alone reads as Not installed', async () => {
73
+ await withDarwinHome(async (tmp, captured) => {
74
+ const laDir = path.join(tmp, 'Library/LaunchAgents');
75
+ fs.mkdirSync(laDir, { recursive: true });
76
+ fs.writeFileSync(path.join(laDir, 'ai.getunbound.discovery.plist'), '<plist/>');
77
+ const program = new Command();
78
+ register(program);
79
+ await program.parseAsync(['node', 'x', 'discover', 'status']);
80
+ assert.equal(captured['Scheduled'], 'No');
81
+ assert.equal(captured['LaunchAgent'], 'Not installed');
82
+ });
83
+ });
84
+
85
+ // Runs `discover status` on darwin with `launchctl` stubbed. `discover.js` calls
86
+ // `childProcess.execSync`, so replacing that method on the shared module object
87
+ // takes effect without re-requiring. `loaded` decides whether `launchctl list
88
+ // <label>` succeeds; `plistLabel` (if set) seeds that LaunchAgent plist. Returns
89
+ // the captured key/values and the exact launchctl command the reader ran.
90
+ async function runStatusWithLaunchctl({ plistLabel, loaded }) {
91
+ const cp = require('child_process');
92
+ const orig = {
93
+ execSync: cp.execSync, home: process.env.HOME, platform: process.platform,
94
+ homedir: os.homedir, keyValue: output.keyValue,
95
+ };
96
+ const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'unbound-discover-status-'));
97
+ if (plistLabel) {
98
+ const laDir = path.join(tmp, 'Library/LaunchAgents');
99
+ fs.mkdirSync(laDir, { recursive: true });
100
+ fs.writeFileSync(path.join(laDir, `${plistLabel}.plist`), '<plist/>');
101
+ }
102
+ let launchctlCmd = '';
103
+ cp.execSync = (cmd) => { launchctlCmd = cmd; if (loaded) return ''; throw new Error('not loaded'); };
104
+ os.homedir = () => tmp;
105
+ process.env.HOME = tmp;
106
+ Object.defineProperty(process, 'platform', { value: 'darwin', configurable: true });
107
+ const captured = {};
108
+ output.keyValue = (pairs) => { for (const [k, v] of pairs) captured[k] = v; };
109
+ try {
110
+ const program = new Command();
111
+ register(program);
112
+ await program.parseAsync(['node', 'x', 'discover', 'status']);
113
+ return { captured, launchctlCmd };
114
+ } finally {
115
+ cp.execSync = orig.execSync;
116
+ os.homedir = orig.homedir;
117
+ Object.defineProperty(process, 'platform', { value: orig.platform, configurable: true });
118
+ if (orig.home === undefined) delete process.env.HOME; else process.env.HOME = orig.home;
119
+ output.keyValue = orig.keyValue;
120
+ fs.rmSync(tmp, { recursive: true, force: true });
121
+ }
122
+ }
123
+
124
+ // The bug was "reports no schedule on a scheduled machine". A machine the
125
+ // installer scheduled must read Scheduled: Yes — which needs both the plist
126
+ // (new label) present AND `launchctl list <label>` to succeed. This asserts the
127
+ // reader queries launchctl under the SAME `ai.getunbound.scheduled` label it
128
+ // reads the plist under, so the two sides can't drift apart into a false "No".
129
+ test('discover status: installed + loaded scheduled agent reads Scheduled: Yes', async () => {
130
+ const { captured, launchctlCmd } = await runStatusWithLaunchctl({ plistLabel: 'ai.getunbound.scheduled', loaded: true });
131
+ assert.match(launchctlCmd, /launchctl list "ai\.getunbound\.scheduled"/, 'queries the new label, not the legacy one');
132
+ assert.equal(captured['Scheduled'], 'Yes (daily at 09:00)');
133
+ });
134
+
135
+ // The reverse guard: the plist is present but the agent is not loaded, so the
136
+ // scan is not actually scheduled — it must read No, not a false Yes off the plist alone.
137
+ test('discover status: installed but not loaded reads Scheduled: No', async () => {
138
+ const { captured } = await runStatusWithLaunchctl({ plistLabel: 'ai.getunbound.scheduled', loaded: false });
139
+ assert.equal(captured['Scheduled'], 'No');
140
+ assert.match(captured['LaunchAgent'], /ai\.getunbound\.scheduled\.plist$/);
141
+ });
142
+
143
+ // Off macOS the command must not touch launchd/plist state; it prints guidance
144
+ // pointing at the real Linux timer and Windows scheduled-task names and exits 1.
145
+ test('discover status: non-darwin prints OS guidance and does not read plist state', async () => {
146
+ const origPlatform = process.platform;
147
+ const origError = output.error;
148
+ const origExit = process.exitCode;
149
+ Object.defineProperty(process, 'platform', { value: 'linux', configurable: true });
150
+ let msg = '';
151
+ output.error = (m) => { msg += m; };
152
+ try {
153
+ const program = new Command();
154
+ register(program);
155
+ await program.parseAsync(['node', 'x', 'discover', 'status']);
156
+ assert.match(msg, /only supports macOS/);
157
+ assert.match(msg, /unbound-scheduled\.timer/, 'points at the real systemd timer');
158
+ assert.match(msg, /ai\.getunbound\.scheduled/, 'points at the real Windows task name');
159
+ assert.equal(process.exitCode, 1);
160
+ } finally {
161
+ output.error = origError;
162
+ Object.defineProperty(process, 'platform', { value: origPlatform, configurable: true });
163
+ process.exitCode = origExit;
164
+ }
165
+ });