f9447f8e5f
CI / typecheck · build · test · bundle-size (push) Failing after 1s
CI / clean-room dependency closure smoke (push) Failing after 1s
CI / server-runtime e2e (docker · pg + valkey) (push) Failing after 2s
Deploy Install Scripts / deploy (push) Failing after 2s
Windows / build (push) Has been cancelled
296 lines
9.4 KiB
TypeScript
296 lines
9.4 KiB
TypeScript
import { describe, it, expect } from 'bun:test';
|
|
import { SessionStore } from '../../src/services/sqlite/SessionStore.js';
|
|
import {
|
|
buildTimeline,
|
|
countObservationsByProjects,
|
|
queryObservationsMulti,
|
|
querySummariesMulti,
|
|
} from '../../src/services/context/ObservationCompiler.js';
|
|
import type { ContextConfig, Observation, SummaryTimelineItem } from '../../src/services/context/types.js';
|
|
|
|
function createTestObservation(overrides: Partial<Observation> = {}): Observation {
|
|
return {
|
|
id: 1,
|
|
memory_session_id: 'session-123',
|
|
type: 'discovery',
|
|
title: 'Test Observation',
|
|
subtitle: null,
|
|
narrative: 'A test narrative',
|
|
facts: '["fact1"]',
|
|
concepts: '["concept1"]',
|
|
files_read: null,
|
|
files_modified: null,
|
|
discovery_tokens: 100,
|
|
created_at: '2025-01-01T12:00:00.000Z',
|
|
created_at_epoch: 1735732800000,
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
function createTestSummaryTimelineItem(overrides: Partial<SummaryTimelineItem> = {}): SummaryTimelineItem {
|
|
return {
|
|
id: 1,
|
|
memory_session_id: 'session-123',
|
|
request: 'Test Request',
|
|
investigated: 'Investigated things',
|
|
learned: 'Learned things',
|
|
completed: 'Completed things',
|
|
next_steps: 'Next steps',
|
|
created_at: '2025-01-01T12:00:00.000Z',
|
|
created_at_epoch: 1735732800000,
|
|
displayEpoch: 1735732800000,
|
|
displayTime: '2025-01-01T12:00:00.000Z',
|
|
shouldShowLink: false,
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
describe('buildTimeline', () => {
|
|
it('should combine observations and summaries into timeline', () => {
|
|
const observations = [
|
|
createTestObservation({ id: 1, created_at_epoch: 1000 }),
|
|
];
|
|
const summaries = [
|
|
createTestSummaryTimelineItem({ id: 1, displayEpoch: 2000 }),
|
|
];
|
|
|
|
const timeline = buildTimeline(observations, summaries);
|
|
|
|
expect(timeline).toHaveLength(2);
|
|
});
|
|
|
|
it('should sort timeline items chronologically by epoch', () => {
|
|
const observations = [
|
|
createTestObservation({ id: 1, created_at_epoch: 3000 }),
|
|
createTestObservation({ id: 2, created_at_epoch: 1000 }),
|
|
];
|
|
const summaries = [
|
|
createTestSummaryTimelineItem({ id: 1, displayEpoch: 2000 }),
|
|
];
|
|
|
|
const timeline = buildTimeline(observations, summaries);
|
|
|
|
expect(timeline).toHaveLength(3);
|
|
expect(timeline[0].type).toBe('observation');
|
|
expect((timeline[0].data as Observation).id).toBe(2);
|
|
expect(timeline[1].type).toBe('summary');
|
|
expect(timeline[2].type).toBe('observation');
|
|
expect((timeline[2].data as Observation).id).toBe(1);
|
|
});
|
|
|
|
it('should handle empty observations array', () => {
|
|
const summaries = [
|
|
createTestSummaryTimelineItem({ id: 1, displayEpoch: 1000 }),
|
|
];
|
|
|
|
const timeline = buildTimeline([], summaries);
|
|
|
|
expect(timeline).toHaveLength(1);
|
|
expect(timeline[0].type).toBe('summary');
|
|
});
|
|
|
|
it('should handle empty summaries array', () => {
|
|
const observations = [
|
|
createTestObservation({ id: 1, created_at_epoch: 1000 }),
|
|
];
|
|
|
|
const timeline = buildTimeline(observations, []);
|
|
|
|
expect(timeline).toHaveLength(1);
|
|
expect(timeline[0].type).toBe('observation');
|
|
});
|
|
|
|
it('should handle both empty arrays', () => {
|
|
const timeline = buildTimeline([], []);
|
|
|
|
expect(timeline).toHaveLength(0);
|
|
});
|
|
|
|
it('should correctly tag items with their type', () => {
|
|
const observations = [createTestObservation()];
|
|
const summaries = [createTestSummaryTimelineItem()];
|
|
|
|
const timeline = buildTimeline(observations, summaries);
|
|
|
|
const observationItem = timeline.find(item => item.type === 'observation');
|
|
const summaryItem = timeline.find(item => item.type === 'summary');
|
|
|
|
expect(observationItem).toBeDefined();
|
|
expect(summaryItem).toBeDefined();
|
|
expect(observationItem!.data).toHaveProperty('narrative');
|
|
expect(summaryItem!.data).toHaveProperty('request');
|
|
});
|
|
|
|
it('should use displayEpoch for summary sorting, not created_at_epoch', () => {
|
|
const observations = [
|
|
createTestObservation({ id: 1, created_at_epoch: 2000 }),
|
|
];
|
|
const summaries = [
|
|
createTestSummaryTimelineItem({
|
|
id: 1,
|
|
created_at_epoch: 3000, // Created later
|
|
displayEpoch: 1000, // But displayed earlier
|
|
}),
|
|
];
|
|
|
|
const timeline = buildTimeline(observations, summaries);
|
|
|
|
expect(timeline[0].type).toBe('summary');
|
|
expect(timeline[1].type).toBe('observation');
|
|
});
|
|
});
|
|
|
|
describe('context compiler platform scoping', () => {
|
|
const config: ContextConfig = {
|
|
totalObservationCount: 20,
|
|
fullObservationCount: 3,
|
|
sessionCount: 20,
|
|
showReadTokens: true,
|
|
showWorkTokens: true,
|
|
showSavingsAmount: true,
|
|
showSavingsPercent: true,
|
|
observationTypes: new Set(['discovery']),
|
|
observationConcepts: new Set(['platform-scope']),
|
|
fullObservationField: 'narrative',
|
|
showLastSummary: true,
|
|
showLastMessage: false,
|
|
};
|
|
|
|
function seed(
|
|
store: SessionStore,
|
|
input: {
|
|
project: string;
|
|
contentSessionId: string;
|
|
memorySessionId: string;
|
|
platformSource: string;
|
|
title: string;
|
|
summaryRequest: string;
|
|
createdAtEpoch: number;
|
|
},
|
|
): void {
|
|
const sessionDbId = store.createSDKSession(
|
|
input.contentSessionId,
|
|
input.project,
|
|
`${input.platformSource} prompt`,
|
|
undefined,
|
|
input.platformSource,
|
|
);
|
|
store.ensureMemorySessionIdRegistered(sessionDbId, input.memorySessionId);
|
|
store.storeObservation(
|
|
input.memorySessionId,
|
|
input.project,
|
|
{
|
|
type: 'discovery',
|
|
title: input.title,
|
|
subtitle: null,
|
|
facts: [],
|
|
narrative: `${input.platformSource} context narrative`,
|
|
concepts: ['platform-scope'],
|
|
files_read: [],
|
|
files_modified: [],
|
|
},
|
|
1,
|
|
0,
|
|
input.createdAtEpoch,
|
|
);
|
|
store.storeSummary(
|
|
input.memorySessionId,
|
|
input.project,
|
|
{
|
|
request: input.summaryRequest,
|
|
investigated: 'investigated',
|
|
learned: 'learned',
|
|
completed: 'completed',
|
|
next_steps: 'next',
|
|
notes: null,
|
|
},
|
|
1,
|
|
0,
|
|
input.createdAtEpoch,
|
|
);
|
|
}
|
|
|
|
it('filters observations, summaries, and project counts by platformSource when supplied', () => {
|
|
const store = new SessionStore(':memory:');
|
|
try {
|
|
seed(store, {
|
|
project: 'context-platform-project',
|
|
contentSessionId: 'shared-context-id',
|
|
memorySessionId: 'codex-context-memory',
|
|
platformSource: 'codex',
|
|
title: 'CODEX_CONTEXT_OBS',
|
|
summaryRequest: 'CODEX_CONTEXT_SUMMARY',
|
|
createdAtEpoch: 1_700_000_000_000,
|
|
});
|
|
seed(store, {
|
|
project: 'context-platform-project',
|
|
contentSessionId: 'shared-context-id',
|
|
memorySessionId: 'claude-context-memory',
|
|
platformSource: 'claude',
|
|
title: 'CLAUDE_CONTEXT_OBS',
|
|
summaryRequest: 'CLAUDE_CONTEXT_SUMMARY',
|
|
createdAtEpoch: 1_700_000_001_000,
|
|
});
|
|
|
|
const codexObservations = queryObservationsMulti(store, ['context-platform-project'], config, 'codex');
|
|
expect(codexObservations.map(obs => obs.title)).toEqual(['CODEX_CONTEXT_OBS']);
|
|
expect(codexObservations[0].platform_source).toBe('codex');
|
|
|
|
const codexSummaries = querySummariesMulti(store, ['context-platform-project'], config, 'codex');
|
|
expect(codexSummaries.map(summary => summary.request)).toEqual(['CODEX_CONTEXT_SUMMARY']);
|
|
expect(codexSummaries[0].platform_source).toBe('codex');
|
|
|
|
expect(countObservationsByProjects(store, ['context-platform-project'], 'codex')).toBe(1);
|
|
expect(countObservationsByProjects(store, ['context-platform-project'], 'claude')).toBe(1);
|
|
expect(countObservationsByProjects(store, ['context-platform-project'])).toBe(2);
|
|
} finally {
|
|
store.close();
|
|
}
|
|
});
|
|
|
|
it('applies platformSource across multi-project context queries', () => {
|
|
const store = new SessionStore(':memory:');
|
|
try {
|
|
seed(store, {
|
|
project: 'context-parent',
|
|
contentSessionId: 'parent-codex',
|
|
memorySessionId: 'parent-codex-memory',
|
|
platformSource: 'codex',
|
|
title: 'PARENT_CODEX_OBS',
|
|
summaryRequest: 'PARENT_CODEX_SUMMARY',
|
|
createdAtEpoch: 1_700_000_000_000,
|
|
});
|
|
seed(store, {
|
|
project: 'context-worktree',
|
|
contentSessionId: 'worktree-codex',
|
|
memorySessionId: 'worktree-codex-memory',
|
|
platformSource: 'codex',
|
|
title: 'WORKTREE_CODEX_OBS',
|
|
summaryRequest: 'WORKTREE_CODEX_SUMMARY',
|
|
createdAtEpoch: 1_700_000_001_000,
|
|
});
|
|
seed(store, {
|
|
project: 'context-worktree',
|
|
contentSessionId: 'worktree-claude',
|
|
memorySessionId: 'worktree-claude-memory',
|
|
platformSource: 'claude',
|
|
title: 'WORKTREE_CLAUDE_OBS',
|
|
summaryRequest: 'WORKTREE_CLAUDE_SUMMARY',
|
|
createdAtEpoch: 1_700_000_002_000,
|
|
});
|
|
|
|
const projects = ['context-parent', 'context-worktree'];
|
|
expect(queryObservationsMulti(store, projects, config, 'codex').map(obs => obs.title)).toEqual([
|
|
'WORKTREE_CODEX_OBS',
|
|
'PARENT_CODEX_OBS',
|
|
]);
|
|
expect(querySummariesMulti(store, projects, config, 'codex').map(summary => summary.request)).toEqual([
|
|
'WORKTREE_CODEX_SUMMARY',
|
|
'PARENT_CODEX_SUMMARY',
|
|
]);
|
|
} finally {
|
|
store.close();
|
|
}
|
|
});
|
|
});
|