Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
test: cover productivity streaks and weeklyDigest assembly directly (#3448)
[._.]/ Adam Eivyfc9c48ced280
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.mdindex 368c1bdc99..47e63d2266 100644--- a/.changelog/NEXT.md+++ b/.changelog/NEXT.md@@ -5,3 +5,5 @@- **Replaced a real Tailscale identity with placeholders across tracked files, and added a guard so it can't come back.** A real MagicDNS tailnet suffix, real device names, and a real CGNAT peer IP had been committed across server routes/lib, client lib, several test fixtures, two design-plan docs, and two released changelog entries — ~14 tracked files in a public repo. This violates the repo's own `CLAUDE.md` "Sensitive Data & Privacy" rule, which lists Tailscale node/MagicDNS names and Tailscale/LAN IPs as never-commit categories. Every occurrence is now an obviously-fake placeholder (`example-tailnet.ts.net`, `host-alpha`/`beta`/`gamma`/`delta`, a documented CGNAT test IP), preserving the two-host distinctness some tests relied on. A new `scripts/tailnet-identity-leak.test.js` enumerates *tracked* files via `git grep` and fails the moment a real-looking tailnet host or CGNAT address lands again — whether via a pasted log line, a doc example lifted from a live install, or a copy-pasted test fixture.These are identifiers, not credentials — Tailscale ACLs still gate all actual network access, and no auth key, token, or secret was ever committed. The historical occurrences remain in git history by deliberate decision (see #3455): a `git filter-repo` rewrite would break every fork and clone of this fork-aware project to remove values that grant no access on their own.++- **The productivity streak store no longer hands back its module-level defaults, and both it and the weekly digest now have real tests.** `server/services/productivity.js` and `server/services/weeklyDigest.js` were referenced in the suite only as `vi.mock()` targets, so ~1,400 lines of streak arithmetic, day/week-boundary handling, and digest assembly were executed by zero tests. They now have direct suites (63 cases) that stub only the file-I/O boundary and the agent-history source and assert concrete values — streak counts across gaps, midnight and year-boundary rollovers, insight copy, week-over-week deltas, and the rendered text summary. Writing them surfaced a real bug: `loadProductivity()` returned the shared `DEFAULT_PRODUCTIVITY` object when no store file existed, so the first incremental `onTaskCompleted()` mutated the module constant in place and every later "empty store" read came back pre-populated. It now clones the defaults per read.diff --git a/server/services/productivity.js b/server/services/productivity.jsindex b67d534cbb..41f723b05e 100644--- a/server/services/productivity.js+++ b/server/services/productivity.js@@ -45,12 +45,17 @@ const DEFAULT_PRODUCTIVITY = {*/export async function loadProductivity() {await ensureDir(DATA_DIR);- const data = await readJSONFile(PRODUCTIVITY_FILE, DEFAULT_PRODUCTIVITY);+ const data = await readJSONFile(PRODUCTIVITY_FILE, null);+ // Clone the defaults on every read: callers (onTaskCompleted) mutate the+ // nested pattern maps in place, so handing back the module-level constant+ // would leak one call's counters into the next "no file yet" read.+ const defaults = structuredClone(DEFAULT_PRODUCTIVITY);+ if (!data) return defaults;// Merge with defaults to ensure all fields existreturn {- ...DEFAULT_PRODUCTIVITY,+ ...defaults,...data,- streaks: { ...DEFAULT_PRODUCTIVITY.streaks, ...data.streaks }+ streaks: { ...defaults.streaks, ...data.streaks }};}diff --git a/server/services/productivity.test.js b/server/services/productivity.test.jsnew file mode 100644index 0000000000..3aacbfa25f--- /dev/null+++ b/server/services/productivity.test.js@@ -0,0 +1,737 @@+/**+ * Direct tests for the productivity service's computation paths (issue #3448).+ *+ * Before this suite the module existed only as a `vi.mock()` target in+ * `routes/cosInsightRoutes.test.js` — referenced by a test, executed by none.+ * Here the real module runs: only the file-I/O boundary is redirected+ * (`PATHS.cos` → a temp dir) and the agent history source (`cosAgents.js`) is+ * stubbed, so the streak arithmetic, the day/week boundary handling, and the+ * insight derivation are all exercised for real.+ *+ * Dates are built from LOCAL components (`new Date(y, m, d, h)`) because the+ * service reads local getters (`getDateString`, `getHours`, `getDay`) — that+ * keeps every expected value stable regardless of the runner's timezone.+ */++import { describe, it, expect, beforeEach, afterEach, afterAll, vi } from 'vitest';+import { EventEmitter } from 'events';+import { mkdtempSync, mkdirSync, rmSync, writeFileSync, existsSync, readFileSync } from 'fs';+import { tmpdir } from 'os';+import { join } from 'path';+import { makePathsProxy } from '../lib/mockPathsDataRoot.js';++const TEST_DATA_ROOT = mkdtempSync(join(tmpdir(), 'productivity-test-'));+const COS_DIR = join(TEST_DATA_ROOT, 'cos');+const PRODUCTIVITY_FILE = join(COS_DIR, 'productivity.json');++// productivity.js anchors its store at PATHS.cos, which is computed+// independently of PATHS.data — redirect both or writes land in the real+// data/cos of the live install.+vi.mock('../lib/fileUtils.js', async (importOriginal) =>+ makePathsProxy(await importOriginal(), {+ dataRoot: TEST_DATA_ROOT,+ extraOverrides: (root) => ({ cos: join(root, 'cos') }),+ }));++const mock = vi.hoisted(() => ({ agents: [] }));++vi.mock('./cosAgents.js', () => ({+ getAgents: vi.fn(async () => mock.agents),+}));++vi.mock('./cosEvents.js', () => ({+ cosEvents: new EventEmitter(),+ emitLog: vi.fn(),+}));++const productivity = await import('./productivity.js');+const { cosEvents } = await import('./cosEvents.js');++afterAll(() => rmSync(TEST_DATA_ROOT, { recursive: true, force: true }));++// Wednesday 2026-03-18 12:00 local. Week of Mon 2026-03-16 (ISO 2026-W12);+// the containing Sunday-start week begins 2026-03-15.+const NOW = new Date(2026, 2, 18, 12, 0, 0);++/** Completed-agent fixture with a local-time completion stamp. */+const agentOn = (id, { month = 3, day, hour = 10, minute = 0, success = true, duration = 60000, status = 'completed', completedAt } = {}) => ({+ id,+ taskId: `task-${id}`,+ status,+ completedAt: completedAt === undefined+ ? new Date(2026, month - 1, day, hour, minute, 0).toISOString()+ : completedAt,+ result: { success, duration },+});++const seed = (data) => {+ mkdirSync(COS_DIR, { recursive: true });+ writeFileSync(PRODUCTIVITY_FILE, JSON.stringify(data));+};++const readStore = () => JSON.parse(readFileSync(PRODUCTIVITY_FILE, 'utf-8'));++beforeEach(() => {+ vi.useFakeTimers({ toFake: ['Date'], now: NOW });+ mock.agents = [];+ rmSync(TEST_DATA_ROOT, { recursive: true, force: true });+ mkdirSync(COS_DIR, { recursive: true });+});++afterEach(() => {+ vi.useRealTimers();+ vi.clearAllMocks();+});++describe('recalculateProductivity — streaks', () => {+ it('returns zeroed streaks and totals for an empty history', async () => {+ const result = await productivity.recalculateProductivity();++ expect(result.streaks).toMatchObject({+ currentDaily: 0,+ longestDaily: 0,+ currentWeekly: 0,+ longestWeekly: 0,+ lastActiveDate: null,+ lastActiveWeek: null,+ });+ expect(result.totals).toEqual({+ totalTasks: 0,+ successfulTasks: 0,+ successRate: 0,+ activeDays: 0,+ activeWeeks: 0,+ });+ expect(result.dailyHistory).toEqual({});+ expect(result.milestones).toEqual([]);+ });++ it('counts a single active day as a 1-day streak', async () => {+ mock.agents = [agentOn('a1', { day: 18 })];++ const { streaks, totals } = await productivity.recalculateProductivity();++ expect(streaks.currentDaily).toBe(1);+ expect(streaks.longestDaily).toBe(1);+ expect(streaks.lastActiveDate).toBe('2026-03-18');+ expect(streaks.lastActiveWeek).toBe('2026-W12');+ expect(totals.activeDays).toBe(1);+ expect(totals.activeWeeks).toBe(1);+ });++ it('counts three consecutive days ending today as a 3-day streak', async () => {+ mock.agents = [+ agentOn('a1', { day: 16 }),+ agentOn('a2', { day: 17 }),+ agentOn('a3', { day: 18 }),+ ];++ const { streaks } = await productivity.recalculateProductivity();++ expect(streaks.currentDaily).toBe(3);+ expect(streaks.longestDaily).toBe(3);+ });++ it('breaks the current streak at a gap but keeps the longest run', async () => {+ mock.agents = [+ agentOn('a1', { day: 10 }),+ agentOn('a2', { day: 11 }),+ agentOn('a3', { day: 18 }),+ ];++ const { streaks, totals } = await productivity.recalculateProductivity();++ expect(streaks.longestDaily).toBe(2);+ expect(streaks.currentDaily).toBe(1);+ expect(totals.activeDays).toBe(3);+ });++ it('keeps the current streak alive when the last activity was yesterday', async () => {+ mock.agents = [agentOn('a1', { day: 16 }), agentOn('a2', { day: 17 })];++ const { streaks } = await productivity.recalculateProductivity();++ expect(streaks.currentDaily).toBe(2);+ expect(streaks.lastActiveDate).toBe('2026-03-17');+ });++ it('zeroes the current streak once the gap exceeds one day', async () => {+ mock.agents = [agentOn('a1', { day: 15 })];++ const { streaks } = await productivity.recalculateProductivity();++ expect(streaks.currentDaily).toBe(0);+ expect(streaks.longestDaily).toBe(1);+ expect(streaks.lastActiveDate).toBe('2026-03-15');+ });++ it('splits activity across the local midnight boundary into two streak days', async () => {+ mock.agents = [+ agentOn('late', { day: 17, hour: 23, minute: 30 }),+ agentOn('early', { day: 18, hour: 0, minute: 15 }),+ ];++ const { streaks, dailyHistory, hourlyPatterns } = await productivity.recalculateProductivity();++ expect(Object.keys(dailyHistory).sort()).toEqual(['2026-03-17', '2026-03-18']);+ expect(hourlyPatterns[23].tasks).toBe(1);+ expect(hourlyPatterns[0].tasks).toBe(1);+ expect(streaks.currentDaily).toBe(2);+ expect(streaks.longestDaily).toBe(2);+ });++ it('ignores agents that are not completed or carry no completion stamp', async () => {+ mock.agents = [+ agentOn('done', { day: 18 }),+ agentOn('running', { day: 18, status: 'running' }),+ agentOn('no-stamp', { day: 18, completedAt: null }),+ ];++ const { totals, dailyHistory } = await productivity.recalculateProductivity();++ expect(totals.totalTasks).toBe(1);+ expect(dailyHistory['2026-03-18'].tasks).toBe(1);+ });+});++describe('recalculateProductivity — aggregates and milestones', () => {+ it('computes exact hourly, day-of-week and per-date aggregates', async () => {+ mock.agents = [+ agentOn('a1', { day: 18, hour: 9, success: true, duration: 1000 }),+ agentOn('a2', { day: 18, hour: 9, success: true, duration: 2000 }),+ agentOn('a3', { day: 18, hour: 9, success: false, duration: 3000 }),+ ];++ const result = await productivity.recalculateProductivity();++ expect(result.hourlyPatterns[9]).toEqual({+ tasks: 3,+ successes: 2,+ failures: 1,+ totalDuration: 6000,+ avgDuration: 2000,+ successRate: 67,+ });+ // 2026-03-18 is a Wednesday → day-of-week index 3.+ expect(result.dailyPatterns[3]).toMatchObject({ tasks: 3, successes: 2, failures: 1, avgDuration: 2000 });+ expect(result.dailyHistory['2026-03-18']).toEqual({+ tasks: 3,+ successes: 2,+ failures: 1,+ successRate: 67,+ });+ expect(result.totals).toMatchObject({ totalTasks: 3, successfulTasks: 2, successRate: 67 });+ });++ it('awards the 10-task and 3-day-streak milestones once earned', async () => {+ mock.agents = [+ ...Array.from({ length: 4 }, (_, i) => agentOn(`d16-${i}`, { day: 16 })),+ ...Array.from({ length: 3 }, (_, i) => agentOn(`d17-${i}`, { day: 17 })),+ ...Array.from({ length: 3 }, (_, i) => agentOn(`d18-${i}`, { day: 18 })),+ ];++ const { milestones } = await productivity.recalculateProductivity();++ expect(milestones).toContainEqual({+ type: 'tasks',+ value: 10,+ achievedAt: expect.any(String),+ description: 'Completed 10 tasks',+ });+ expect(milestones).toContainEqual({ type: 'streak', value: 3, description: '3-day work streak' });+ // 25-task and 7-day milestones are not reached yet.+ expect(milestones.filter(m => m.value === 25)).toEqual([]);+ expect(milestones.filter(m => m.type === 'streak' && m.value === 7)).toEqual([]);+ });++ it('persists the recalculated snapshot with a lastUpdated stamp', async () => {+ mock.agents = [agentOn('a1', { day: 18 })];++ await productivity.recalculateProductivity();++ const stored = readStore();+ expect(stored.streaks.currentDaily).toBe(1);+ expect(stored.lastUpdated).toBe(NOW.toISOString());+ });+});++describe('onTaskCompleted — incremental streak updates', () => {+ it('starts a 1-day streak from an empty store and emits the update event', async () => {+ const emitted = [];+ const listener = () => emitted.push('productivity:updated');+ cosEvents.on('productivity:updated', listener);++ await productivity.onTaskCompleted(agentOn('a1', { day: 18, hour: 9, duration: 4000 }));++ const stored = readStore();+ expect(stored.streaks).toMatchObject({+ currentDaily: 1,+ longestDaily: 1,+ currentWeekly: 1,+ longestWeekly: 1,+ lastActiveDate: '2026-03-18',+ lastActiveWeek: '2026-W12',+ });+ expect(stored.hourlyPatterns[9]).toMatchObject({ tasks: 1, successes: 1, avgDuration: 4000, successRate: 100 });+ expect(stored.dailyHistory['2026-03-18']).toEqual({ tasks: 1, successes: 1, failures: 0, successRate: 100 });+ expect(emitted).toEqual(['productivity:updated']);++ cosEvents.off('productivity:updated', listener);+ });++ it('extends the streak on a consecutive day and preserves the longest run', async () => {+ seed({+ streaks: { currentDaily: 2, longestDaily: 5, currentWeekly: 1, longestWeekly: 3, lastActiveDate: '2026-03-17', lastActiveWeek: '2026-W12' },+ hourlyPatterns: {}, dailyPatterns: {}, dailyHistory: {}, milestones: [],+ });++ await productivity.onTaskCompleted(agentOn('a1', { day: 18 }));++ expect(readStore().streaks).toMatchObject({ currentDaily: 3, longestDaily: 5, lastActiveDate: '2026-03-18' });+ });++ it('resets the current streak to 1 after a gap', async () => {+ seed({+ streaks: { currentDaily: 4, longestDaily: 5, currentWeekly: 1, longestWeekly: 3, lastActiveDate: '2026-03-10', lastActiveWeek: '2026-W11' },+ hourlyPatterns: {}, dailyPatterns: {}, dailyHistory: {}, milestones: [],+ });++ await productivity.onTaskCompleted(agentOn('a1', { day: 18 }));++ expect(readStore().streaks).toMatchObject({ currentDaily: 1, longestDaily: 5 });+ });++ it('leaves the streak untouched for a second task on the same day', async () => {+ seed({+ streaks: { currentDaily: 4, longestDaily: 5, currentWeekly: 2, longestWeekly: 3, lastActiveDate: '2026-03-18', lastActiveWeek: '2026-W12' },+ hourlyPatterns: {}, dailyPatterns: {},+ dailyHistory: { '2026-03-18': { tasks: 1, successes: 1, failures: 0, successRate: 100 } },+ milestones: [],+ });++ await productivity.onTaskCompleted(agentOn('a1', { day: 18, success: false }));++ const stored = readStore();+ expect(stored.streaks).toMatchObject({ currentDaily: 4, longestDaily: 5, currentWeekly: 2 });+ expect(stored.dailyHistory['2026-03-18']).toEqual({ tasks: 2, successes: 1, failures: 1, successRate: 50 });+ });++ it('rolls the weekly streak across a year boundary (W52 → W01)', async () => {+ seed({+ streaks: { currentDaily: 1, longestDaily: 4, currentWeekly: 3, longestWeekly: 3, lastActiveDate: '2025-12-26', lastActiveWeek: '2025-W52' },+ hourlyPatterns: {}, dailyPatterns: {}, dailyHistory: {}, milestones: [],+ });++ await productivity.onTaskCompleted(agentOn('a1', { month: 1, day: 1 }));++ expect(readStore().streaks).toMatchObject({+ currentWeekly: 4,+ longestWeekly: 4,+ lastActiveWeek: '2026-W01',+ });+ });++ it('prunes daily history older than 90 days', async () => {+ seed({+ streaks: { currentDaily: 0, longestDaily: 0, currentWeekly: 0, longestWeekly: 0, lastActiveDate: null, lastActiveWeek: null },+ hourlyPatterns: {}, dailyPatterns: {},+ dailyHistory: {+ '2025-06-01': { tasks: 3, successes: 3, failures: 0, successRate: 100 },+ '2026-03-01': { tasks: 1, successes: 1, failures: 0, successRate: 100 },+ },+ milestones: [],+ });++ await productivity.onTaskCompleted(agentOn('a1', { day: 18 }));++ expect(Object.keys(readStore().dailyHistory).sort()).toEqual(['2026-03-01', '2026-03-18']);+ });++ it('is a no-op for an agent with no completion stamp', async () => {+ await productivity.onTaskCompleted({ id: 'a1', result: { success: true } });++ expect(existsSync(PRODUCTIVITY_FILE)).toBe(false);+ });++ it('does not leak one incremental update into the next empty-store read', async () => {+ await productivity.onTaskCompleted(agentOn('a1', { day: 18, hour: 9 }));+ rmSync(PRODUCTIVITY_FILE, { force: true });++ const fresh = await productivity.loadProductivity();++ expect(fresh.hourlyPatterns).toEqual({});+ expect(fresh.dailyHistory).toEqual({});+ expect(fresh.streaks.currentDaily).toBe(0);+ });+});++describe('getProductivityInsights', () => {+ const insightsFixture = {+ streaks: { currentDaily: 5, longestDaily: 9, currentWeekly: 4, longestWeekly: 4, lastActiveDate: '2026-03-18', lastActiveWeek: '2026-W12' },+ hourlyPatterns: {+ 9: { tasks: 10, successes: 8, failures: 2, totalDuration: 10000, avgDuration: 1000, successRate: 80 },+ 13: { tasks: 6, successes: 3, failures: 3, totalDuration: 6000, avgDuration: 1000, successRate: 50 },+ // Below the 5-task reliability floor — must be filtered out entirely.+ 2: { tasks: 4, successes: 4, failures: 0, totalDuration: 4000, avgDuration: 1000, successRate: 100 },+ },+ dailyPatterns: {+ 3: { tasks: 8, successes: 7, failures: 1, totalDuration: 8000, avgDuration: 1000, successRate: 88 },+ 5: { tasks: 4, successes: 1, failures: 3, totalDuration: 4000, avgDuration: 1000, successRate: 25 },+ // Below the 3-task floor for days.+ 0: { tasks: 2, successes: 2, failures: 0, totalDuration: 2000, avgDuration: 1000, successRate: 100 },+ },+ dailyHistory: {},+ milestones: [],+ };++ it('derives peak-hour, best-day, streak and weekly insights with concrete copy', async () => {+ seed(insightsFixture);++ const result = await productivity.getProductivityInsights();++ expect(result.bestHour).toMatchObject({ hour: 9, successRate: 80 });+ expect(result.worstHour).toMatchObject({ hour: 13, successRate: 50 });+ expect(result.bestDay).toMatchObject({ day: 3, dayName: 'Wednesday', successRate: 88 });+ expect(result.worstDay).toMatchObject({ day: 5, dayName: 'Friday', successRate: 25 });++ expect(result.insights).toEqual([+ {+ type: 'optimization',+ title: 'Peak Performance Hour',+ message: 'Tasks completed around 9AM have a 80% success rate',+ icon: 'clock',+ },+ {+ type: 'info',+ title: 'Most Productive Day',+ message: 'Wednesdays show 88% success rate with 8 tasks completed',+ icon: 'calendar',+ },+ {+ type: 'success',+ title: '🔥 Hot Streak!',+ message: '5 days of continuous productivity! Keep it up!',+ icon: 'flame',+ },+ {+ type: 'success',+ title: 'Weekly Warrior',+ message: '4 consecutive weeks of activity!',+ icon: 'trophy',+ },+ ]);+ });++ it('labels midnight and afternoon peak hours in 12-hour form', async () => {+ seed({+ ...insightsFixture,+ hourlyPatterns: { 0: { tasks: 5, successes: 5, failures: 0, avgDuration: 100, successRate: 100 } },+ });+ expect((await productivity.getProductivityInsights()).insights[0].message)+ .toBe('Tasks completed around 12AM have a 100% success rate');++ seed({+ ...insightsFixture,+ hourlyPatterns: { 15: { tasks: 5, successes: 3, failures: 2, avgDuration: 100, successRate: 60 } },+ });+ expect((await productivity.getProductivityInsights()).insights[0].message)+ .toBe('Tasks completed around 3PM have a 60% success rate');+ });++ it('flags a broken streak against the previous best', async () => {+ seed({+ ...insightsFixture,+ hourlyPatterns: {},+ dailyPatterns: {},+ streaks: { currentDaily: 0, longestDaily: 7, currentWeekly: 1, longestWeekly: 4, lastActiveDate: '2026-03-10', lastActiveWeek: '2026-W11' },+ });++ const { insights } = await productivity.getProductivityInsights();++ expect(insights).toEqual([{+ type: 'warning',+ title: 'Streak Broken',+ message: 'Your best was 7 days. Start a new streak today!',+ icon: 'refresh',+ }]);+ });++ it('returns no insights and null extremes for a store with no history', async () => {+ const result = await productivity.getProductivityInsights();++ expect(result.insights).toEqual([]);+ expect(result.bestHour).toBeNull();+ expect(result.worstHour).toBeNull();+ expect(result.bestDay).toBeNull();+ expect(result.worstDay).toBeNull();+ });+});++describe('getProductivitySummary', () => {+ it('projects the dashboard fields from the stored snapshot', async () => {+ seed({+ streaks: { currentDaily: 3, longestDaily: 9, currentWeekly: 2, longestWeekly: 4, lastActiveDate: '2026-03-18', lastActiveWeek: '2026-W12' },+ hourlyPatterns: {}, dailyPatterns: {}, dailyHistory: {},+ totals: { totalTasks: 20, successfulTasks: 18, successRate: 90, activeDays: 11, activeWeeks: 3 },+ milestones: [+ { type: 'tasks', value: 10, description: 'Completed 10 tasks' },+ { type: 'streak', value: 3, description: '3-day work streak' },+ ],+ });++ expect(await productivity.getProductivitySummary()).toEqual({+ currentStreak: 3,+ longestStreak: 9,+ weeklyStreak: 2,+ lastActive: '2026-03-18',+ totalDays: 11,+ recentMilestone: { type: 'streak', value: 3, description: '3-day work streak' },+ });+ });++ it('falls back to zeros and nulls for an empty store', async () => {+ expect(await productivity.getProductivitySummary()).toEqual({+ currentStreak: 0,+ longestStreak: 0,+ weeklyStreak: 0,+ lastActive: null,+ totalDays: 0,+ recentMilestone: null,+ });+ });+});++describe('getWeekComparison', () => {+ const withHistory = (dailyHistory) => seed({+ streaks: {}, hourlyPatterns: {}, dailyPatterns: {}, dailyHistory, milestones: [],+ });++ it('compares matching day ranges of this week and last week', async () => {+ // Today is Wednesday 2026-03-18 → this week starts Sunday 2026-03-15 and+ // last week's comparable range is Sunday 2026-03-08 through Wed 2026-03-11.+ withHistory({+ '2026-03-16': { tasks: 4, successes: 4, failures: 0, successRate: 100 },+ '2026-03-18': { tasks: 2, successes: 1, failures: 1, successRate: 50 },+ '2026-03-09': { tasks: 3, successes: 3, failures: 0, successRate: 100 },+ // Outside last week's comparable range (Thursday) — must not be counted.+ '2026-03-12': { tasks: 9, successes: 9, failures: 0, successRate: 100 },+ });++ expect(await productivity.getWeekComparison()).toEqual({+ thisWeek: { tasks: 6, successes: 5, successRate: 83 },+ lastWeek: { tasks: 3, successes: 3, successRate: 100 },+ changePercent: 100,+ trend: 'up',+ daysCompared: 4,+ });+ });++ it('reports a decline when this week trails last week', async () => {+ withHistory({+ '2026-03-16': { tasks: 2, successes: 2, failures: 0, successRate: 100 },+ '2026-03-09': { tasks: 10, successes: 5, failures: 5, successRate: 50 },+ });++ const result = await productivity.getWeekComparison();++ expect(result.changePercent).toBe(-80);+ expect(result.trend).toBe('down');+ });++ it('treats a first active week as a 100% gain and a fully idle pair as neutral', async () => {+ withHistory({ '2026-03-16': { tasks: 5, successes: 5, failures: 0, successRate: 100 } });+ expect(await productivity.getWeekComparison()).toMatchObject({ changePercent: 100, trend: 'up' });++ withHistory({});+ expect(await productivity.getWeekComparison()).toMatchObject({ changePercent: null, trend: 'neutral' });+ });+});++describe('getVelocityMetrics', () => {+ it('scores today against the historical active-day average', async () => {+ seed({+ streaks: {}, hourlyPatterns: {}, dailyPatterns: {}, milestones: [],+ dailyHistory: {+ '2026-03-18': { tasks: 6, successes: 5, failures: 1, successRate: 83 },+ '2026-03-17': { tasks: 4, successes: 4, failures: 0, successRate: 100 },+ '2026-03-16': { tasks: 2, successes: 1, failures: 1, successRate: 50 },+ // Zero-task days are excluded from the average.+ '2026-03-15': { tasks: 0, successes: 0, failures: 0, successRate: 0 },+ },+ });++ expect(await productivity.getVelocityMetrics()).toEqual({+ today: 6,+ todaySuccesses: 5,+ todayFailures: 1,+ avgPerDay: 3,+ historicalDays: 2,+ velocity: 200,+ velocityLabel: 'exceptional',+ });+ });++ it('labels the first ever active day', async () => {+ seed({+ streaks: {}, hourlyPatterns: {}, dailyPatterns: {}, milestones: [],+ dailyHistory: { '2026-03-18': { tasks: 2, successes: 2, failures: 0, successRate: 100 } },+ });++ expect(await productivity.getVelocityMetrics()).toMatchObject({ velocity: 100, velocityLabel: 'first-day' });+ });++ it('leaves velocity null when nothing has been completed today', async () => {+ seed({+ streaks: {}, hourlyPatterns: {}, dailyPatterns: {}, milestones: [],+ dailyHistory: { '2026-03-17': { tasks: 4, successes: 4, failures: 0, successRate: 100 } },+ });++ expect(await productivity.getVelocityMetrics()).toMatchObject({+ today: 0,+ avgPerDay: 4,+ historicalDays: 1,+ velocity: null,+ velocityLabel: null,+ });+ });+});++describe('getOptimalTimeInfo', () => {+ it('needs at least three reliable hours before making a recommendation', async () => {+ seed({+ streaks: {}, dailyPatterns: {}, dailyHistory: {}, milestones: [],+ hourlyPatterns: {+ 9: { tasks: 5, successes: 5, failures: 0, successRate: 100 },+ 10: { tasks: 4, successes: 2, failures: 2, successRate: 50 },+ 11: { tasks: 2, successes: 2, failures: 0, successRate: 100 },+ },+ });++ expect(await productivity.getOptimalTimeInfo()).toEqual({ hasData: false });+ });++ it('ranks the top quartile and points at the next optimal hour', async () => {+ seed({+ streaks: {}, dailyPatterns: {}, dailyHistory: {}, milestones: [],+ hourlyPatterns: {+ 9: { tasks: 6, successes: 6, failures: 0, successRate: 100 },+ 10: { tasks: 5, successes: 4, failures: 1, successRate: 80 },+ 14: { tasks: 5, successes: 3, failures: 2, successRate: 60 },+ 20: { tasks: 5, successes: 1, failures: 4, successRate: 20 },+ },+ });++ // Fake clock sits at 12:00, an hour with no recorded history.+ expect(await productivity.getOptimalTimeInfo()).toEqual({+ hasData: true,+ currentHour: 12,+ currentSuccessRate: null,+ currentTasks: 0,+ isOptimal: false,+ isAboveAverage: false,+ topHours: [9],+ nextOptimalHour: 9,+ nextOptimalFormatted: '9AM',+ avgSuccessRate: 65,+ peakSuccessRate: 100,+ });+ });++ it('marks the current hour optimal when it tops the ranking', async () => {+ seed({+ streaks: {}, dailyPatterns: {}, dailyHistory: {}, milestones: [],+ hourlyPatterns: {+ 12: { tasks: 8, successes: 8, failures: 0, successRate: 100 },+ 10: { tasks: 5, successes: 4, failures: 1, successRate: 80 },+ 14: { tasks: 5, successes: 3, failures: 2, successRate: 60 },+ },+ });++ expect(await productivity.getOptimalTimeInfo()).toMatchObject({+ isOptimal: true,+ isAboveAverage: true,+ currentSuccessRate: 100,+ currentTasks: 8,+ nextOptimalHour: null,+ nextOptimalFormatted: null,+ avgSuccessRate: 80,+ });+ });+});++describe('getDailyTrends', () => {+ it('fills missing days with zeros and reports rolling averages over the window', async () => {+ seed({+ streaks: {}, hourlyPatterns: {}, dailyPatterns: {}, milestones: [],+ dailyHistory: {+ '2026-03-18': { tasks: 4, successes: 4, failures: 0, successRate: 100 },+ '2026-03-16': { tasks: 2, successes: 1, failures: 1, successRate: 50 },+ },+ });++ const { data, summary } = await productivity.getDailyTrends(7);++ expect(data).toHaveLength(7);+ expect(data[0].date).toBe('2026-03-12');+ expect(data[6]).toMatchObject({ date: '2026-03-18', dateShort: '03-18', tasks: 4, rollingAvgTasks: 0.9 });+ expect(data[3]).toMatchObject({ date: '2026-03-15', tasks: 0, successRate: 0 });+ expect(summary).toMatchObject({+ days: 7,+ activeDays: 2,+ totalTasks: 6,+ avgTasksPerActiveDay: 3,+ avgSuccessRate: 75,+ });+ });++ it('calls out a falling volume trend against the prior week', async () => {+ const dailyHistory = {};+ // Older week (2026-03-05 … 2026-03-11): 5 tasks a day. Recent week: 1 a day.+ for (let day = 5; day <= 11; day++) {+ dailyHistory[`2026-03-${String(day).padStart(2, '0')}`] = { tasks: 5, successes: 5, failures: 0, successRate: 100 };+ }+ for (let day = 12; day <= 18; day++) {+ dailyHistory[`2026-03-${String(day).padStart(2, '0')}`] = { tasks: 1, successes: 0, failures: 1, successRate: 0 };+ }+ seed({ streaks: {}, hourlyPatterns: {}, dailyPatterns: {}, milestones: [], dailyHistory });++ const { summary } = await productivity.getDailyTrends(30);++ expect(summary.volumeTrend).toBe('decreasing');+ expect(summary.successTrend).toBe('declining');+ });+});++describe('getActivityCalendar', () => {+ it('builds Sunday-aligned weeks through the end of the current week', async () => {+ seed({+ streaks: { currentDaily: 3 }, hourlyPatterns: {}, dailyPatterns: {}, milestones: [],+ dailyHistory: {+ '2026-03-18': { tasks: 7, successes: 6, failures: 1, successRate: 86 },+ '2026-03-10': { tasks: 3, successes: 3, failures: 0, successRate: 100 },+ },+ });++ const calendar = await productivity.getActivityCalendar(2);++ // 2026-03-01 (Sunday) through 2026-03-21 (Saturday) → three full weeks.+ expect(calendar.weeks).toHaveLength(3);+ expect(calendar.weeks.every(week => week.length === 7)).toBe(true);+ expect(calendar.weeks[0][0].date).toBe('2026-03-01');+ expect(calendar.weeks[2][6]).toMatchObject({ date: '2026-03-21', isFuture: true, tasks: 0 });++ const today = calendar.weeks.flat().find(d => d.isToday);+ expect(today).toMatchObject({ date: '2026-03-18', tasks: 7, successRate: 86 });++ expect(calendar.maxTasks).toBe(7);+ expect(calendar.currentStreak).toBe(3);+ expect(calendar.summary).toEqual({+ totalDays: 21,+ activeDays: 2,+ totalTasks: 10,+ totalSuccesses: 9,+ successRate: 90,+ avgTasksPerActiveDay: 5,+ });+ });+});diff --git a/server/services/weeklyDigest.test.js b/server/services/weeklyDigest.test.jsnew file mode 100644index 0000000000..0369e5bb44--- /dev/null+++ b/server/services/weeklyDigest.test.js@@ -0,0 +1,525 @@+/**+ * Direct tests for the weekly digest assembly (issue #3448).+ *+ * The module previously existed in the suite only as a `vi.mock()` target in+ * `routes/cosLearningRoutes.test.js`, so none of the aggregation, the+ * week-over-week math, or the insight copy was ever executed. Here the real+ * module runs against a temp digests dir (the only file-I/O boundary) with the+ * agent history source (`cosAgents.js`) stubbed.+ *+ * Fixture timestamps are built from LOCAL date components so every expectation+ * holds regardless of the runner's timezone — the service reads local getters.+ */++import { describe, it, expect, beforeEach, afterEach, afterAll, vi } from 'vitest';+import { EventEmitter } from 'events';+import { mkdtempSync, mkdirSync, rmSync, writeFileSync, existsSync, readFileSync } from 'fs';+import { tmpdir } from 'os';+import { join } from 'path';+import { makePathsProxy } from '../lib/mockPathsDataRoot.js';++const TEST_DATA_ROOT = mkdtempSync(join(tmpdir(), 'weekly-digest-test-'));+const DIGESTS_DIR = join(TEST_DATA_ROOT, 'cos', 'digests');++// weeklyDigest.js anchors its store at PATHS.digests, computed independently of+// PATHS.data — redirect it or digests land in the real install's data/cos.+vi.mock('../lib/fileUtils.js', async (importOriginal) =>+ makePathsProxy(await importOriginal(), {+ dataRoot: TEST_DATA_ROOT,+ extraOverrides: (root) => ({ digests: join(root, 'cos', 'digests') }),+ }));++const mock = vi.hoisted(() => ({+ agents: [], // getAgents() — the live state index+ agentsByDate: {}, // getAgentsByDate(date)+}));++vi.mock('./cosAgents.js', () => ({+ getAgents: vi.fn(async () => mock.agents),+ getAgentDates: vi.fn(async () => Object.keys(mock.agentsByDate).map(date => ({ date }))),+ getAgentsByDate: vi.fn(async (date) => mock.agentsByDate[date] || []),+}));++vi.mock('./cosEvents.js', () => ({+ cosEvents: new EventEmitter(),+ emitLog: vi.fn(),+}));++const digestService = await import('./weeklyDigest.js');+const { cosEvents } = await import('./cosEvents.js');++afterAll(() => rmSync(TEST_DATA_ROOT, { recursive: true, force: true }));++// Wednesday 2026-03-18 12:00 local → ISO week 2026-W12, week start Mon 2026-03-16.+const NOW = new Date(2026, 2, 18, 12, 0, 0);+const THIS_WEEK = '2026-W12';+const LAST_WEEK = '2026-W11';++const at = (day, hour = 10, month = 3) => new Date(2026, month - 1, day, hour, 0, 0).toISOString();++/** Completed-agent fixture. `taskType` lands in metadata.analysisType. */+const agent = (id, {+ day = 18,+ hour = 10,+ month = 3,+ success = true,+ duration = 60000,+ taskType = null,+ description = null,+ error = null,+ status = 'completed',+ completedAt,+} = {}) => ({+ id,+ taskId: `task-${id}`,+ status,+ startedAt: at(day, hour - 1, month),+ completedAt: completedAt === undefined ? at(day, hour, month) : completedAt,+ result: { success, duration, ...(error ? { error } : {}) },+ metadata: { ...(taskType ? { analysisType: taskType } : {}), ...(description ? { taskDescription: description } : {}) },+});++/** Register agents on the date index keyed by their local completion date. */+const onDates = (agents) => {+ mock.agentsByDate = {};+ for (const a of agents) {+ const d = new Date(a.completedAt);+ const key = `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;+ (mock.agentsByDate[key] ||= []).push(a);+ }+};++const writeDigest = (weekId, digest) => {+ mkdirSync(DIGESTS_DIR, { recursive: true });+ writeFileSync(join(DIGESTS_DIR, `${weekId}.json`), JSON.stringify({ weekId, ...digest }));+};++const readDigest = (weekId) => JSON.parse(readFileSync(join(DIGESTS_DIR, `${weekId}.json`), 'utf-8'));++/** Minimal stored-digest shape the week-over-week comparison reads. */+const storedDigest = (weekId, { totalTasks, successRate, totalWorkTimeMs }) => ({+ weekId,+ weekStart: at(9),+ weekEnd: at(15),+ generatedAt: at(15),+ summary: { totalTasks, succeededTasks: totalTasks, failedTasks: 0, successRate, totalWorkTimeMs, totalWorkTime: '1m' },+});++beforeEach(() => {+ vi.useFakeTimers({ toFake: ['Date'], now: NOW });+ mock.agents = [];+ mock.agentsByDate = {};+ rmSync(TEST_DATA_ROOT, { recursive: true, force: true });+ mkdirSync(DIGESTS_DIR, { recursive: true });+});++afterEach(() => {+ vi.useRealTimers();+ vi.clearAllMocks();+});++describe('generateWeeklyDigest — summary assembly', () => {+ it('aggregates only the target week and reports exact totals', async () => {+ const weekAgents = [+ agent('a1', { day: 16, duration: 3600000, taskType: 'review' }),+ agent('a2', { day: 17, duration: 1800000, taskType: 'review' }),+ agent('a3', { day: 17, success: false, duration: 600000, taskType: 'triage', error: 'timeout' }),+ agent('a4', { day: 18, success: false, duration: 0, taskType: 'triage', error: 'timeout' }),+ ];+ const outOfWeek = agent('old', { day: 10, duration: 999 });+ const neverFinished = agent('open', { status: 'running', completedAt: null });+ onDates([...weekAgents, outOfWeek, neverFinished]);++ const digest = await digestService.generateWeeklyDigest();++ expect(digest.weekId).toBe(THIS_WEEK);+ expect(digest.summary).toEqual({+ totalTasks: 4,+ succeededTasks: 2,+ failedTasks: 2,+ successRate: 50,+ totalWorkTimeMs: 6000000,+ totalWorkTime: '1h 40m',+ });+ expect(digest.weekStart).toBe(new Date(2026, 2, 16, 0, 0, 0).toISOString());+ expect(digest.weekEnd).toBe(new Date(2026, 2, 22, 0, 0, 0).toISOString());+ expect(digest.generatedAt).toBe(NOW.toISOString());+ });++ it('ranks task types by volume with per-type success rates', async () => {+ onDates([+ agent('a1', { day: 16, taskType: 'review' }),+ agent('a2', { day: 16, taskType: 'review' }),+ agent('a3', { day: 17, taskType: 'review', success: false, error: 'lint' }),+ agent('a4', { day: 17, taskType: 'triage' }),+ // No metadata at all → the 'user-task' default bucket.+ agent('a5', { day: 18 }),+ ]);++ const { byTaskType } = await digestService.generateWeeklyDigest();++ expect(byTaskType).toEqual([+ { type: 'review', completed: 3, succeeded: 2, failed: 1, totalDurationMs: 180000, successRate: 67 },+ { type: 'triage', completed: 1, succeeded: 1, failed: 0, totalDurationMs: 60000, successRate: 100 },+ { type: 'user-task', completed: 1, succeeded: 1, failed: 0, totalDurationMs: 60000, successRate: 100 },+ ]);+ });++ it('lists successful tasks as accomplishments, longest first, capped at ten', async () => {+ onDates([+ ...Array.from({ length: 12 }, (_, i) => agent(`ok-${i}`, { day: 16, duration: (i + 1) * 1000, description: `did thing ${i}` })),+ agent('bad', { day: 16, success: false, duration: 99999999, error: 'boom' }),+ ]);++ const { accomplishments } = await digestService.generateWeeklyDigest();++ expect(accomplishments).toHaveLength(10);+ expect(accomplishments[0]).toMatchObject({ id: 'ok-11', taskId: 'task-ok-11', duration: 12000, description: 'did thing 11', taskType: 'task' });+ expect(accomplishments.at(-1).duration).toBe(3000);+ expect(accomplishments.some(a => a.id === 'bad')).toBe(false);+ });++ it('truncates a long accomplishment description to 100 characters', async () => {+ const longDescription = 'x'.repeat(140);+ onDates([agent('a1', { day: 16, description: longDescription })]);++ const { accomplishments } = await digestService.generateWeeklyDigest();++ expect(accomplishments[0].description).toHaveLength(100);+ expect(accomplishments[0].description).toBe(`${'x'.repeat(97)}...`);+ });++ it('groups failures into ranked error patterns, top five only', async () => {+ onDates([+ agent('e1', { day: 16, success: false, error: 'timeout', description: 'y'.repeat(80) }),+ agent('e2', { day: 16, success: false, error: 'timeout' }),+ agent('e3', { day: 17, success: false, error: 'lint' }),+ agent('e4', { day: 17, success: false }),+ ]);++ const { issues } = await digestService.generateWeeklyDigest();++ expect(issues).toHaveLength(3);+ expect(issues[0]).toMatchObject({ error: 'timeout', count: 2 });+ // Issue-level descriptions truncate at 50 chars, and a missing one reads+ // as the explicit 'No description' placeholder.+ expect(issues[0].tasks[0]).toEqual({ id: 'task-e1', description: `${'y'.repeat(47)}...` });+ expect(issues[0].tasks[1]).toEqual({ id: 'task-e2', description: 'No description' });+ expect(issues.map(i => i.error)).toEqual(['timeout', 'lint', 'unknown']);+ });++ it('counts an agent present in both the date index and live state exactly once', async () => {+ const shared = agent('dupe', { day: 17 });+ onDates([shared]);+ mock.agents = [shared, agent('state-only', { day: 18 })];++ const digest = await digestService.generateWeeklyDigest();++ expect(digest.summary.totalTasks).toBe(2);+ expect(digest.accomplishments.map(a => a.id).sort()).toEqual(['dupe', 'state-only']);+ });++ it('persists the digest and announces it on the CoS event bus', async () => {+ const emitted = [];+ const listener = (d) => emitted.push(d.weekId);+ cosEvents.on('digest:generated', listener);+ onDates([agent('a1', { day: 16 })]);++ await digestService.generateWeeklyDigest();++ expect(existsSync(join(DIGESTS_DIR, `${THIS_WEEK}.json`))).toBe(true);+ expect(readDigest(THIS_WEEK).summary.totalTasks).toBe(1);+ expect(emitted).toEqual([THIS_WEEK]);++ cosEvents.off('digest:generated', listener);+ });+});++describe('generateWeeklyDigest — week-over-week comparison', () => {+ it('leaves every delta null when no prior digest exists', async () => {+ onDates([agent('a1', { day: 16 })]);++ const digest = await digestService.generateWeeklyDigest();++ expect(digest.weekOverWeek).toEqual({ tasksChange: null, successRateChange: null, workTimeChange: null });+ expect(digest.previousWeekId).toBeNull();+ });++ it('computes percentage and point deltas against the prior week', async () => {+ writeDigest(LAST_WEEK, storedDigest(LAST_WEEK, { totalTasks: 2, successRate: 100, totalWorkTimeMs: 1000000 }));+ onDates([+ agent('a1', { day: 16, duration: 1000000 }),+ agent('a2', { day: 17, duration: 500000 }),+ agent('a3', { day: 17, success: false, duration: 250000, error: 'timeout' }),+ agent('a4', { day: 18, success: false, duration: 250000, error: 'timeout' }),+ ]);++ const digest = await digestService.generateWeeklyDigest();++ expect(digest.previousWeekId).toBe(LAST_WEEK);+ expect(digest.weekOverWeek).toEqual({ tasksChange: 100, successRateChange: -50, workTimeChange: 100 });+ });++ it('treats a zero-task prior week as a 100% gain, or 0% when still idle', async () => {+ writeDigest(LAST_WEEK, storedDigest(LAST_WEEK, { totalTasks: 0, successRate: 0, totalWorkTimeMs: 0 }));+ onDates([agent('a1', { day: 16, duration: 1000 })]);++ expect((await digestService.generateWeeklyDigest()).weekOverWeek)+ .toEqual({ tasksChange: 100, successRateChange: 100, workTimeChange: 100 });++ onDates([]);+ expect((await digestService.generateWeeklyDigest()).weekOverWeek)+ .toEqual({ tasksChange: 0, successRateChange: 0, workTimeChange: 0 });+ });+});++describe('generateWeeklyDigest — insights', () => {+ const titles = (digest) => digest.insights.map(i => i.title);++ it('calls a week with no completions quiet', async () => {+ const digest = await digestService.generateWeeklyDigest();++ expect(digest.insights).toEqual([{ type: 'info', title: 'Quiet Week', message: 'No tasks were completed this week.' }]);+ });++ it('celebrates twenty or more completions', async () => {+ onDates(Array.from({ length: 20 }, (_, i) => agent(`a${i}`, { day: 16, taskType: 'review' })));++ const digest = await digestService.generateWeeklyDigest();++ expect(digest.insights).toContainEqual({+ type: 'success',+ title: 'High Productivity',+ message: 'Completed 20 tasks this week - excellent output!',+ });+ });++ it('flags rising volume and improving success against the prior week', async () => {+ writeDigest(LAST_WEEK, storedDigest(LAST_WEEK, { totalTasks: 2, successRate: 50, totalWorkTimeMs: 1000 }));+ onDates([+ agent('a1', { day: 16 }),+ agent('a2', { day: 17 }),+ agent('a3', { day: 18 }),+ ]);++ const digest = await digestService.generateWeeklyDigest();++ expect(digest.insights).toContainEqual({+ type: 'success',+ title: 'Increased Output',+ message: 'Task completion increased by 50% compared to last week.',+ });+ expect(digest.insights).toContainEqual({+ type: 'success',+ title: 'Improved Success Rate',+ message: 'Success rate improved by 50 percentage points.',+ });+ });++ it('warns on falling volume and a declining success rate', async () => {+ writeDigest(LAST_WEEK, storedDigest(LAST_WEEK, { totalTasks: 10, successRate: 100, totalWorkTimeMs: 1000 }));+ onDates([+ agent('a1', { day: 16, success: false, error: 'timeout' }),+ agent('a2', { day: 17 }),+ ]);++ const digest = await digestService.generateWeeklyDigest();++ expect(digest.insights).toContainEqual({+ type: 'warning',+ title: 'Decreased Output',+ message: 'Task completion decreased by 80% compared to last week.',+ });+ expect(digest.insights).toContainEqual({+ type: 'warning',+ title: 'Declining Success Rate',+ message: 'Success rate dropped by 50 percentage points.',+ });+ });++ it('names star performers, problem areas, recurring errors and the focus area', async () => {+ onDates([+ // 3 completed at 100% → star performer; the self-improve: prefix is dropped+ // and dashes become spaces in the rendered copy.+ agent('s1', { day: 16, taskType: 'self-improve:code-review' }),+ agent('s2', { day: 16, taskType: 'self-improve:code-review' }),+ agent('s3', { day: 16, taskType: 'self-improve:code-review' }),+ // 4 completed at 25% → needs attention, and its 3 failures are the top error.+ agent('p1', { day: 17, taskType: 'flaky-check' }),+ agent('p2', { day: 17, taskType: 'flaky-check', success: false, error: 'timeout' }),+ agent('p3', { day: 17, taskType: 'flaky-check', success: false, error: 'timeout' }),+ agent('p4', { day: 18, taskType: 'flaky-check', success: false, error: 'timeout' }),+ ]);++ const digest = await digestService.generateWeeklyDigest();++ expect(digest.insights).toContainEqual({+ type: 'success',+ title: 'Star Performer',+ message: 'code review tasks achieved 100% success rate.',+ });+ expect(digest.insights).toContainEqual({+ type: 'warning',+ title: 'Needs Attention',+ message: 'flaky check tasks have only 25% success rate.',+ });+ expect(digest.insights).toContainEqual({+ type: 'action',+ title: 'Recurring Issue',+ message: '"timeout" error occurred 3 times this week.',+ });+ expect(digest.insights).toContainEqual({+ type: 'info',+ title: 'Focus Area',+ message: 'Most effort spent on flaky check tasks (4 completed).',+ });+ });++ it('omits volume-threshold insights for a small, healthy week', async () => {+ onDates([agent('a1', { day: 16, taskType: 'review' }), agent('a2', { day: 17, taskType: 'review' })]);++ const digest = await digestService.generateWeeklyDigest();++ expect(titles(digest)).toEqual(['Focus Area']);+ });+});++describe('getWeeklyDigest', () => {+ it('returns a stored digest for a past week without recomputing', async () => {+ writeDigest('2026-W05', storedDigest('2026-W05', { totalTasks: 99, successRate: 42, totalWorkTimeMs: 5000 }));+ const { getAgents } = await import('./cosAgents.js');++ const digest = await digestService.getWeeklyDigest('2026-W05');++ expect(digest.summary.totalTasks).toBe(99);+ expect(getAgents).not.toHaveBeenCalled();+ });++ it('regenerates the current week even when a stale file exists', async () => {+ writeDigest(THIS_WEEK, storedDigest(THIS_WEEK, { totalTasks: 99, successRate: 42, totalWorkTimeMs: 5000 }));+ onDates([agent('a1', { day: 16 })]);++ const digest = await digestService.getWeeklyDigest();++ expect(digest.summary.totalTasks).toBe(1);+ expect(readDigest(THIS_WEEK).summary.totalTasks).toBe(1);+ });+});++describe('listWeeklyDigests', () => {+ it('summarizes stored digests newest week first', async () => {+ writeDigest('2026-W10', storedDigest('2026-W10', { totalTasks: 4, successRate: 75, totalWorkTimeMs: 1000 }));+ writeDigest(LAST_WEEK, storedDigest(LAST_WEEK, { totalTasks: 7, successRate: 100, totalWorkTimeMs: 2000 }));++ const list = await digestService.listWeeklyDigests();++ expect(list.map(d => d.weekId)).toEqual([LAST_WEEK, '2026-W10']);+ expect(list[0]).toEqual({+ weekId: LAST_WEEK,+ weekStart: at(9),+ weekEnd: at(15),+ totalTasks: 7,+ successRate: 100,+ generatedAt: at(15),+ });+ });++ it('returns an empty list when nothing has been generated', async () => {+ expect(await digestService.listWeeklyDigests()).toEqual([]);+ });+});++describe('compareWeeks', () => {+ it('returns null when either week is missing', async () => {+ writeDigest(LAST_WEEK, storedDigest(LAST_WEEK, { totalTasks: 4, successRate: 50, totalWorkTimeMs: 1000 }));++ expect(await digestService.compareWeeks(LAST_WEEK, '2026-W10')).toBeNull();+ });++ it('reports the deltas between two stored weeks', async () => {+ writeDigest(LAST_WEEK, storedDigest(LAST_WEEK, { totalTasks: 6, successRate: 80, totalWorkTimeMs: 3000 }));+ writeDigest('2026-W10', storedDigest('2026-W10', { totalTasks: 4, successRate: 50, totalWorkTimeMs: 6000 }));++ const result = await digestService.compareWeeks(LAST_WEEK, '2026-W10');++ expect(result.week1).toMatchObject({ weekId: LAST_WEEK, totalTasks: 6, successRate: 80 });+ expect(result.week2).toMatchObject({ weekId: '2026-W10', totalTasks: 4, successRate: 50 });+ expect(result.comparison).toEqual({ tasksChange: 50, successRateChange: 30, workTimeChange: -50 });+ });+});++describe('getCurrentWeekProgress', () => {+ it('projects the full week from the pace so far and lists recent completions', async () => {+ mock.agents = [+ agent('a1', { day: 16, duration: 600000, description: 'z'.repeat(80) }),+ agent('a2', { day: 17, duration: 600000 }),+ agent('a3', { day: 18, duration: 600000, success: false, error: 'timeout' }),+ agent('old', { day: 10, duration: 600000 }),+ { id: 'live', taskId: 'task-live', status: 'running', startedAt: new Date(NOW.getTime() - 600000).toISOString() },+ ];++ const progress = await digestService.getCurrentWeekProgress();++ expect(progress.weekId).toBe(THIS_WEEK);+ expect(progress.weekStart).toBe(new Date(2026, 2, 16, 0, 0, 0).toISOString());+ // Week runs Mon 03-16 → Mon 03-23 00:00; now is Wed 12:00 → 4.5 days left.+ expect(progress.daysRemaining).toBe(5);+ expect(progress.daysPassed).toBe(2);+ expect(progress.current).toMatchObject({+ totalTasks: 3,+ succeededTasks: 2,+ failedTasks: 1,+ successRate: 67,+ totalWorkTimeMs: 1800000,+ totalWorkTime: '30m',+ runningAgents: 1,+ activeTimeMs: 600000,+ activeTime: '10m',+ });+ expect(progress.projected).toEqual({ tasks: 11, workTimeMs: 6300000, workTime: '1h 45m' });+ expect(progress.recentCompletions.map(c => c.id)).toEqual(['a3', 'a2', 'a1']);+ // Recent-completion descriptions truncate at 60 characters.+ expect(progress.recentCompletions.at(-1).description).toBe(`${'z'.repeat(57)}...`);+ });++ it('reports an idle week without projecting phantom tasks', async () => {+ const progress = await digestService.getCurrentWeekProgress();++ expect(progress.current).toMatchObject({ totalTasks: 0, successRate: 0, runningAgents: 0, totalWorkTime: '0m' });+ expect(progress.projected).toEqual({ tasks: 0, workTimeMs: 0, workTime: '0m' });+ expect(progress.recentCompletions).toEqual([]);+ });+});++describe('generateTextSummary', () => {+ it('renders the digest as plain text with week-over-week and insight lines', async () => {+ writeDigest(LAST_WEEK, storedDigest(LAST_WEEK, { totalTasks: 2, successRate: 50, totalWorkTimeMs: 1000 }));+ onDates([+ agent('a1', { day: 16, duration: 1800000, taskType: 'review', description: 'shipped the digest' }),+ agent('a2', { day: 17, duration: 1800000, taskType: 'review', description: 'fixed the streaks' }),+ agent('a3', { day: 18, duration: 0, taskType: 'review', description: 'tried a thing' }),+ ]);++ const text = await digestService.generateTextSummary();++ expect(text).toContain(`Weekly Digest: ${THIS_WEEK}`);+ expect(text).toContain(' - Tasks Completed: 3');+ expect(text).toContain(' - Success Rate: 100%');+ expect(text).toContain(' - Total Work Time: 1h 0m');+ expect(text).toContain(' - Tasks: +50%');+ expect(text).toContain(' - Success Rate: +50 pts');+ expect(text).toContain(' - shipped the digest');+ expect(text).toContain(' [+] Increased Output: Task completion increased by 50% compared to last week.');+ expect(text).toContain(' [-] Focus Area: Most effort spent on review tasks (3 completed).');+ });++ it('omits the week-over-week block when there is no prior week', async () => {+ onDates([agent('a1', { day: 16, description: 'only thing' })]);++ const text = await digestService.generateTextSummary();++ expect(text).not.toContain('Week-over-Week:');+ expect(text).toContain('Top Accomplishments:');+ expect(text).toContain(' - only thing');+ });+});