linkedin-studio/hooks/scripts/__tests__/session-start-trends-staleness.test.mjs
Kjell Tore Guttormsen 0907b2b92d feat(linkedin-studio): trend-store staleness nudge — SessionStart B-S3 [skip-docs]
SessionStart now warns (>=7d, warn-only) when the persistent trend store's
newest capture is stale, firing ONLY when the store already holds captures
(a never-scanned user is never nagged). Neutral wording — "scan for trends"
hits trend-spotter's own trigger; no hardcoded beat (de-niche invariant).

- store.ts: newestCaptureDate() — pure max-capturedAt staleness signal (SSOT)
- cli.ts: status [--json] subcommand (count + newest + daysStale)
- session-start.mjs: trendsNewestCapture() reads trends.json as raw JSON
  (no tsx spawn at session start) + the reminder line, beside import-staleness
- tests: +3 store tests (newestCaptureDate) + hook subprocess test (3 cases:
  >=7d fires, <7d silent, absent/empty silent + no crash)
- test-runner.sh: trends floor 21->24

Verified: trends 24/24 · all hook tests 131/131 · gate 89/0/0 · real render
confirms "Trend signals are N days old. Scan for trends…".

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RigJBiRFNtFZKCz21qNbQ4
2026-06-23 12:23:53 +02:00

79 lines
3.4 KiB
JavaScript

import { describe, test } from 'node:test';
import assert from 'node:assert/strict';
import { execFileSync } from 'node:child_process';
import { mkdtempSync, rmSync, mkdirSync, writeFileSync, copyFileSync } from 'node:fs';
import { join, dirname } from 'node:path';
import { tmpdir } from 'node:os';
import { fileURLToPath } from 'node:url';
// B-S3: SessionStart emits a warn-only "trend signals are N days old" nudge,
// driven by the trend store's newest capturedAt, and ONLY when the store already
// holds captures. session-start is a procedural hook with no exports, so we run
// it as a subprocess with an isolated HOME + LINKEDIN_STUDIO_DATA and inspect the
// additionalContext. Pattern: session-start-remember.test.mjs subprocess harness.
const here = dirname(fileURLToPath(import.meta.url));
const hookPath = join(here, '..', 'session-start.mjs');
const PLUGIN_ROOT = join(here, '..', '..', '..');
const STATE_TEMPLATE = join(PLUGIN_ROOT, 'config', 'state-file.template.md');
const NUDGE = 'Trend signals are';
const isoDaysAgo = (n) => new Date(Date.now() - n * 86400000).toISOString().slice(0, 10);
// Run the hook against an isolated HOME + data root; optionally seed a trend
// store. Returns the additionalContext string the hook injected at session start.
function runHook({ captures }) {
const home = mkdtempSync(join(tmpdir(), 'lis-ts-home-'));
const data = mkdtempSync(join(tmpdir(), 'lis-ts-data-'));
try {
// The reminders block only runs when the state file already exists; the
// template is the canonical valid state, so copy it into the isolated HOME.
mkdirSync(join(home, '.claude'), { recursive: true });
copyFileSync(STATE_TEMPLATE, join(home, '.claude', 'linkedin-studio.local.md'));
if (captures) {
mkdirSync(join(data, 'trends'), { recursive: true });
writeFileSync(
join(data, 'trends', 'trends.json'),
JSON.stringify({ schemaVersion: 1, trends: captures }),
);
}
const stdout = execFileSync('node', [hookPath], {
input: '',
env: { ...process.env, HOME: home, USERPROFILE: home, LINKEDIN_STUDIO_DATA: data },
encoding: 'utf-8',
});
return JSON.parse(stdout).hookSpecificOutput.additionalContext;
} finally {
rmSync(home, { recursive: true, force: true });
rmSync(data, { recursive: true, force: true });
}
}
const trend = (capturedAt) => ({
id: 't' + capturedAt,
title: 'Some trend ' + capturedAt,
url: 'https://example.com/' + capturedAt,
source: 'tavily',
capturedAt,
topics: ['x'],
});
describe('session-start — trend-store staleness nudge (B-S3)', () => {
test('fires when newest capture is >=7 days old', () => {
const ctx = runHook({ captures: [trend('2026-01-01'), trend(isoDaysAgo(10))] });
assert.ok(ctx.includes(NUDGE), 'expected the trend-staleness nudge for a 10-day-old store');
});
test('does NOT fire when newest capture is fresh (<7 days)', () => {
const ctx = runHook({ captures: [trend(isoDaysAgo(2))] });
assert.ok(!ctx.includes(NUDGE), 'a 2-day-old store must not trigger the nudge');
});
test('does NOT fire (and does not crash) when the store is absent/empty', () => {
const absent = runHook({ captures: null });
assert.ok(!absent.includes(NUDGE), 'no store => no nudge');
const empty = runHook({ captures: [] });
assert.ok(!empty.includes(NUDGE), 'empty store => no nudge (never-scanned user is never nagged)');
});
});