2026-05-10 23:12:26 +02:00
|
|
|
import { mkdtemp, rm } from 'node:fs/promises';
|
|
|
|
|
import { tmpdir } from 'node:os';
|
|
|
|
|
import { join } from 'node:path';
|
2026-05-10 23:51:24 +02:00
|
|
|
import { initKtxProject } from '@ktx/context/project';
|
2026-05-11 22:59:45 +02:00
|
|
|
import type { KtxEmbeddingPort } from '@ktx/context';
|
2026-05-10 23:12:26 +02:00
|
|
|
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
2026-05-10 23:51:24 +02:00
|
|
|
import { runKtxKnowledge } from './knowledge.js';
|
2026-05-10 23:12:26 +02:00
|
|
|
|
|
|
|
|
function makeIo() {
|
|
|
|
|
let stdout = '';
|
|
|
|
|
let stderr = '';
|
|
|
|
|
return {
|
|
|
|
|
io: {
|
|
|
|
|
stdout: {
|
|
|
|
|
write: (chunk: string) => {
|
|
|
|
|
stdout += chunk;
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
stderr: {
|
|
|
|
|
write: (chunk: string) => {
|
|
|
|
|
stderr += chunk;
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
stdout: () => stdout,
|
|
|
|
|
stderr: () => stderr,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-11 22:59:45 +02:00
|
|
|
class FakeEmbeddingPort implements KtxEmbeddingPort {
|
|
|
|
|
readonly maxBatchSize = 16;
|
|
|
|
|
|
|
|
|
|
async computeEmbedding(text: string): Promise<number[]> {
|
|
|
|
|
const lower = text.toLowerCase();
|
|
|
|
|
return lower.includes('revenue') || lower.includes('arr') ? [1, 0] : [0, 1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async computeEmbeddingsBulk(texts: string[]): Promise<number[][]> {
|
|
|
|
|
return Promise.all(texts.map((text) => this.computeEmbedding(text)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
describe('runKtxKnowledge', () => {
|
2026-05-10 23:12:26 +02:00
|
|
|
let tempDir: string;
|
|
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
2026-05-10 23:51:24 +02:00
|
|
|
tempDir = await mkdtemp(join(tmpdir(), 'ktx-cli-knowledge-'));
|
2026-05-10 23:12:26 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
|
await rm(tempDir, { recursive: true, force: true });
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-13 16:05:58 +02:00
|
|
|
it('writes, reads, lists, and searches wiki pages', async () => {
|
2026-05-10 23:12:26 +02:00
|
|
|
const projectDir = join(tempDir, 'project');
|
2026-05-14 17:39:31 +02:00
|
|
|
await initKtxProject({ projectDir });
|
2026-05-13 16:05:58 +02:00
|
|
|
|
|
|
|
|
const writeIo = makeIo();
|
|
|
|
|
await expect(
|
|
|
|
|
runKtxKnowledge(
|
|
|
|
|
{
|
|
|
|
|
command: 'write',
|
|
|
|
|
projectDir,
|
|
|
|
|
key: 'metrics-revenue',
|
|
|
|
|
scope: 'GLOBAL',
|
|
|
|
|
userId: 'local',
|
|
|
|
|
summary: 'Revenue',
|
|
|
|
|
content: 'Revenue is paid order value.',
|
|
|
|
|
tags: ['finance'],
|
|
|
|
|
refs: [],
|
|
|
|
|
slRefs: ['orders'],
|
|
|
|
|
},
|
|
|
|
|
writeIo.io,
|
|
|
|
|
),
|
|
|
|
|
).resolves.toBe(0);
|
|
|
|
|
expect(writeIo.stdout()).toContain('Wrote wiki/global/metrics-revenue.md');
|
|
|
|
|
|
|
|
|
|
const readIo = makeIo();
|
|
|
|
|
await expect(
|
|
|
|
|
runKtxKnowledge({ command: 'read', projectDir, key: 'metrics-revenue', userId: 'local' }, readIo.io),
|
|
|
|
|
).resolves.toBe(0);
|
|
|
|
|
expect(readIo.stdout()).toContain('# metrics-revenue');
|
|
|
|
|
expect(readIo.stdout()).toContain('Revenue is paid order value.');
|
2026-05-10 23:12:26 +02:00
|
|
|
|
|
|
|
|
const listIo = makeIo();
|
2026-05-10 23:51:24 +02:00
|
|
|
await expect(runKtxKnowledge({ command: 'list', projectDir, userId: 'local' }, listIo.io)).resolves.toBe(0);
|
2026-05-12 16:56:58 -04:00
|
|
|
expect(listIo.stdout()).toContain('GLOBAL\tmetrics-revenue\tRevenue');
|
2026-05-10 23:12:26 +02:00
|
|
|
|
|
|
|
|
const searchIo = makeIo();
|
|
|
|
|
await expect(
|
2026-05-10 23:51:24 +02:00
|
|
|
runKtxKnowledge({ command: 'search', projectDir, query: 'paid order', userId: 'local' }, searchIo.io),
|
2026-05-10 23:12:26 +02:00
|
|
|
).resolves.toBe(0);
|
2026-05-12 16:56:58 -04:00
|
|
|
expect(searchIo.stdout()).toContain('metrics-revenue');
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-13 16:05:58 +02:00
|
|
|
it('prints wiki list, search, and read as public JSON envelopes', async () => {
|
2026-05-13 13:01:56 +02:00
|
|
|
const projectDir = join(tempDir, 'project');
|
2026-05-14 17:39:31 +02:00
|
|
|
await initKtxProject({ projectDir });
|
2026-05-13 16:05:58 +02:00
|
|
|
|
|
|
|
|
await expect(
|
|
|
|
|
runKtxKnowledge(
|
|
|
|
|
{
|
|
|
|
|
command: 'write',
|
|
|
|
|
projectDir,
|
|
|
|
|
key: 'metrics-revenue',
|
|
|
|
|
scope: 'GLOBAL',
|
|
|
|
|
userId: 'local',
|
|
|
|
|
summary: 'Revenue',
|
|
|
|
|
content: 'Revenue is paid order value.',
|
|
|
|
|
tags: ['finance'],
|
|
|
|
|
refs: [],
|
|
|
|
|
slRefs: ['orders'],
|
|
|
|
|
},
|
|
|
|
|
makeIo().io,
|
|
|
|
|
),
|
|
|
|
|
).resolves.toBe(0);
|
2026-05-13 13:01:56 +02:00
|
|
|
|
|
|
|
|
const listIo = makeIo();
|
|
|
|
|
await expect(runKtxKnowledge({ command: 'list', projectDir, userId: 'local', json: true }, listIo.io)).resolves.toBe(
|
|
|
|
|
0,
|
|
|
|
|
);
|
|
|
|
|
expect(JSON.parse(listIo.stdout())).toMatchObject({
|
|
|
|
|
kind: 'list',
|
|
|
|
|
data: { items: [expect.objectContaining({ key: 'metrics-revenue', summary: 'Revenue' })] },
|
|
|
|
|
meta: { command: 'wiki list' },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const searchIo = makeIo();
|
|
|
|
|
await expect(
|
|
|
|
|
runKtxKnowledge(
|
|
|
|
|
{ command: 'search', projectDir, query: 'paid order', userId: 'local', json: true, limit: 5 },
|
|
|
|
|
searchIo.io,
|
|
|
|
|
),
|
|
|
|
|
).resolves.toBe(0);
|
|
|
|
|
expect(JSON.parse(searchIo.stdout())).toMatchObject({
|
|
|
|
|
kind: 'list',
|
|
|
|
|
data: { items: [expect.objectContaining({ key: 'metrics-revenue', summary: 'Revenue' })] },
|
|
|
|
|
meta: { command: 'wiki search' },
|
|
|
|
|
});
|
2026-05-13 16:05:58 +02:00
|
|
|
|
|
|
|
|
const readIo = makeIo();
|
|
|
|
|
await expect(
|
|
|
|
|
runKtxKnowledge({ command: 'read', projectDir, key: 'metrics-revenue', userId: 'local', json: true }, readIo.io),
|
|
|
|
|
).resolves.toBe(0);
|
|
|
|
|
expect(JSON.parse(readIo.stdout())).toMatchObject({
|
|
|
|
|
kind: 'wiki.page',
|
|
|
|
|
data: {
|
|
|
|
|
key: 'metrics-revenue',
|
|
|
|
|
summary: 'Revenue',
|
|
|
|
|
content: 'Revenue is paid order value.',
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('rejects slash-delimited write keys with a flat-key suggestion', async () => {
|
|
|
|
|
const projectDir = join(tempDir, 'project');
|
2026-05-14 17:39:31 +02:00
|
|
|
await initKtxProject({ projectDir });
|
2026-05-13 16:05:58 +02:00
|
|
|
|
|
|
|
|
const writeIo = makeIo();
|
|
|
|
|
await expect(
|
|
|
|
|
runKtxKnowledge(
|
|
|
|
|
{
|
|
|
|
|
command: 'write',
|
|
|
|
|
projectDir,
|
|
|
|
|
key: 'orbit/company-overview',
|
|
|
|
|
scope: 'GLOBAL',
|
|
|
|
|
userId: 'local',
|
|
|
|
|
summary: 'Orbit',
|
|
|
|
|
content: 'Orbit overview.',
|
|
|
|
|
tags: [],
|
|
|
|
|
refs: [],
|
|
|
|
|
slRefs: [],
|
|
|
|
|
},
|
|
|
|
|
writeIo.io,
|
|
|
|
|
),
|
|
|
|
|
).resolves.toBe(1);
|
|
|
|
|
|
|
|
|
|
expect(writeIo.stderr()).toContain(
|
|
|
|
|
'Invalid wiki key "orbit/company-overview". Wiki keys must be flat; use "orbit-company-overview".',
|
|
|
|
|
);
|
|
|
|
|
expect(writeIo.stdout()).toBe('');
|
2026-05-10 23:12:26 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('explains empty search results for a project without wiki pages', async () => {
|
|
|
|
|
const projectDir = join(tempDir, 'empty-project');
|
2026-05-14 17:39:31 +02:00
|
|
|
await initKtxProject({ projectDir });
|
2026-05-10 23:12:26 +02:00
|
|
|
|
|
|
|
|
const searchIo = makeIo();
|
|
|
|
|
await expect(
|
2026-05-10 23:51:24 +02:00
|
|
|
runKtxKnowledge({ command: 'search', projectDir, query: 'revenue', userId: 'local' }, searchIo.io),
|
2026-05-10 23:12:26 +02:00
|
|
|
).resolves.toBe(0);
|
|
|
|
|
|
|
|
|
|
expect(searchIo.stdout()).toBe('');
|
|
|
|
|
expect(searchIo.stderr()).toContain('No local wiki pages found');
|
2026-05-14 01:43:06 +02:00
|
|
|
expect(searchIo.stderr()).toContain('ktx ingest <connectionId>');
|
2026-05-10 23:12:26 +02:00
|
|
|
});
|
2026-05-11 22:59:45 +02:00
|
|
|
|
|
|
|
|
it('uses configured embeddings for semantic wiki search', async () => {
|
|
|
|
|
const projectDir = join(tempDir, 'semantic-project');
|
2026-05-14 17:39:31 +02:00
|
|
|
await initKtxProject({ projectDir });
|
2026-05-13 16:05:58 +02:00
|
|
|
|
|
|
|
|
await expect(
|
|
|
|
|
runKtxKnowledge(
|
|
|
|
|
{
|
|
|
|
|
command: 'write',
|
|
|
|
|
projectDir,
|
|
|
|
|
key: 'active-contract-arr-open-tickets',
|
|
|
|
|
scope: 'GLOBAL',
|
|
|
|
|
userId: 'local',
|
|
|
|
|
summary: 'Active Contract ARR Ranked by Open Support Ticket Count',
|
|
|
|
|
content: 'Accounts ranked by annual recurring contract value and support ticket load.',
|
|
|
|
|
tags: ['historic-sql'],
|
|
|
|
|
refs: [],
|
|
|
|
|
slRefs: [],
|
|
|
|
|
},
|
|
|
|
|
makeIo().io,
|
|
|
|
|
),
|
|
|
|
|
).resolves.toBe(0);
|
2026-05-11 22:59:45 +02:00
|
|
|
|
|
|
|
|
const searchIo = makeIo();
|
|
|
|
|
await expect(
|
|
|
|
|
runKtxKnowledge(
|
|
|
|
|
{ command: 'search', projectDir, query: 'revenue', userId: 'local' },
|
|
|
|
|
searchIo.io,
|
|
|
|
|
{ embeddingService: new FakeEmbeddingPort() },
|
|
|
|
|
),
|
|
|
|
|
).resolves.toBe(0);
|
|
|
|
|
|
2026-05-12 16:56:58 -04:00
|
|
|
expect(searchIo.stdout()).toContain('active-contract-arr-open-tickets');
|
2026-05-11 22:59:45 +02:00
|
|
|
expect(searchIo.stderr()).toBe('');
|
|
|
|
|
});
|
2026-05-10 23:12:26 +02:00
|
|
|
});
|