2026-05-10 23:12:26 +02:00
|
|
|
import { mkdtemp, readFile, rm, stat } from 'node:fs/promises';
|
|
|
|
|
import { tmpdir } from 'node:os';
|
|
|
|
|
import { join } from 'node:path';
|
|
|
|
|
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
2026-05-10 23:51:24 +02:00
|
|
|
import { initKtxProject, loadKtxProject } from './project.js';
|
2026-05-10 23:12:26 +02:00
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
describe('KTX local project runtime', () => {
|
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-project-runtime-'));
|
2026-05-10 23:12:26 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
|
await rm(tempDir, { recursive: true, force: true });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('initializes the standalone project layout and commits it', async () => {
|
|
|
|
|
const projectDir = join(tempDir, 'warehouse');
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
const result = await initKtxProject({
|
2026-05-10 23:12:26 +02:00
|
|
|
projectDir,
|
|
|
|
|
projectName: 'warehouse',
|
|
|
|
|
authorName: 'Agent',
|
|
|
|
|
authorEmail: 'agent@example.com',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(result.projectDir).toBe(projectDir);
|
|
|
|
|
expect(result.config.project).toBe('warehouse');
|
|
|
|
|
expect(result.commitHash).toMatch(/^[0-9a-f]{40}$/);
|
2026-05-10 23:51:24 +02:00
|
|
|
await expect(readFile(join(projectDir, 'ktx.yaml'), 'utf-8')).resolves.toContain('project: warehouse');
|
|
|
|
|
const gitignore = await readFile(join(projectDir, '.ktx/.gitignore'), 'utf-8');
|
2026-05-10 23:12:26 +02:00
|
|
|
expect(gitignore).toContain('cache/');
|
|
|
|
|
expect(gitignore).toContain('db.sqlite');
|
2026-05-11 00:31:15 -07:00
|
|
|
expect(gitignore).toContain('db.sqlite-*');
|
|
|
|
|
expect(gitignore).toContain('ingest-transcripts/');
|
2026-05-10 23:12:26 +02:00
|
|
|
expect(gitignore).toContain('secrets/');
|
|
|
|
|
expect(gitignore).toContain('setup/');
|
|
|
|
|
expect(gitignore).toContain('agents/');
|
|
|
|
|
await expect(stat(join(projectDir, 'knowledge/global/.gitkeep'))).resolves.toBeDefined();
|
|
|
|
|
await expect(stat(join(projectDir, 'semantic-layer/.gitkeep'))).resolves.toBeDefined();
|
|
|
|
|
await expect(stat(join(projectDir, '_schema/.gitkeep'))).rejects.toMatchObject({ code: 'ENOENT' });
|
|
|
|
|
await expect(stat(join(projectDir, 'raw-sources/.gitkeep'))).resolves.toBeDefined();
|
|
|
|
|
await expect(stat(join(projectDir, '.git'))).resolves.toBeDefined();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('loads an initialized project with a working file store', async () => {
|
|
|
|
|
const projectDir = join(tempDir, 'warehouse');
|
2026-05-10 23:51:24 +02:00
|
|
|
await initKtxProject({ projectDir, projectName: 'warehouse' });
|
2026-05-10 23:12:26 +02:00
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
const loaded = await loadKtxProject({ projectDir });
|
2026-05-10 23:12:26 +02:00
|
|
|
await loaded.fileStore.writeFile(
|
|
|
|
|
'knowledge/global/revenue.md',
|
|
|
|
|
'# Revenue\n',
|
|
|
|
|
'Agent',
|
|
|
|
|
'agent@example.com',
|
|
|
|
|
'Add revenue page',
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(loaded.config.project).toBe('warehouse');
|
|
|
|
|
await expect(loaded.fileStore.readFile('knowledge/global/revenue.md')).resolves.toMatchObject({
|
|
|
|
|
content: '# Revenue\n',
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('rejects reinitializing an existing project unless force is set', async () => {
|
|
|
|
|
const projectDir = join(tempDir, 'warehouse');
|
2026-05-10 23:51:24 +02:00
|
|
|
await initKtxProject({ projectDir, projectName: 'warehouse' });
|
2026-05-10 23:12:26 +02:00
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
await expect(initKtxProject({ projectDir, projectName: 'warehouse' })).rejects.toThrow(
|
|
|
|
|
'Project already contains ktx.yaml',
|
2026-05-10 23:12:26 +02:00
|
|
|
);
|
|
|
|
|
|
2026-05-10 23:51:24 +02:00
|
|
|
await expect(initKtxProject({ projectDir, projectName: 'warehouse-v2', force: true })).resolves.toMatchObject({
|
2026-05-10 23:12:26 +02:00
|
|
|
config: {
|
|
|
|
|
project: 'warehouse-v2',
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|