@indigoai-us/hq-cli 5.17.0 → 5.18.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.
@@ -0,0 +1,235 @@
1
+ /**
2
+ * Unit tests for `narrow-hint-banner` (US-011).
3
+ *
4
+ * Coverage:
5
+ * 1. Emits a hint when sync mode is 'all' and not suppressed.
6
+ * 2. Skips emission for shared/custom memberships.
7
+ * 3. Dedupes per {companyUid, level} for the process lifetime.
8
+ * 4. Suppression via HQ_SYNC_NARROW_HINT=off (env).
9
+ * 5. Suppression via <hqRoot>/.hq/config.json `syncNarrowHint: 'off'`.
10
+ * 6. Warning + strict level messages.
11
+ * 7. `isStrictRefusal` only fires for strict + 'all'.
12
+ * 8. `resolveBannerLevel` parses env values and defaults to 'hint'.
13
+ */
14
+
15
+ import { beforeEach, describe, expect, it } from "vitest";
16
+
17
+ import {
18
+ _resetShownForTests,
19
+ emitNarrowHint,
20
+ isStrictRefusal,
21
+ resolveBannerLevel,
22
+ shouldShowBanner,
23
+ } from "./narrow-hint-banner.js";
24
+
25
+ function makeSink(): { lines: string[]; write: (s: string) => void } {
26
+ const lines: string[] = [];
27
+ return {
28
+ lines,
29
+ write: (s: string) => {
30
+ lines.push(s);
31
+ },
32
+ };
33
+ }
34
+
35
+ beforeEach(() => {
36
+ _resetShownForTests();
37
+ });
38
+
39
+ describe("emitNarrowHint", () => {
40
+ it("emits a hint message when sync mode is 'all' and not suppressed", () => {
41
+ const sink = makeSink();
42
+ emitNarrowHint(
43
+ { companyUid: "cmp_acme", syncMode: "all", level: "hint" },
44
+ { write: sink.write, showOverride: true },
45
+ );
46
+ expect(sink.lines).toHaveLength(1);
47
+ expect(sink.lines[0]).toMatch(/switch to shared-mode sync/i);
48
+ expect(sink.lines[0]).toMatch(/hq sync narrow --dry-run/);
49
+ });
50
+
51
+ it("skips emission when sync mode is 'shared'", () => {
52
+ const sink = makeSink();
53
+ emitNarrowHint(
54
+ { companyUid: "cmp_acme", syncMode: "shared", level: "hint" },
55
+ { write: sink.write, showOverride: true },
56
+ );
57
+ expect(sink.lines).toHaveLength(0);
58
+ });
59
+
60
+ it("skips emission when sync mode is 'custom'", () => {
61
+ const sink = makeSink();
62
+ emitNarrowHint(
63
+ { companyUid: "cmp_acme", syncMode: "custom", level: "hint" },
64
+ { write: sink.write, showOverride: true },
65
+ );
66
+ expect(sink.lines).toHaveLength(0);
67
+ });
68
+
69
+ it("dedupes per {companyUid, level} across calls", () => {
70
+ const sink = makeSink();
71
+ const input = {
72
+ companyUid: "cmp_acme",
73
+ syncMode: "all",
74
+ level: "hint",
75
+ } as const;
76
+ emitNarrowHint(input, { write: sink.write, showOverride: true });
77
+ emitNarrowHint(input, { write: sink.write, showOverride: true });
78
+ emitNarrowHint(input, { write: sink.write, showOverride: true });
79
+ expect(sink.lines).toHaveLength(1);
80
+ });
81
+
82
+ it("emits separately for different companies", () => {
83
+ const sink = makeSink();
84
+ emitNarrowHint(
85
+ { companyUid: "cmp_acme", syncMode: "all", level: "hint" },
86
+ { write: sink.write, showOverride: true },
87
+ );
88
+ emitNarrowHint(
89
+ { companyUid: "cmp_globex", syncMode: "all", level: "hint" },
90
+ { write: sink.write, showOverride: true },
91
+ );
92
+ expect(sink.lines).toHaveLength(2);
93
+ });
94
+
95
+ it("emits separately for the same company at a different level", () => {
96
+ const sink = makeSink();
97
+ emitNarrowHint(
98
+ { companyUid: "cmp_acme", syncMode: "all", level: "hint" },
99
+ { write: sink.write, showOverride: true },
100
+ );
101
+ emitNarrowHint(
102
+ { companyUid: "cmp_acme", syncMode: "all", level: "warning" },
103
+ { write: sink.write, showOverride: true },
104
+ );
105
+ expect(sink.lines).toHaveLength(2);
106
+ });
107
+
108
+ it("suppresses emission when showOverride=false (suppression path)", () => {
109
+ const sink = makeSink();
110
+ emitNarrowHint(
111
+ { companyUid: "cmp_acme", syncMode: "all", level: "hint" },
112
+ { write: sink.write, showOverride: false },
113
+ );
114
+ expect(sink.lines).toHaveLength(0);
115
+ });
116
+
117
+ it("emits the warning-level message", () => {
118
+ const sink = makeSink();
119
+ emitNarrowHint(
120
+ { companyUid: "cmp_acme", syncMode: "all", level: "warning" },
121
+ { write: sink.write, showOverride: true },
122
+ );
123
+ expect(sink.lines).toHaveLength(1);
124
+ expect(sink.lines[0]).toMatch(/shared-mode sync is the new default/i);
125
+ });
126
+
127
+ it("emits the strict-level message (does NOT throw or exit)", () => {
128
+ const sink = makeSink();
129
+ expect(() =>
130
+ emitNarrowHint(
131
+ { companyUid: "cmp_acme", syncMode: "all", level: "strict" },
132
+ { write: sink.write, showOverride: true },
133
+ ),
134
+ ).not.toThrow();
135
+ expect(sink.lines).toHaveLength(1);
136
+ expect(sink.lines[0]).toMatch(/--mode-all/);
137
+ });
138
+ });
139
+
140
+ describe("shouldShowBanner", () => {
141
+ it("returns true with no overrides", () => {
142
+ expect(shouldShowBanner({ envValue: undefined })).toBe(true);
143
+ });
144
+
145
+ it("returns false when env value is 'off' (case-insensitive)", () => {
146
+ expect(shouldShowBanner({ envValue: "off" })).toBe(false);
147
+ expect(shouldShowBanner({ envValue: "OFF" })).toBe(false);
148
+ });
149
+
150
+ it("returns true for env values other than 'off'", () => {
151
+ expect(shouldShowBanner({ envValue: "on" })).toBe(true);
152
+ expect(shouldShowBanner({ envValue: "" })).toBe(true);
153
+ });
154
+
155
+ it("returns false when .hq/config.json sets syncNarrowHint='off'", () => {
156
+ const exists = (_p: string) => true;
157
+ const read = (_p: string) => JSON.stringify({ syncNarrowHint: "off" });
158
+ expect(
159
+ shouldShowBanner({
160
+ envValue: undefined,
161
+ hqRoot: "/tmp/hq",
162
+ existsFile: exists,
163
+ readFile: read,
164
+ }),
165
+ ).toBe(false);
166
+ });
167
+
168
+ it("returns true when .hq/config.json has no syncNarrowHint key", () => {
169
+ const exists = (_p: string) => true;
170
+ const read = (_p: string) =>
171
+ JSON.stringify({ activeCompany: "acme" });
172
+ expect(
173
+ shouldShowBanner({
174
+ envValue: undefined,
175
+ hqRoot: "/tmp/hq",
176
+ existsFile: exists,
177
+ readFile: read,
178
+ }),
179
+ ).toBe(true);
180
+ });
181
+
182
+ it("tolerates malformed config.json (returns true)", () => {
183
+ const exists = (_p: string) => true;
184
+ const read = (_p: string) => "{not json";
185
+ expect(
186
+ shouldShowBanner({
187
+ envValue: undefined,
188
+ hqRoot: "/tmp/hq",
189
+ existsFile: exists,
190
+ readFile: read,
191
+ }),
192
+ ).toBe(true);
193
+ });
194
+
195
+ it("returns true when config file does not exist", () => {
196
+ expect(
197
+ shouldShowBanner({
198
+ envValue: undefined,
199
+ hqRoot: "/tmp/hq",
200
+ existsFile: () => false,
201
+ readFile: () => {
202
+ throw new Error("should not be called");
203
+ },
204
+ }),
205
+ ).toBe(true);
206
+ });
207
+ });
208
+
209
+ describe("isStrictRefusal", () => {
210
+ it("returns true only for level=strict + syncMode=all", () => {
211
+ expect(isStrictRefusal("all", "strict")).toBe(true);
212
+ expect(isStrictRefusal("shared", "strict")).toBe(false);
213
+ expect(isStrictRefusal("custom", "strict")).toBe(false);
214
+ expect(isStrictRefusal("all", "warning")).toBe(false);
215
+ expect(isStrictRefusal("all", "hint")).toBe(false);
216
+ });
217
+ });
218
+
219
+ describe("resolveBannerLevel", () => {
220
+ it("defaults to 'hint' when env is undefined", () => {
221
+ expect(resolveBannerLevel(undefined)).toBe("hint");
222
+ });
223
+
224
+ it("parses recognized values (case-insensitive)", () => {
225
+ expect(resolveBannerLevel("hint")).toBe("hint");
226
+ expect(resolveBannerLevel("warning")).toBe("warning");
227
+ expect(resolveBannerLevel("strict")).toBe("strict");
228
+ expect(resolveBannerLevel("WARNING")).toBe("warning");
229
+ });
230
+
231
+ it("falls back to 'hint' for unknown values", () => {
232
+ expect(resolveBannerLevel("loud")).toBe("hint");
233
+ expect(resolveBannerLevel("")).toBe("hint");
234
+ });
235
+ });
@@ -0,0 +1,212 @@
1
+ /**
2
+ * `narrow-hint-banner` (US-011) — one-time-per-session hint nudging
3
+ * existing all-mode owners to switch to shared-mode sync.
4
+ *
5
+ * Emitted from `hq sync pull --all` and `hq sync now` after the per-target
6
+ * fanout resolves each membership's sync config. Suppressed when:
7
+ *
8
+ * - the membership is NOT on `syncMode: 'all'` (shared / custom users
9
+ * have already opted in to narrowing, so there is nothing to nudge),
10
+ * - the env var `HQ_SYNC_NARROW_HINT=off` is set,
11
+ * - the per-hqRoot CLI config (`<hqRoot>/.hq/config.json`) has
12
+ * `syncNarrowHint: 'off'`,
13
+ * - or the same `{companyUid, level}` pair has already been shown this
14
+ * process (module-singleton dedupe; the runner imports the same module
15
+ * once per `hq` invocation so a single invocation prints at most one
16
+ * banner per company per level).
17
+ *
18
+ * Three escalating levels — `'hint' | 'warning' | 'strict'`. Today's
19
+ * default level is `'hint'`; this release ships the plumbing so a future
20
+ * hq-core-staging release can flip the default to `'warning'` and then
21
+ * `'strict'` without re-touching the call sites.
22
+ *
23
+ * - hint → dim suggestion to stderr, never blocks.
24
+ * - warning → yellow note to stderr, never blocks.
25
+ * - strict → red error to stderr; callers are expected to refuse to
26
+ * proceed unless the operator passes `--mode-all`. The
27
+ * helper itself never throws or exits — the decision lives at
28
+ * the call site so tests don't have to stub `process.exit`.
29
+ *
30
+ * The level is selected by the caller (typically from the
31
+ * `HQ_SYNC_NARROW_HINT_LEVEL` env var). See `resolveBannerLevel` for the
32
+ * default-and-override ladder.
33
+ *
34
+ * TODO(hq-core-staging release N+2): bump default level to 'warning'.
35
+ * TODO(hq-core-staging release N+3): bump default level to 'strict' and
36
+ * wire `--mode-all` as the only opt-out.
37
+ */
38
+
39
+ import chalk from "chalk";
40
+ import * as fs from "node:fs";
41
+ import * as path from "node:path";
42
+
43
+ export type BannerLevel = "hint" | "warning" | "strict";
44
+
45
+ export interface BannerInput {
46
+ /** Company UID — used to dedupe per-process so each company emits once. */
47
+ companyUid: string;
48
+ /** Effective sync mode resolved from `getMembershipSyncConfig`. */
49
+ syncMode: "shared" | "all" | "custom";
50
+ /** Escalation level — see file header. */
51
+ level: BannerLevel;
52
+ }
53
+
54
+ export interface ShouldShowBannerOpts {
55
+ /**
56
+ * Optional hqRoot — when provided, the helper checks
57
+ * `<hqRoot>/.hq/config.json` for a `syncNarrowHint: 'off'` key in
58
+ * addition to the env override. Falsy paths skip the file check.
59
+ */
60
+ hqRoot?: string;
61
+ /**
62
+ * Test seam: override `process.env` lookup. Defaults to
63
+ * `process.env.HQ_SYNC_NARROW_HINT`.
64
+ */
65
+ envValue?: string | undefined;
66
+ /**
67
+ * Test seam: override `fs.readFileSync`. Defaults to `node:fs`.
68
+ */
69
+ readFile?: (p: string) => string;
70
+ /** Test seam: override `fs.existsSync`. */
71
+ existsFile?: (p: string) => boolean;
72
+ }
73
+
74
+ const SHOWN = new Set<string>();
75
+
76
+ /**
77
+ * Decides whether a banner should be printed AT ALL — independent of
78
+ * dedupe and `syncMode` gating. Exposed for tests and for the strict-mode
79
+ * call site (which needs to know whether to refuse-to-proceed even if the
80
+ * banner itself is suppressed).
81
+ */
82
+ export function shouldShowBanner(opts: ShouldShowBannerOpts = {}): boolean {
83
+ const envValue =
84
+ opts.envValue !== undefined
85
+ ? opts.envValue
86
+ : process.env.HQ_SYNC_NARROW_HINT;
87
+ if (typeof envValue === "string" && envValue.toLowerCase() === "off") {
88
+ return false;
89
+ }
90
+
91
+ if (opts.hqRoot) {
92
+ const configPath = path.join(opts.hqRoot, ".hq", "config.json");
93
+ const exists = opts.existsFile ?? fs.existsSync;
94
+ const read = opts.readFile ?? ((p: string) => fs.readFileSync(p, "utf-8"));
95
+ if (exists(configPath)) {
96
+ try {
97
+ const cfg = JSON.parse(read(configPath)) as {
98
+ syncNarrowHint?: unknown;
99
+ };
100
+ if (
101
+ typeof cfg.syncNarrowHint === "string" &&
102
+ cfg.syncNarrowHint.toLowerCase() === "off"
103
+ ) {
104
+ return false;
105
+ }
106
+ } catch {
107
+ // Malformed config is best-effort — fall through and assume "on".
108
+ }
109
+ }
110
+ }
111
+
112
+ return true;
113
+ }
114
+
115
+ /**
116
+ * Resolve the banner level from environment overrides. Defaults to
117
+ * `'hint'`. Recognized values for `HQ_SYNC_NARROW_HINT_LEVEL`:
118
+ * `hint`, `warning`, `strict` (case-insensitive). Unknown values fall
119
+ * back to the default rather than throwing — the env var is operator-
120
+ * facing and a typo shouldn't break a sync.
121
+ */
122
+ export function resolveBannerLevel(
123
+ envValue: string | undefined = process.env.HQ_SYNC_NARROW_HINT_LEVEL,
124
+ ): BannerLevel {
125
+ if (typeof envValue !== "string") return "hint";
126
+ const v = envValue.toLowerCase();
127
+ if (v === "warning" || v === "strict" || v === "hint") return v;
128
+ return "hint";
129
+ }
130
+
131
+ /**
132
+ * Returns `true` when the strict-mode rollout has been opted into AND
133
+ * the membership in question is still on `'all'`. Call sites should
134
+ * refuse to proceed (exit non-zero) when this returns true and the
135
+ * operator hasn't passed `--mode-all`.
136
+ */
137
+ export function isStrictRefusal(
138
+ syncMode: BannerInput["syncMode"],
139
+ level: BannerLevel,
140
+ ): boolean {
141
+ return level === "strict" && syncMode === "all";
142
+ }
143
+
144
+ /**
145
+ * Emit a one-time-per-{company,level} banner. Writes to `stderr` by
146
+ * default; tests inject a sink. Idempotent — repeated calls with the
147
+ * same `{companyUid, level}` are no-ops for the lifetime of the
148
+ * process.
149
+ */
150
+ export function emitNarrowHint(
151
+ input: BannerInput,
152
+ opts: {
153
+ hqRoot?: string;
154
+ write?: (s: string) => void;
155
+ /** Test seam — overrides `shouldShowBanner` env/config lookups. */
156
+ showOverride?: boolean;
157
+ } = {},
158
+ ): void {
159
+ // Only nudge 'all'-mode memberships — shared/custom callers already
160
+ // opted in to narrowing, so the hint would just be noise.
161
+ if (input.syncMode !== "all") return;
162
+
163
+ const show =
164
+ opts.showOverride !== undefined
165
+ ? opts.showOverride
166
+ : shouldShowBanner({ ...(opts.hqRoot ? { hqRoot: opts.hqRoot } : {}) });
167
+ if (!show) return;
168
+
169
+ const key = `${input.companyUid}:${input.level}`;
170
+ if (SHOWN.has(key)) return;
171
+ SHOWN.add(key);
172
+
173
+ const write =
174
+ opts.write ?? ((s: string) => process.stderr.write(s + "\n"));
175
+
176
+ if (input.level === "hint") {
177
+ write(
178
+ chalk.dim(
179
+ "Tip: switch to shared-mode sync — fewer files, same visibility. " +
180
+ "Run `hq sync narrow --dry-run` to preview.",
181
+ ),
182
+ );
183
+ return;
184
+ }
185
+
186
+ if (input.level === "warning") {
187
+ write(
188
+ chalk.yellow(
189
+ "Warning: shared-mode sync is the new default; this membership " +
190
+ "still pulls everything. Run `hq sync narrow --dry-run` to " +
191
+ "preview the migration before the next release flips the " +
192
+ "default to strict.",
193
+ ),
194
+ );
195
+ return;
196
+ }
197
+
198
+ // strict — caller is responsible for refusing to proceed unless
199
+ // --mode-all was passed. We only emit the message here.
200
+ write(
201
+ chalk.red(
202
+ "Error: shared-mode sync is now strict; pass --mode-all to keep " +
203
+ "all-mode behavior for this run, or run `hq sync narrow --apply` " +
204
+ "to migrate this membership.",
205
+ ),
206
+ );
207
+ }
208
+
209
+ /** Test-only helper — clears the per-process dedupe set. */
210
+ export function _resetShownForTests(): void {
211
+ SHOWN.clear();
212
+ }