Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions packages/adf/src/__tests__/manifest.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { describe, it, expect } from 'vitest';
import { isKeywordMatch, buildTriggerReport } from '../manifest';
import type { Manifest } from '../types';

describe('isKeywordMatch', () => {
it('matches exact keywords', () => {
expect(isKeywordMatch('react', 'react')).toBe(true);
});

it('rejects different keywords', () => {
expect(isKeywordMatch('react', 'vue')).toBe(false);
});

it('matches prefix stem when trigger is prefix of keyword', () => {
// "react" (5 chars) is prefix of "reacting" (8 chars), ratio 5/8 = 0.625 < 0.66 → no match
expect(isKeywordMatch('react', 'reacting')).toBe(false);
// "node" (4 chars) is prefix of "nodes" (5 chars), ratio 4/5 = 0.80 >= 0.66 → match
expect(isKeywordMatch('node', 'nodes')).toBe(true);
});

it('matches prefix stem when keyword is prefix of trigger', () => {
// "data" (4 chars) is prefix of "database" (8 chars), ratio 4/8 = 0.50 < 0.66 → no match
expect(isKeywordMatch('database', 'data')).toBe(false);
// "api" (3 chars) — too short (<4), no prefix match
expect(isKeywordMatch('apis', 'api')).toBe(false);
});

it('requires minimum 4 chars for prefix matching', () => {
// "css" (3 chars) prefix of "cssx" — too short
expect(isKeywordMatch('css', 'cssx')).toBe(false);
});

it('rejects when length ratio is below 66%', () => {
// "test" (4 chars) prefix of "testing123" (10 chars), ratio 4/10 = 0.40 < 0.66
expect(isKeywordMatch('test', 'testing123')).toBe(false);
});
});

describe('buildTriggerReport', () => {
const manifest: Manifest = {
version: '0.1',
defaultLoad: ['core.adf'],
onDemand: [
{ path: 'frontend.adf', triggers: ['React', 'CSS'], loadPolicy: 'ON_DEMAND' },
{ path: 'backend.adf', triggers: ['API', 'Node'], loadPolicy: 'ON_DEMAND' },
],
rules: [],
sync: [],
cadence: [],
metrics: [],
};

it('reports matched triggers with keywords', () => {
const report = buildTriggerReport(manifest, ['core.adf', 'frontend.adf'], ['React']);
const reactEntry = report.find(r => r.trigger === 'React');
expect(reactEntry).toBeDefined();
expect(reactEntry!.matched).toBe(true);
expect(reactEntry!.matchedKeywords).toEqual(['react']);
expect(reactEntry!.loadReason).toBe('trigger');
});

it('reports unmatched triggers with empty keywords', () => {
const report = buildTriggerReport(manifest, ['core.adf'], ['React']);
const apiEntry = report.find(r => r.trigger === 'API');
expect(apiEntry).toBeDefined();
expect(apiEntry!.matched).toBe(false);
expect(apiEntry!.matchedKeywords).toEqual([]);
});

it('includes all triggers from all on-demand modules', () => {
const report = buildTriggerReport(manifest, ['core.adf'], []);
expect(report).toHaveLength(4); // React, CSS, API, Node
});
});
123 changes: 123 additions & 0 deletions packages/adf/src/__tests__/merger.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { describe, it, expect } from 'vitest';
import { mergeDocuments, estimateTokens } from '../merger';
import type { AdfDocument } from '../types';

describe('mergeDocuments', () => {
it('merges list sections by concatenating items', () => {
const doc1: AdfDocument = {
version: '0.1',
sections: [{ key: 'RULES', decoration: null, content: { type: 'list', items: ['Rule A'] } }],
};
const doc2: AdfDocument = {
version: '0.1',
sections: [{ key: 'RULES', decoration: null, content: { type: 'list', items: ['Rule B'] } }],
};
const merged = mergeDocuments([doc1, doc2]);
const rules = merged.sections.find(s => s.key === 'RULES');
expect(rules).toBeDefined();
expect(rules!.content).toEqual({ type: 'list', items: ['Rule A', 'Rule B'] });
});

it('merges map sections by concatenating entries', () => {
const doc1: AdfDocument = {
version: '0.1',
sections: [{ key: 'STATE', decoration: null, content: { type: 'map', entries: [{ key: 'A', value: '1' }] } }],
};
const doc2: AdfDocument = {
version: '0.1',
sections: [{ key: 'STATE', decoration: null, content: { type: 'map', entries: [{ key: 'B', value: '2' }] } }],
};
const merged = mergeDocuments([doc1, doc2]);
const state = merged.sections.find(s => s.key === 'STATE');
expect(state!.content).toEqual({ type: 'map', entries: [{ key: 'A', value: '1' }, { key: 'B', value: '2' }] });
});

it('merges text sections by joining with newline', () => {
const doc1: AdfDocument = {
version: '0.1',
sections: [{ key: 'ROLE', decoration: null, content: { type: 'text', value: 'Part 1' } }],
};
const doc2: AdfDocument = {
version: '0.1',
sections: [{ key: 'ROLE', decoration: null, content: { type: 'text', value: 'Part 2' } }],
};
const merged = mergeDocuments([doc1, doc2]);
const role = merged.sections.find(s => s.key === 'ROLE');
expect(role!.content).toEqual({ type: 'text', value: 'Part 1\nPart 2' });
});

it('merges metric sections by concatenating entries', () => {
const doc1: AdfDocument = {
version: '0.1',
sections: [{ key: 'METRICS', decoration: null, content: { type: 'metric', entries: [{ key: 'loc', value: 100, ceiling: 200, unit: 'lines' }] } }],
};
const doc2: AdfDocument = {
version: '0.1',
sections: [{ key: 'METRICS', decoration: null, content: { type: 'metric', entries: [{ key: 'fns', value: 10, ceiling: 20, unit: 'count' }] } }],
};
const merged = mergeDocuments([doc1, doc2]);
const metrics = merged.sections.find(s => s.key === 'METRICS');
expect(metrics!.content.type).toBe('metric');
if (metrics!.content.type === 'metric') {
expect(metrics!.content.entries).toHaveLength(2);
}
});

it('keeps target content for mismatched types (first-wins)', () => {
const doc1: AdfDocument = {
version: '0.1',
sections: [{ key: 'DATA', decoration: null, content: { type: 'text', value: 'text' } }],
};
const doc2: AdfDocument = {
version: '0.1',
sections: [{ key: 'DATA', decoration: null, content: { type: 'list', items: ['item'] } }],
};
const merged = mergeDocuments([doc1, doc2]);
const data = merged.sections.find(s => s.key === 'DATA');
expect(data!.content).toEqual({ type: 'text', value: 'text' });
});

it('promotes weight to load-bearing when either source is load-bearing', () => {
const doc1: AdfDocument = {
version: '0.1',
sections: [{ key: 'RULES', decoration: null, content: { type: 'list', items: ['A'] }, weight: 'advisory' }],
};
const doc2: AdfDocument = {
version: '0.1',
sections: [{ key: 'RULES', decoration: null, content: { type: 'list', items: ['B'] }, weight: 'load-bearing' }],
};
const merged = mergeDocuments([doc1, doc2]);
expect(merged.sections[0].weight).toBe('load-bearing');
});

it('does not mutate input documents', () => {
const doc1: AdfDocument = {
version: '0.1',
sections: [{ key: 'RULES', decoration: null, content: { type: 'list', items: ['A'] } }],
};
const doc2: AdfDocument = {
version: '0.1',
sections: [{ key: 'RULES', decoration: null, content: { type: 'list', items: ['B'] } }],
};
mergeDocuments([doc1, doc2]);
expect(doc1.sections[0].content).toEqual({ type: 'list', items: ['A'] });
});
});

describe('estimateTokens', () => {
it('returns positive token count for non-empty document', () => {
const doc: AdfDocument = {
version: '0.1',
sections: [
{ key: 'RULES', decoration: null, content: { type: 'list', items: ['Use TypeScript', 'Follow conventions'] } },
],
};
const tokens = estimateTokens(doc);
expect(tokens).toBeGreaterThan(0);
});

it('returns 0 for empty document', () => {
const doc: AdfDocument = { version: '0.1', sections: [] };
expect(estimateTokens(doc)).toBe(0);
});
});
Loading