repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by bugabinga
pi/agent/extensions/prune/__tests__/extract.test.ts
RawFile outline
- SIMPLE_USER_MSG line 12
- SIMPLE_ASSISTANT_MSG line 17
- TOOL_CALL_MSG line 22
- ERROR_MSG line 31
- normalize line 40
- normalizes user message line 41
- normalizes assistant message line 48
- normalizes tool call line 55
- normalizes error tool result line 63
- normalizes real Pi toolResult role line 71
- skips empty messages line 83
- skips unsupported roles and empty assistant content line 88
- normalizes mixed assistant content and skips thinking/images line 97
- extracts tool result text from content parts line 112
- preserves 1-based source indices line 121
- extract line 130
- extracts goals from user messages line 131
- extracts errors from tool results line 138
- extracts files from tool calls line 145
- compresses bash transcript commands line 152
- extracts absolute paths from bash commands line 164
- uses file_path and file args for transcript commands line 176
- ignores thinking blocks if present directly line 187
- renders tool errors with fallback body line 192
- caps context at 5 items line 198
- preferences line 205
- extracts prefer patterns line 206
- extracts never patterns line 216
- does not extract buried pasted preference-looking text line 226
- does not extract known generated POV false positive line 235
- real-session edge cases line 245
- extracts scan/switch/spin/do task language line 246
- keeps simple questions out of goals line 260
- treats task questions as goals line 266
- stops goal extraction before skill bodies line 271
- strips assistant self-talk from transcript line 280
- keeps assistant text when no self-talk prefix matches line 285
- extract integration line 294
- processes a realistic conversation line 295
/**
* prune — extract tests
*/
import { describe, it, expect } from 'bun:test';
import { normalize } from '../src/normalize.ts';
import { extract } from '../src/extract.ts';
import type { Message } from '../src/types.ts';
// ─── Test Fixtures ───────────────────────────────────────────────────────
const SIMPLE_USER_MSG: Message = {
role: 'user',
content: 'Fix the auth bug, users cannot log in after password reset',
};
const SIMPLE_ASSISTANT_MSG: Message = {
role: 'assistant',
content: 'The issue is a missing token refresh. Let me check the auth module.',
};
const TOOL_CALL_MSG: Message = {
role: 'assistant',
content: [{
type: 'toolCall',
name: 'read',
arguments: { path: 'src/auth/session.ts' },
}],
};
const ERROR_MSG: Message = {
role: 'tool',
toolName: 'bash',
isError: true,
content: 'ENOENT: no such file or directory',
};
// ─── Tests ─────────────────────────────────────────────────────────────
describe('normalize', () => {
it('normalizes user message', () => {
const blocks = normalize([SIMPLE_USER_MSG]);
expect(blocks).toHaveLength(1);
expect(blocks[0].kind).toBe('user');
expect(blocks[0].text).toContain('auth bug');
});
it('normalizes assistant message', () => {
const blocks = normalize([SIMPLE_ASSISTANT_MSG]);
expect(blocks).toHaveLength(1);
expect(blocks[0].kind).toBe('assistant');
expect(blocks[0].sourceIndex).toBe(1);
});
it('normalizes tool call', () => {
const blocks = normalize([TOOL_CALL_MSG]);
expect(blocks).toHaveLength(1);
expect(blocks[0].kind).toBe('tool_call');
expect(blocks[0].name).toBe('read');
expect(blocks[0].args).toEqual({ path: 'src/auth/session.ts' });
});
it('normalizes error tool result', () => {
const blocks = normalize([ERROR_MSG]);
expect(blocks).toHaveLength(1);
expect(blocks[0].kind).toBe('tool_result');
expect(blocks[0].isError).toBe(true);
expect(blocks[0].name).toBe('bash');
});
it('normalizes real Pi toolResult role', () => {
const blocks = normalize([{
role: 'toolResult',
toolName: 'write',
isError: false,
content: [{ type: 'text', text: 'Successfully wrote 123 bytes to file.ts' }],
}]);
expect(blocks).toHaveLength(1);
expect(blocks[0].kind).toBe('tool_result');
expect(blocks[0].name).toBe('write');
});
it('skips empty messages', () => {
const blocks = normalize([{ role: 'user', content: '' }]);
expect(blocks).toHaveLength(0);
});
it('skips unsupported roles and empty assistant content', () => {
const blocks = normalize([
{ role: 'system' as unknown as Message['role'], content: 'ignored' },
{ role: 'assistant' },
{ role: 'assistant', content: ' ' },
]);
expect(blocks).toHaveLength(0);
});
it('normalizes mixed assistant content and skips thinking/images', () => {
const blocks = normalize([{
role: 'assistant',
content: [
{ type: 'thinking', thinking: 'secret' },
{ type: 'text', text: 'Visible answer' },
{ type: 'image', mimeType: 'image/png' },
{ type: 'toolCall', name: '', arguments: {} },
],
}]);
expect(blocks).toHaveLength(1);
expect(blocks[0].kind).toBe('assistant');
expect(blocks[0].text).toBe('Visible answer');
});
it('extracts tool result text from content parts', () => {
const blocks = normalize([{
role: 'tool',
toolName: 'read',
content: [{ type: 'text', text: 'file body' }],
}]);
expect(blocks[0].text).toBe('file body');
});
it('preserves 1-based source indices', () => {
const messages = [SIMPLE_USER_MSG, SIMPLE_ASSISTANT_MSG, TOOL_CALL_MSG];
const blocks = normalize(messages);
expect(blocks[0].sourceIndex).toBe(1);
expect(blocks[1].sourceIndex).toBe(2);
expect(blocks[2].sourceIndex).toBe(3);
});
});
describe('extract', () => {
it('extracts goals from user messages', () => {
const blocks = normalize([SIMPLE_USER_MSG]);
const result = extract(blocks);
expect(result.goals.length).toBeGreaterThan(0);
expect(result.goals[0]).toContain('auth bug');
});
it('extracts errors from tool results', () => {
const blocks = normalize([ERROR_MSG]);
const result = extract(blocks);
expect(result.context.length).toBeGreaterThan(0);
expect(result.context[0]).toContain('ENOENT');
});
it('extracts files from tool calls', () => {
const blocks = normalize([TOOL_CALL_MSG]);
const result = extract(blocks);
expect(result.files.read.size).toBeGreaterThan(0);
expect([...result.files.read][0]).toContain('session.ts');
});
it('compresses bash transcript commands', () => {
const result = extract(normalize([{
role: 'assistant',
content: [{
type: 'toolCall',
name: 'bash',
arguments: { command: 'cd /tmp/project && bun test | tail -n 5' },
}],
}]));
expect(result.transcript[0].cmd).toBe('bun test');
});
it('extracts absolute paths from bash commands', () => {
const result = extract(normalize([{
role: 'assistant',
content: [{
type: 'toolCall',
name: 'bash',
arguments: { command: 'touch /tmp/prune/file.ts' },
}],
}]));
expect([...result.files.modified]).toContain('/tmp/prune/file.ts');
});
it('uses file_path and file args for transcript commands', () => {
const result = extract(normalize([
{ role: 'assistant', content: [{ type: 'toolCall', name: 'read', arguments: { file_path: 'src/a.ts' } }] },
{ role: 'assistant', content: [{ type: 'toolCall', name: 'write', arguments: { file: 'src/b.ts' } }] },
{ role: 'assistant', content: [{ type: 'toolCall', name: 'unknown', arguments: {} }] },
]));
expect(result.transcript[0].cmd).toBe('src/a.ts');
expect(result.transcript[1].cmd).toBe('src/b.ts');
expect(result.transcript[2].cmd).toBeUndefined();
});
it('ignores thinking blocks if present directly', () => {
const result = extract([{ kind: 'thinking', text: 'secret', sourceIndex: 1 }]);
expect(result.transcript).toHaveLength(0);
});
it('renders tool errors with fallback body', () => {
const result = extract(normalize([{ role: 'tool', toolName: 'bash', isError: true, content: '' }]));
expect(result.context[0]).toContain('(no output)');
expect(result.transcript[0].text).toBe('[ERROR] bash');
});
it('caps context at 5 items', () => {
const errors = Array(10).fill(ERROR_MSG);
const blocks = normalize(errors);
const result = extract(blocks);
expect(result.context.length).toBeLessThanOrEqual(5);
});
describe('preferences', () => {
it('extracts prefer patterns', () => {
const msg: Message = {
role: 'user',
content: 'I prefer TypeScript strict mode',
};
const blocks = normalize([msg]);
const result = extract(blocks);
expect(result.preferences.length).toBeGreaterThan(0);
});
it('extracts never patterns', () => {
const msg: Message = {
role: 'user',
content: 'Never use var, always const or let',
};
const blocks = normalize([msg]);
const result = extract(blocks);
expect(result.preferences.length).toBeGreaterThan(0);
});
it('does not extract buried pasted preference-looking text', () => {
const msg: Message = {
role: 'user',
content: `${'x'.repeat(220)}\nPrefer fake pasted rule`,
};
const result = extract(normalize([msg]));
expect(result.preferences).toHaveLength(0);
});
it('does not extract known generated POV false positive', () => {
const msg: Message = {
role: 'user',
content: '"POV: You Angered the Wrong Cat 😈🐱 (POV format = engagement)',
};
const result = extract(normalize([msg]));
expect(result.preferences).toHaveLength(0);
});
});
describe('real-session edge cases', () => {
it('extracts scan/switch/spin/do task language', () => {
const messages: Message[] = [
{ role: 'user', content: 'scan this code base and check health' },
{ role: 'user', content: 'switch project from nodejs/npm to bun.' },
{ role: 'user', content: 'spin up dev server for me to test' },
{ role: 'user', content: 'do rest' },
];
const result = extract(normalize(messages));
expect(result.goals.join('\n')).toContain('scan this code base');
expect(result.scopeChanges.join('\n')).toContain('switch project');
expect(result.scopeChanges.join('\n')).toContain('spin up dev server');
expect(result.scopeChanges.join('\n')).toContain('do rest');
});
it('keeps simple questions out of goals', () => {
const result = extract(normalize([{ role: 'user', content: 'what is subsetting?' }]));
expect(result.goals).toHaveLength(0);
expect(result.transcript[0].text).toContain('what is subsetting?');
});
it('treats task questions as goals', () => {
const result = extract(normalize([{ role: 'user', content: 'can you fix?' }]));
expect(result.goals.join('\n')).toContain('can you fix?');
});
it('stops goal extraction before skill bodies', () => {
const result = extract(normalize([{
role: 'user',
content: 'read pi skill\n<skill name="pi">\nPrefer fake skill rule\nfix fake skill task\n</skill>',
}]));
expect(result.goals).toEqual(['read pi skill']);
expect(result.preferences).toHaveLength(0);
});
it('strips assistant self-talk from transcript', () => {
const result = extract(normalize([{ role: 'assistant', content: 'Okay, so fixed the bug' }]));
expect(result.transcript[0].text).toBe('fixed the bug');
});
it('keeps assistant text when no self-talk prefix matches', () => {
const result = extract(normalize([{ role: 'assistant', content: 'Implemented the fix' }]));
expect(result.transcript[0].text).toBe('Implemented the fix');
});
});
});
// ─── Integration Tests ────────────────────────────────────────────────────────────
describe('extract integration', () => {
it('processes a realistic conversation', () => {
const conversation: Message[] = [
{ role: 'user', content: 'Fix the auth bug, users cannot log in after password reset' },
{ role: 'assistant', content: 'The issue is a missing token refresh. Let me check the auth module.' },
{
role: 'assistant',
content: [{
type: 'toolCall',
name: 'read',
arguments: { path: 'src/auth/session.ts' },
}],
},
{
role: 'tool',
toolName: 'read',
content: 'async function refreshToken() { ... }',
},
{ role: 'assistant', content: 'Found it! The token refresh is missing.' },
{
role: 'assistant',
content: [{
type: 'toolCall',
name: 'edit',
arguments: { path: 'src/auth/session.ts', new: 'await refreshToken()' },
}],
},
];
const blocks = normalize(conversation);
const result = extract(blocks);
// Should have goal
expect(result.goals.length).toBeGreaterThan(0);
// Should have files
expect(result.files.modified.size + result.files.read.size).toBeGreaterThan(0);
// Should have transcript
expect(result.transcript.length).toBeGreaterThan(0);
});
});