import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import path from 'node:path'; import { fixture, launchWorkbench } from './helpers.mjs'; const content = value => { const { exportedAt, ...record } = value; assert.ok(Number.isFinite(Date.parse(exportedAt))); return record; }; test('cache controls require desktop authority, exact inputs and owned project identity while preserving task evidence', async t => { const f = fixture(t); fs.writeFileSync(path.join(f.projectRoot, 'main.ts'), 'export const current = 1;\n'); const host = await launchWorkbench(t, f.data), route = '/projects/' + f.project.id; const before = content(await host.api('/sessions/' + f.session.id + '/export')), config = (await host.api('/bootstrap')).config; const source = fs.readFileSync(path.join(f.projectRoot, 'main.ts')); assert.equal((await fetch(host.url + '/api/project-cache', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: '{"enabled":false}' })).status, 403); assert.equal((await fetch(host.url + '/api' + route + '/index-cache', { method: 'DELETE', headers: { 'Content-Type': 'application/json' }, body: '{}' })).status, 403); await assert.rejects(host.api('/project-cache', { enabled: true, path: f.root }), { status: 400 }); await assert.rejects(host.api('/project-cache', { enabled: 'yes' }), { status: 400 }); await assert.rejects(host.api('/projects/00000000-0000-0000-0000-000000000000/index-cache'), { status: 404 }); const first = await host.api(route + '/code-index'); assert.equal(first.cache.parsedFiles, 1); const query = await host.api(route + '/code-query?query=current&kind=symbols'); assert.equal(query.index.cache.parsedFiles, 0); assert.equal(query.results[0].name, 'current'); const diagnostics = await host.api('/diagnostics?session=' + f.session.id); assert.equal(diagnostics.projectCache.latest.sourceVerified, true); assert.equal(diagnostics.projectCache.latest.checkedAt, query.index.cache.checkedAt, 'diagnostics does not perform another query'); assert.equal((await host.api('/project-cache', { enabled: false })).enabled, false); assert.equal((await host.api(route + '/code-index')).cache.parsedFiles, 1); const clear = await fetch(host.url + '/api' + route + '/index-cache', { method: 'DELETE', headers: { 'Content-Type': 'application/json', 'X-Workbench-Token': host.bootstrap.token }, body: '{}' }); assert.equal(clear.status, 200); assert.equal((await clear.json()).removedProjects, 1); assert.equal((await host.api(route + '/index-cache')).diskProjects, 0); assert.deepEqual(content(await host.api('/sessions/' + f.session.id + '/export')), before); assert.deepEqual((await host.api('/bootstrap')).config, config); assert.deepEqual(fs.readFileSync(path.join(f.projectRoot, 'main.ts')), source); }); test('cache enablement and derived reuse survive a real host restart without becoming task recovery state', async t => { const f = fixture(t); fs.writeFileSync(path.join(f.projectRoot, 'main.py'), 'def candidate():\n return 1\n'); const first = await launchWorkbench(t, f.data), route = '/projects/' + f.project.id; const indexed = await first.api(route + '/code-index'); await first.api('/project-cache', { enabled: false }); await first.close(); const second = await launchWorkbench(t, f.data); assert.equal((await second.api('/project-cache')).enabled, false); await second.api('/project-cache', { enabled: true }); const reused = await second.api(route + '/code-index'); assert.equal(reused.cache.origin, 'disk'); assert.equal(reused.cache.parsedFiles, 0); assert.equal(reused.indexFingerprint, indexed.indexFingerprint); assert.equal((await second.api('/sessions/' + f.session.id)).status, 'idle'); });