Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
/**
* Behavioral cover for the Data Manager's per-item purge (issue #3327).
*
* Every case here runs against a throwaway temp root — `PATHS.data` is
* redirected before `dataManager.js` captures it, so nothing in this file can
* reach the install's real `data/` directory, which holds the only copy of the
* media these categories hold.
*/
import { describe, it, expect, beforeEach, afterAll, vi } from 'vitest';
import { mkdtempSync, rmSync, mkdirSync, writeFileSync, existsSync, symlinkSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';
import { makePathsProxy } from '../lib/mockPathsDataRoot.js';
const TEST_DATA_ROOT = mkdtempSync(join(tmpdir(), 'datamanager-purge-test-'));
vi.mock('../lib/fileUtils.js', async (importOriginal) => {
const actual = await importOriginal();
return makePathsProxy(actual, { dataRoot: TEST_DATA_ROOT });
});
vi.mock('../lib/db.js', () => ({ query: vi.fn() }));
const { query } = await import('../lib/db.js');
const { purgeCategory, getCategoryDetail } = await import('./dataManager.js');
afterAll(() => rmSync(TEST_DATA_ROOT, { recursive: true, force: true }));
const dataPath = (...parts) => join(TEST_DATA_ROOT, ...parts);
beforeEach(() => {
rmSync(TEST_DATA_ROOT, { recursive: true, force: true });
// `images` is item-scoped: a flat directory of assets plus one scratch dir.
mkdirSync(dataPath('images', '.scratch'), { recursive: true });
writeFileSync(dataPath('images', 'render-0001.png'), 'png');
writeFileSync(dataPath('images', 'render-0002.png'), 'png');
writeFileSync(dataPath('images', '.scratch', 'work.tmp'), 'tmp');
// `screenshots` stays category-scoped (and deletable: messages/review are not).
mkdirSync(dataPath('screenshots', 'account'), { recursive: true });
writeFileSync(dataPath('screenshots', 'index.json'), '{}');
});
describe('purgeCategory — item-scoped categories (#3327)', () => {
it('removes exactly the named file and leaves the rest of the category', async () => {
const result = await purgeCategory('images', { subPath: 'render-0001.png' });
expect(result).toEqual({ category: 'images', subPath: 'render-0001.png' });
expect(existsSync(dataPath('images', 'render-0001.png'))).toBe(false);
expect(existsSync(dataPath('images', 'render-0002.png'))).toBe(true);
});
it('refuses a directory entry — those hold other features working state', async () => {
await expect(purgeCategory('images', { subPath: '.scratch' })).rejects.toThrow(/only removes files/);
expect(existsSync(dataPath('images', '.scratch', 'work.tmp'))).toBe(true);
});
it('404s a subPath that does not exist instead of silently reporting success', async () => {
await expect(purgeCategory('images', { subPath: 'never-existed.png' })).rejects.toThrow(/Item not found/);
});
it('refuses a nested subPath, so only listed entries are reachable', async () => {
await expect(purgeCategory('images', { subPath: '.scratch/work.tmp' })).rejects.toThrow(/single entry/);
expect(existsSync(dataPath('images', '.scratch', 'work.tmp'))).toBe(true);
});
it('cannot follow an intermediate symlink out of the category', async () => {
mkdirSync(dataPath('elsewhere'), { recursive: true });
writeFileSync(dataPath('elsewhere', 'keepsake.png'), 'png');
symlinkSync(dataPath('elsewhere'), dataPath('images', 'link'));
// The lexical resolve/relative containment check alone would pass this —
// it never touches the filesystem, so it cannot see the symlink hop.
await expect(purgeCategory('images', { subPath: 'link/keepsake.png' })).rejects.toThrow(/single entry/);
expect(existsSync(dataPath('elsewhere', 'keepsake.png'))).toBe(true);
});
it('unlinks a symlinked entry itself without touching what it points at', async () => {
mkdirSync(dataPath('elsewhere'), { recursive: true });
writeFileSync(dataPath('elsewhere', 'keepsake.png'), 'png');
symlinkSync(dataPath('elsewhere', 'keepsake.png'), dataPath('images', 'alias.png'));
await purgeCategory('images', { subPath: 'alias.png' });
expect(existsSync(dataPath('images', 'alias.png'))).toBe(false);
expect(existsSync(dataPath('elsewhere', 'keepsake.png'))).toBe(true);
});
it('still refuses the whole-category form even though the directory exists', async () => {
await expect(purgeCategory('images')).rejects.toThrow(/only supports per-item purge/);
expect(existsSync(dataPath('images', 'render-0001.png'))).toBe(true);
});
});
describe('purgeCategory — category-scoped categories keep their behavior', () => {
it('empties the directory when no subPath is given', async () => {
const result = await purgeCategory('screenshots');
expect(result).toEqual({ category: 'screenshots', subPath: null });
expect(existsSync(dataPath('screenshots', 'index.json'))).toBe(false);
expect(existsSync(dataPath('screenshots', 'account'))).toBe(false);
expect(existsSync(dataPath('screenshots'))).toBe(true);
});
it('preserves Mind files during bulk purge and refuses explicit deletion of their reserved entries', async () => {
const owned = ['mind-claimed-photo.png', 'mind-unindexed-photo.png', '.mind-pending-unindexed'];
for (const name of owned) writeFileSync(dataPath('screenshots', name), 'owned');
await purgeCategory('screenshots');
expect(existsSync(dataPath('screenshots', 'index.json'))).toBe(false);
for (const name of owned) {
expect(existsSync(dataPath('screenshots', name))).toBe(true);
await expect(purgeCategory('screenshots', { subPath: name }))
.rejects.toMatchObject({ status: 409, code: 'SCREENSHOT_OWNED_BY_MIND' });
await expect(purgeCategory('screenshots', { subPath: name.toUpperCase() }))
.rejects.toMatchObject({ status: 409, code: 'SCREENSHOT_OWNED_BY_MIND' });
expect(existsSync(dataPath('screenshots', name))).toBe(true);
}
});
it('removes a named subdirectory recursively', async () => {
writeFileSync(dataPath('screenshots', 'account', 'inbox.json'), '{}');
await purgeCategory('screenshots', { subPath: 'account' });
expect(existsSync(dataPath('screenshots', 'account'))).toBe(false);
expect(existsSync(dataPath('screenshots', 'index.json'))).toBe(true);
});
// A caller that meant to name an entry and produced an empty string must not
// land in the branch that empties the whole directory.
it('400s an empty subPath instead of widening it into a whole-category wipe', async () => {
await expect(purgeCategory('screenshots', { subPath: '' })).rejects.toThrow(/single entry/);
expect(existsSync(dataPath('screenshots', 'index.json'))).toBe(true);
});
it('still refuses a nested subPath', async () => {
await expect(purgeCategory('screenshots', { subPath: '../images/render-0001.png' })).rejects.toThrow(/single entry/);
expect(existsSync(dataPath('images', 'render-0001.png'))).toBe(true);
});
});
describe('training-run source details', () => {
it('resolves stored run names and dataset links while retaining orphan folders', async () => {
for (const id of ['example-run', 'orphan-run']) {
mkdirSync(dataPath('training-runs', id), { recursive: true });
writeFileSync(dataPath('training-runs', id, 'checkpoint.bin'), 'checkpoint');
}
query.mockResolvedValue({ rows: [{ data: {
id: 'example-run', name: 'Example portrait study', datasetId: 'example-dataset',
baseModelId: 'example-model', status: 'completed',
} }] });
const detail = await getCategoryDetail('training-runs');
expect(detail.items.find(item => item.name === 'example-run')).toMatchObject({
label: 'Example portrait study', sourceHref: '/models/training/example-dataset',
description: 'example-model · completed',
});
expect(detail.items.find(item => item.name === 'orphan-run')).toMatchObject({ sourceUnavailable: 'Run record no longer available' });
await expect(purgeCategory('training-runs', { subPath: 'example-run' })).rejects.toMatchObject({ status: 403, code: 'CATEGORY_NOT_PURGEABLE' });
expect(existsSync(dataPath('training-runs', 'example-run'))).toBe(true);
expect(existsSync(dataPath('training-runs', 'orphan-run', 'checkpoint.bin'))).toBe(true);
});
it('keeps storage details usable when source lookup fails', async () => {
mkdirSync(dataPath('training-runs', 'example-run'), { recursive: true });
query.mockRejectedValue(new Error('database unavailable'));
const detail = await getCategoryDetail('training-runs');
expect(detail.items[0]).toMatchObject({ name: 'example-run', sourceUnavailable: 'Source lookup unavailable' });
expect(detail.items[0].sourceHref).toBeUndefined();
});
});
// data/backup/ is shared with the backup service's own state (state.json) and
// the staging directory an in-flight archive writes into. A category-wide purge
// must take the archives and nothing else (#11864).
describe('purgeCategory — backup keeps the service state (#11864)', () => {
it('removes only archive files and leaves state.json and staging intact', async () => {
const archiveA = 'images-2026-10-01T10-00-00.tar.gz';
const archiveB = 'images-2026-10-02T10-00-00-0123abcd-1234-1234-1234-123456789abc.tar.gz';
mkdirSync(dataPath('backup', '.archive-inflight'), { recursive: true });
writeFileSync(dataPath('backup', '.archive-inflight', 'partial.tar.gz'), 'partial');
writeFileSync(dataPath('backup', 'state.json'), '{"lastRun":"2026-10-01"}');
writeFileSync(dataPath('backup', archiveA), 'a');
writeFileSync(dataPath('backup', archiveB), 'b');
const result = await purgeCategory('backup');
expect(result).toEqual({ category: 'backup', subPath: null });
expect(existsSync(dataPath('backup', archiveA))).toBe(false);
expect(existsSync(dataPath('backup', archiveB))).toBe(false);
expect(existsSync(dataPath('backup', 'state.json'))).toBe(true);
expect(existsSync(dataPath('backup', '.archive-inflight', 'partial.tar.gz'))).toBe(true);
});
});