* feat(hooks): opt-in SessionStart update banner for non-statusline users (#2795) When a user declines (or keeps a non-GSD) statusline at install time, the installer now offers an opt-in SessionStart banner that surfaces GSD update availability. The banner reads the existing ~/.cache/gsd/gsd-update-check.json cache (written by gsd-check-update-worker.js) and emits a single systemMessage line only when update_available is true: GSD update available: <installed> → <latest>. Run /gsd-update. It is silent when up-to-date and rate-limits "check failed" diagnostics to once per 24h via a sentinel file so a corrupt cache doesn't nag every session. Removed cleanly by `npx get-shit-done-cc --uninstall` which strips both the script and the SessionStart entry. The banner is never offered when GSD's statusline is being installed (statusline already surfaces update info, so re-prompting would be noise). Implementation: - hooks/gsd-update-banner.js — pure functions buildBannerOutput, shouldSuppressFailureWarning, readCache; thin main() wires them. - bin/install.js — handleUpdateBanner() prompt, parseUpdateBannerInput(), buildUpdateBannerHookEntry(), buildUpdateBannerPromptText(); chained into installAllRuntimes() so finalize() receives both flags. updateBannerCommand computed alongside the other JS-hook commands; finishInstall() registers the SessionStart entry only when shouldInstallBanner === true and the hook file is present at the target. - Hook ships in scripts/build-hooks.js HOOKS_TO_COPY, listed in MANAGED_HOOKS for stale-detection in gsd-check-update-worker.js, in the uninstall hook-removal lists in install.js, and in the rewriteLegacyManagedNodeHookCommands allowlist. Tests: - tests/feat-2795-update-banner.test.cjs — 22 tests, structural-IR assertions on parsed JSON envelopes (no raw-text matching). Covers pure-function branches (cache present/absent, parseError, rate-limit suppression, missing version fields), end-to-end hook invocation against fixture cache states, and install.js wiring (prompt text, input parsing, hook entry shape). - tests/trae-install.test.cjs — updated install() return-shape assertion to include updateBannerCommand: null for the no-settings runtime. - 6881/6881 tests pass. Docs (bundled in same commit per the bundle-docs-with-code skill): - docs/USER-GUIDE.md — new "Surface GSD Update Notifications Without GSD's Statusline" task section with opt-in/opt-out instructions. - docs/FEATURES.md — REQ-HOOK-08 added; "Update Banner" subsection under the Hook System feature with cache flow + removal path. - docs/INVENTORY.md — hook count 11 → 12, new row for gsd-update-banner.js. - docs/INVENTORY-MANIFEST.json — regenerated. Closes #2795 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * fix(install): gate banner prompt on actual installability (CR #3035) CodeRabbit findings on PR #3035: - bin/install.js (Major): continueAfterStatusline gated banner prompt on the raw `shouldInstallStatusline` flag from handleStatusline. But finishInstall later silently skips the statusline write on local installs unless --force-statusline is set (#2248). Two consequences: 1. Interactive local Claude/Gemini installs got neither a statusline nor a banner offer. 2. Codex/Cursor/Copilot/Windsurf/Trae/Cline-only installs (where every result.updateBannerCommand is null) still got prompted even though the choice was silently ignored. Fix: derive willInstallStatusline = shouldInstallStatusline && (isGlobal || forceStatusline), and gate the banner prompt on a canInstallBanner precondition computed from results[].updateBannerCommand. Pass the raw shouldInstallStatusline through to finalize unchanged so per-runtime statusline gating in finishInstall is unaffected. - tests/feat-2795-update-banner.test.cjs (Minor): rate-limit suppression test parsed r1.stdout without first asserting r1.status === 0. Other e2e tests in this file (lines 210, 241) do this. A non-zero exit would surface as a cryptic SyntaxError instead of a status assertion failure. Fix applied verbatim. 6881/6881 tests pass. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
361
tests/feat-2795-update-banner.test.cjs
Normal file
361
tests/feat-2795-update-banner.test.cjs
Normal file
@@ -0,0 +1,361 @@
|
||||
/**
|
||||
* Tests for gsd-update-banner.js (#2795).
|
||||
*
|
||||
* The banner hook is an opt-in SessionStart consumer of the update cache that
|
||||
* gsd-check-update-worker.js writes. When a user declines GSD's statusline,
|
||||
* install.js may register this hook so update availability still surfaces in
|
||||
* runtimes that use a non-GSD statusline.
|
||||
*
|
||||
* Tests follow the typed-IR convention (CONTRIBUTING.md "Prohibited: Raw Text
|
||||
* Matching on Test Outputs"): assert on parsed JSON envelopes, not on raw
|
||||
* stdout substrings.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const { test, describe } = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const os = require('node:os');
|
||||
const path = require('node:path');
|
||||
const { spawnSync } = require('node:child_process');
|
||||
|
||||
const HOOK_PATH = path.join(__dirname, '..', 'hooks', 'gsd-update-banner.js');
|
||||
const {
|
||||
buildBannerOutput,
|
||||
shouldSuppressFailureWarning,
|
||||
RATE_LIMIT_SECONDS,
|
||||
} = require('../hooks/gsd-update-banner.js');
|
||||
|
||||
// ─── Pure function: buildBannerOutput ───────────────────────────────────────
|
||||
|
||||
describe('buildBannerOutput', () => {
|
||||
test('returns null when cache is missing', () => {
|
||||
const out = buildBannerOutput({
|
||||
cache: null,
|
||||
parseError: false,
|
||||
suppressFailureWarning: false,
|
||||
});
|
||||
assert.equal(out, null);
|
||||
});
|
||||
|
||||
test('returns null when update_available is false', () => {
|
||||
const out = buildBannerOutput({
|
||||
cache: { update_available: false, installed: '1.40.0', latest: '1.40.0' },
|
||||
parseError: false,
|
||||
suppressFailureWarning: false,
|
||||
});
|
||||
assert.equal(out, null);
|
||||
});
|
||||
|
||||
test('returns banner envelope when update_available is true', () => {
|
||||
const out = buildBannerOutput({
|
||||
cache: { update_available: true, installed: '1.39.0', latest: '1.40.0' },
|
||||
parseError: false,
|
||||
suppressFailureWarning: false,
|
||||
});
|
||||
assert.ok(out, 'expected banner envelope');
|
||||
assert.equal(typeof out.systemMessage, 'string');
|
||||
assert.ok(
|
||||
out.systemMessage.includes('1.39.0'),
|
||||
'banner should name installed version'
|
||||
);
|
||||
assert.ok(
|
||||
out.systemMessage.includes('1.40.0'),
|
||||
'banner should name latest version'
|
||||
);
|
||||
assert.ok(
|
||||
out.systemMessage.includes('/gsd-update'),
|
||||
'banner should reference /gsd-update command'
|
||||
);
|
||||
});
|
||||
|
||||
test('returns failure diagnostic on parseError when not suppressed', () => {
|
||||
const out = buildBannerOutput({
|
||||
cache: null,
|
||||
parseError: true,
|
||||
suppressFailureWarning: false,
|
||||
});
|
||||
assert.ok(out, 'expected diagnostic envelope');
|
||||
assert.equal(typeof out.systemMessage, 'string');
|
||||
assert.ok(
|
||||
/check failed/i.test(out.systemMessage),
|
||||
'diagnostic should describe a failed check'
|
||||
);
|
||||
});
|
||||
|
||||
test('returns null on parseError when suppressed by rate limit', () => {
|
||||
const out = buildBannerOutput({
|
||||
cache: null,
|
||||
parseError: true,
|
||||
suppressFailureWarning: true,
|
||||
});
|
||||
assert.equal(out, null);
|
||||
});
|
||||
|
||||
test('falls back to "unknown" when installed/latest missing', () => {
|
||||
const out = buildBannerOutput({
|
||||
cache: { update_available: true },
|
||||
parseError: false,
|
||||
suppressFailureWarning: false,
|
||||
});
|
||||
assert.ok(out);
|
||||
assert.ok(
|
||||
out.systemMessage.includes('unknown'),
|
||||
'banner should degrade gracefully when versions are absent'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
// ─── Pure function: shouldSuppressFailureWarning ────────────────────────────
|
||||
|
||||
describe('shouldSuppressFailureWarning', () => {
|
||||
function tmpDir() {
|
||||
return fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-banner-supp-'));
|
||||
}
|
||||
|
||||
test('returns false when sentinel file is missing', () => {
|
||||
const dir = tmpDir();
|
||||
try {
|
||||
const result = shouldSuppressFailureWarning(
|
||||
path.join(dir, 'no-such-file'),
|
||||
100
|
||||
);
|
||||
assert.equal(result, false);
|
||||
} finally {
|
||||
fs.rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test('returns true within rate-limit window', () => {
|
||||
const dir = tmpDir();
|
||||
try {
|
||||
const f = path.join(dir, 'sentinel');
|
||||
fs.writeFileSync(f, '1000');
|
||||
const result = shouldSuppressFailureWarning(f, 1000 + RATE_LIMIT_SECONDS - 1);
|
||||
assert.equal(result, true);
|
||||
} finally {
|
||||
fs.rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test('returns false outside rate-limit window', () => {
|
||||
const dir = tmpDir();
|
||||
try {
|
||||
const f = path.join(dir, 'sentinel');
|
||||
fs.writeFileSync(f, '1000');
|
||||
const result = shouldSuppressFailureWarning(f, 1000 + RATE_LIMIT_SECONDS + 1);
|
||||
assert.equal(result, false);
|
||||
} finally {
|
||||
fs.rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test('returns false when sentinel content is non-numeric', () => {
|
||||
const dir = tmpDir();
|
||||
try {
|
||||
const f = path.join(dir, 'sentinel');
|
||||
fs.writeFileSync(f, 'garbage-not-a-number');
|
||||
const result = shouldSuppressFailureWarning(f, 100);
|
||||
assert.equal(result, false);
|
||||
} finally {
|
||||
fs.rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// ─── End-to-end: spawn the hook against fixture cache states ────────────────
|
||||
|
||||
describe('gsd-update-banner.js end-to-end', () => {
|
||||
function setupHome() {
|
||||
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-banner-home-'));
|
||||
fs.mkdirSync(path.join(home, '.cache', 'gsd'), { recursive: true });
|
||||
return home;
|
||||
}
|
||||
|
||||
function runHook(home) {
|
||||
return spawnSync(process.execPath, [HOOK_PATH], {
|
||||
env: { ...process.env, HOME: home, USERPROFILE: home },
|
||||
encoding: 'utf8',
|
||||
});
|
||||
}
|
||||
|
||||
function writeCache(home, contents) {
|
||||
fs.writeFileSync(
|
||||
path.join(home, '.cache', 'gsd', 'gsd-update-check.json'),
|
||||
typeof contents === 'string' ? contents : JSON.stringify(contents)
|
||||
);
|
||||
}
|
||||
|
||||
test('exits 0 with empty stdout when cache file missing', () => {
|
||||
const home = setupHome();
|
||||
try {
|
||||
const r = runHook(home);
|
||||
assert.equal(r.status, 0, `expected exit 0, got ${r.status} stderr=${r.stderr}`);
|
||||
assert.equal(r.stdout.trim(), '');
|
||||
} finally {
|
||||
fs.rmSync(home, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test('emits valid SessionStart JSON when update_available=true', () => {
|
||||
const home = setupHome();
|
||||
try {
|
||||
writeCache(home, {
|
||||
update_available: true,
|
||||
installed: '1.39.0',
|
||||
latest: '1.40.0',
|
||||
});
|
||||
const r = runHook(home);
|
||||
assert.equal(r.status, 0);
|
||||
const parsed = JSON.parse(r.stdout);
|
||||
assert.equal(typeof parsed.systemMessage, 'string');
|
||||
assert.ok(parsed.systemMessage.includes('1.40.0'));
|
||||
assert.ok(parsed.systemMessage.includes('/gsd-update'));
|
||||
} finally {
|
||||
fs.rmSync(home, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test('exits silent when update_available=false', () => {
|
||||
const home = setupHome();
|
||||
try {
|
||||
writeCache(home, {
|
||||
update_available: false,
|
||||
installed: '1.40.0',
|
||||
latest: '1.40.0',
|
||||
});
|
||||
const r = runHook(home);
|
||||
assert.equal(r.status, 0);
|
||||
assert.equal(r.stdout.trim(), '');
|
||||
} finally {
|
||||
fs.rmSync(home, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test('emits failure diagnostic when cache JSON is malformed', () => {
|
||||
const home = setupHome();
|
||||
try {
|
||||
writeCache(home, 'not json {{{{');
|
||||
const r = runHook(home);
|
||||
assert.equal(r.status, 0);
|
||||
const parsed = JSON.parse(r.stdout);
|
||||
assert.equal(typeof parsed.systemMessage, 'string');
|
||||
assert.ok(/check failed/i.test(parsed.systemMessage));
|
||||
} finally {
|
||||
fs.rmSync(home, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test('suppresses repeat failure diagnostic within 24h via sentinel', () => {
|
||||
const home = setupHome();
|
||||
try {
|
||||
writeCache(home, 'not json');
|
||||
const r1 = runHook(home);
|
||||
assert.equal(
|
||||
r1.status,
|
||||
0,
|
||||
`expected exit 0, got ${r1.status} stderr=${r1.stderr}`
|
||||
);
|
||||
const parsed1 = JSON.parse(r1.stdout);
|
||||
assert.ok(/check failed/i.test(parsed1.systemMessage));
|
||||
|
||||
// Sentinel should now exist so the next run is silent
|
||||
const sentinel = path.join(home, '.cache', 'gsd', 'banner-failure-warned-at');
|
||||
assert.ok(fs.existsSync(sentinel), 'first run must record the warning sentinel');
|
||||
|
||||
const r2 = runHook(home);
|
||||
assert.equal(r2.status, 0);
|
||||
assert.equal(
|
||||
r2.stdout.trim(),
|
||||
'',
|
||||
'subsequent run within rate-limit window must stay silent'
|
||||
);
|
||||
} finally {
|
||||
fs.rmSync(home, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test('handles cache present but update_available field absent (older cache schema)', () => {
|
||||
const home = setupHome();
|
||||
try {
|
||||
writeCache(home, { installed: '1.40.0', latest: '1.40.0' });
|
||||
const r = runHook(home);
|
||||
assert.equal(r.status, 0);
|
||||
assert.equal(r.stdout.trim(), '');
|
||||
} finally {
|
||||
fs.rmSync(home, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// ─── Install.js wiring: prompt + SessionStart entry registration ────────────
|
||||
//
|
||||
// These tests load bin/install.js as a module via GSD_TEST_MODE and assert on
|
||||
// pure exported helpers. The shape mirrors how runtime-prompt-builder /
|
||||
// statusline tests interact with install.js.
|
||||
|
||||
describe('install.js update-banner wiring', () => {
|
||||
process.env.GSD_TEST_MODE = '1';
|
||||
// Re-require fresh so test-mode exports are populated.
|
||||
const installPath = path.join(__dirname, '..', 'bin', 'install.js');
|
||||
delete require.cache[installPath];
|
||||
const installExports = require(installPath);
|
||||
|
||||
test('exports buildUpdateBannerPromptText for structural prompt assertions', () => {
|
||||
assert.equal(
|
||||
typeof installExports.buildUpdateBannerPromptText,
|
||||
'function',
|
||||
'install.js must export buildUpdateBannerPromptText so tests can assert without grepping source'
|
||||
);
|
||||
const text = installExports.buildUpdateBannerPromptText();
|
||||
assert.equal(typeof text, 'string');
|
||||
assert.ok(text.length > 0);
|
||||
// Strip ANSI color escapes before structural assertions — the choice
|
||||
// digits are wrapped in color codes so word-boundary regex against the
|
||||
// raw text would miss them.
|
||||
const stripped = text.replace(/\x1b\[[0-9;]*m/g, '');
|
||||
// Prompt must offer at least two choices (default + opt-in).
|
||||
assert.match(stripped, /\b1\b/);
|
||||
assert.match(stripped, /\b2\b/);
|
||||
});
|
||||
|
||||
test('parseUpdateBannerInput defaults to false on empty / "1"', () => {
|
||||
assert.equal(typeof installExports.parseUpdateBannerInput, 'function');
|
||||
assert.equal(installExports.parseUpdateBannerInput(''), false);
|
||||
assert.equal(installExports.parseUpdateBannerInput(' '), false);
|
||||
assert.equal(installExports.parseUpdateBannerInput('1'), false);
|
||||
});
|
||||
|
||||
test('parseUpdateBannerInput returns true on "2"', () => {
|
||||
assert.equal(installExports.parseUpdateBannerInput('2'), true);
|
||||
assert.equal(installExports.parseUpdateBannerInput('2 '), true);
|
||||
});
|
||||
|
||||
test('parseUpdateBannerInput accepts "y" / "yes" affirmative shortcuts', () => {
|
||||
assert.equal(installExports.parseUpdateBannerInput('y'), true);
|
||||
assert.equal(installExports.parseUpdateBannerInput('Y'), true);
|
||||
assert.equal(installExports.parseUpdateBannerInput('yes'), true);
|
||||
assert.equal(installExports.parseUpdateBannerInput('YES'), true);
|
||||
});
|
||||
|
||||
test('buildUpdateBannerHookEntry produces a SessionStart hook entry', () => {
|
||||
assert.equal(typeof installExports.buildUpdateBannerHookEntry, 'function');
|
||||
const entry = installExports.buildUpdateBannerHookEntry(
|
||||
'"/usr/local/bin/node" "/home/u/.claude/hooks/gsd-update-banner.js"'
|
||||
);
|
||||
assert.ok(entry, 'expected hook entry object');
|
||||
assert.ok(Array.isArray(entry.hooks), 'entry.hooks must be an array');
|
||||
assert.equal(entry.hooks.length, 1);
|
||||
assert.equal(entry.hooks[0].type, 'command');
|
||||
assert.ok(
|
||||
entry.hooks[0].command.includes('gsd-update-banner.js'),
|
||||
'command must reference the banner hook'
|
||||
);
|
||||
});
|
||||
|
||||
test('buildUpdateBannerHookEntry returns null on null command', () => {
|
||||
assert.equal(installExports.buildUpdateBannerHookEntry(null), null);
|
||||
assert.equal(installExports.buildUpdateBannerHookEntry(''), null);
|
||||
});
|
||||
});
|
||||
@@ -182,6 +182,7 @@ describe('Trae local install/uninstall', () => {
|
||||
settingsPath: null,
|
||||
settings: null,
|
||||
statuslineCommand: null,
|
||||
updateBannerCommand: null,
|
||||
runtime: 'trae',
|
||||
configDir: fs.realpathSync(targetDir),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user