import { mkdirSync, mkdtempSync, readFileSync, writeFileSync } from 'node:fs'; import os from 'node:os'; import path from 'node:path'; export function tempDir(prefix = 'bidx-') { return mkdtempSync(path.join(os.tmpdir(), prefix)); } /** Minimal card writer producing the tracker's own on-disk format. */ export function writeCard(backlogDir, spec) { const lines = [ '---', `id: ${spec.id}`, `title: ${spec.title}`, `status: ${spec.status ?? 'To Do'}`, 'assignee: []', `created_date: '${spec.createdDate ?? '2026-11-30T00:00:00Z'}'`, `labels: [${(spec.labels ?? []).map((label) => JSON.stringify(label)).join(', ')}]`, `milestone: ${spec.milestone ?? 'm-1'}`, `parent_task_id: ${spec.parent ?? 'none'}`, `dependencies: [${(spec.dependencies ?? []).join(', ')}]`, `priority: ${spec.priority ?? 'medium'}`, `project: ${spec.project ?? 'lab'}`, `ordinal: ${spec.ordinal ?? 1000}`, ...(spec.type === undefined ? [] : [`type: ${spec.type}`]), '---', '', '## Description', '', '', spec.description ?? '', '', '', ]; if (spec.acceptanceCriteria !== undefined && spec.acceptanceCriteria.length > 0) { lines.push('## Acceptance Criteria', ''); spec.acceptanceCriteria.forEach((item, index) => { const checked = item.checked === true ? 'x' : ' '; lines.push(`- [${checked}] #${String(index + 1)} ${item.text}`); }); lines.push('', ''); } lines.push('## Definition of Done', ''); for (const item of spec.definitionOfDone ?? ['Общая Definition of Done лаборатории']) { lines.push(`- [ ] #1 ${item}`); } lines.push('', ''); if (spec.comments !== undefined) { lines.push( '## Comments', '', spec.comments, '', '', ); } if (spec.notes !== undefined) { lines.push( '## Implementation Notes', '', spec.notes, '', '', ); } const directory = spec.archived === true ? path.join(backlogDir, 'archive', 'tasks') : path.join(backlogDir, 'tasks'); mkdirSync(directory, { recursive: true }); const file = path.join(directory, `${spec.id.toLowerCase()}.md`); writeFileSync(file, lines.join('\n'), 'utf8'); return file; } export function writeRawCard(backlogDir, id, text, archived = false) { const directory = archived ? path.join(backlogDir, 'archive', 'tasks') : path.join(backlogDir, 'tasks'); mkdirSync(directory, { recursive: true }); const file = path.join(directory, `${id.toLowerCase()}.md`); writeFileSync(file, text, 'utf8'); return file; } export function readText(file) { return readFileSync(file, 'utf8'); } export function makeConfig(root, options = {}) { const config = { repoRoot: root, backlogDir: path.join(root, 'backlog'), indexFile: path.join(root, 'index.sqlite'), topK: options.topK ?? 10, provider: { kind: options.providerKind ?? 'mock', endpoint: options.endpoint ?? '', model: options.model ?? 'mock-hash-v1', dimensions: options.dimensions ?? 64, batchSize: options.batchSize ?? 4, timeoutMs: 1000, retries: options.retries ?? 2, concurrency: options.concurrency ?? 2, retryDelayMs: 1, apiKeySources: options.apiKeySources ?? [{ kind: 'env', name: 'BIDX_TEST_KEY' }], }, thresholds: options.thresholds ?? { duplicate: 0.9, overlap: 0.8, related: 0.7 }, statusClasses: options.statusClasses ?? { active: ['To Do', 'In Progress'], done: ['Done'], skipped: [], }, }; const file = path.join(root, 'config.json'); writeFileSync(file, JSON.stringify(config, null, 2), 'utf8'); return { configPath: file, config }; } export function fakeTransport(handler) { const calls = []; let inFlight = 0; let maxInFlight = 0; const transport = async (request) => { calls.push(request); inFlight += 1; maxInFlight = Math.max(maxInFlight, inFlight); try { return await handler(request, calls.length); } finally { inFlight -= 1; } }; return { transport, calls, get maxInFlight() { return maxInFlight; }, }; } export function embeddingsResponse(vectors, model = 'test-model') { return { status: 200, body: JSON.stringify({ model, data: vectors.map((vector, index) => ({ index, embedding: vector })), usage: { prompt_tokens: 10 }, }), }; } export function vector(seed, dimensions) { const out = []; let value = seed; for (let index = 0; index < dimensions; index += 1) { value = (value * 1103515245 + 12345) % 2147483648; out.push((value % 1000) / 1000 - 0.5); } const norm = Math.sqrt(out.reduce((sum, item) => sum + item * item, 0)); return out.map((item) => item / norm); }