mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
* refactor storage to use worker-based vector operations * update memory service and agent implementations * update opencode plugin sdk * update memory plugin documentation * add tests for memory service and injection hook * update lockfile for dependency changes * Add memory plugin v0.0.13 with message deduplication and config UI * Remove planning state feature from memory plugin
146 lines
4.5 KiB
TypeScript
146 lines
4.5 KiB
TypeScript
import { describe, test, expect } from 'bun:test'
|
|
import {
|
|
buildCustomCompactionPrompt,
|
|
formatCompactionDiagnostics,
|
|
estimateTokens,
|
|
trimToTokenBudget,
|
|
extractCompactionSummary,
|
|
} from '../src/hooks/compaction-utils'
|
|
|
|
describe('buildCustomCompactionPrompt', () => {
|
|
test('returns a non-empty string', () => {
|
|
const prompt = buildCustomCompactionPrompt()
|
|
expect(prompt).toBeTruthy()
|
|
expect(typeof prompt).toBe('string')
|
|
expect(prompt.length).toBeGreaterThan(100)
|
|
})
|
|
|
|
test('contains required sections', () => {
|
|
const prompt = buildCustomCompactionPrompt()
|
|
expect(prompt).toContain('## CRITICAL - Preserve These Verbatim')
|
|
expect(prompt).toContain('### Active Task')
|
|
expect(prompt).toContain('### Key Context')
|
|
expect(prompt).toContain('### Active Files')
|
|
expect(prompt).toContain('### Next Steps')
|
|
})
|
|
})
|
|
|
|
describe('formatCompactionDiagnostics', () => {
|
|
test('returns empty string for zero counts', () => {
|
|
const result = formatCompactionDiagnostics({
|
|
conventions: 0,
|
|
decisions: 0,
|
|
tokensInjected: 0,
|
|
})
|
|
expect(result).toBe('')
|
|
})
|
|
|
|
test('formats single items correctly', () => {
|
|
const result = formatCompactionDiagnostics({
|
|
conventions: 1,
|
|
decisions: 0,
|
|
tokensInjected: 100,
|
|
})
|
|
expect(result).toContain('1 convention')
|
|
expect(result).toContain('~100 tokens injected')
|
|
})
|
|
|
|
test('formats multiple items correctly', () => {
|
|
const result = formatCompactionDiagnostics({
|
|
conventions: 5,
|
|
decisions: 2,
|
|
tokensInjected: 800,
|
|
})
|
|
expect(result).toContain('5 conventions')
|
|
expect(result).toContain('2 decisions')
|
|
expect(result).toContain('~800 tokens injected')
|
|
})
|
|
})
|
|
|
|
describe('estimateTokens', () => {
|
|
test('estimates based on character count', () => {
|
|
const text = 'a'.repeat(400)
|
|
const tokens = estimateTokens(text)
|
|
expect(tokens).toBe(100)
|
|
})
|
|
|
|
test('handles empty string', () => {
|
|
expect(estimateTokens('')).toBe(0)
|
|
})
|
|
})
|
|
|
|
describe('trimToTokenBudget', () => {
|
|
test('returns original if under budget', () => {
|
|
const text = 'short text'
|
|
const result = trimToTokenBudget(text, 100, 'high')
|
|
expect(result).toBe('short text')
|
|
})
|
|
|
|
test('trims from end for low priority', () => {
|
|
const text = 'line1\nline2\nline3\nline4\nline5'
|
|
const result = trimToTokenBudget(text, 1, 'low')
|
|
expect(result).toContain('...')
|
|
expect(result.startsWith('line')).toBe(true)
|
|
})
|
|
|
|
test('trims from middle for medium priority', () => {
|
|
const text = 'line1\nline2\nline3\nline4\nline5\nline6\nline7\nline8\nline9\nline10'
|
|
const result = trimToTokenBudget(text, 2, 'medium')
|
|
expect(result).toContain('...')
|
|
})
|
|
|
|
test('preserves high priority content', () => {
|
|
const text = 'line1\nline2\nline3'
|
|
const result = trimToTokenBudget(text, 1, 'high')
|
|
expect(result).toContain('...')
|
|
})
|
|
})
|
|
|
|
describe('extractCompactionSummary', () => {
|
|
test('extracts text from last assistant message', () => {
|
|
const messages = [
|
|
{ info: { role: 'user' }, parts: [{ type: 'text', text: 'User message' }] },
|
|
{ info: { role: 'assistant' }, parts: [{ type: 'text', text: 'Compaction summary here' }] },
|
|
]
|
|
const result = extractCompactionSummary(messages)
|
|
expect(result).toBe('Compaction summary here')
|
|
})
|
|
|
|
test('returns null when no assistant messages', () => {
|
|
const messages = [
|
|
{ info: { role: 'user' }, parts: [{ type: 'text', text: 'User message' }] },
|
|
]
|
|
const result = extractCompactionSummary(messages)
|
|
expect(result).toBeNull()
|
|
})
|
|
|
|
test('returns null for empty messages', () => {
|
|
expect(extractCompactionSummary([])).toBeNull()
|
|
})
|
|
|
|
test('concatenates multiple text parts', () => {
|
|
const messages = [
|
|
{
|
|
info: { role: 'assistant' },
|
|
parts: [
|
|
{ type: 'text', text: 'Part 1' },
|
|
{ type: 'tool', text: undefined },
|
|
{ type: 'text', text: 'Part 2' },
|
|
],
|
|
},
|
|
]
|
|
const result = extractCompactionSummary(messages)
|
|
expect(result).toBe('Part 1\nPart 2')
|
|
})
|
|
|
|
test('picks the last assistant message when multiple exist', () => {
|
|
const messages = [
|
|
{ info: { role: 'assistant' }, parts: [{ type: 'text', text: 'Old summary' }] },
|
|
{ info: { role: 'user' }, parts: [{ type: 'text', text: 'User msg' }] },
|
|
{ info: { role: 'assistant' }, parts: [{ type: 'text', text: 'Latest summary' }] },
|
|
]
|
|
const result = extractCompactionSummary(messages)
|
|
expect(result).toBe('Latest summary')
|
|
})
|
|
})
|