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
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest';
import { mkdir, readFile, rm, writeFile } from 'fs/promises';
import { join } from 'path';
import { mockPathsDataRoot } from '../lib/mockPathsDataRoot.js';
const { tempRoot, makeProxy, cleanup } = mockPathsDataRoot({ prefix: 'portos-feedback-' });
vi.mock('../lib/fileUtils.js', async () => {
const actual = await vi.importActual('../lib/fileUtils.js');
return makeProxy(actual);
});
vi.mock('./digital-twin.js', () => ({ digitalTwinEvents: { emit: vi.fn() } }));
vi.mock('../lib/backupSnapshotBoundary.js', () => ({ withBackupAssetPublication: work => work() }));
const FILE = join(tempRoot, 'digital-twin', 'feedback.json');
const entry = (id, createdAt, validation = 'sounds_like_me') => ({ id, validation, createdAt, contentType: 'enrichment', documentsUsed: [] });
const doc = (entries, updatedAt) => ({
version: '1.0.0', entries, updatedAt,
stats: { totalFeedback: entries.length, soundsLikeMe: entries.length, doesntSoundLikeMe: 0 },
documentWeightAdjustments: {}, lastCalculatedAt: null,
});
const readDisk = async () => JSON.parse(await readFile(FILE, 'utf-8'));
let feedback;
let sync;
beforeEach(async () => {
vi.resetModules();
await rm(join(tempRoot, 'digital-twin'), { recursive: true, force: true });
await mkdir(join(tempRoot, 'digital-twin'), { recursive: true });
feedback = await import('./feedbackLoop.js');
sync = await import('./digital-twin-sync.js');
});
afterAll(cleanup);
const merge = async (remote) => (await sync.applyDigitalTwinRemote({ feedback: remote })).count > 0;
describe('document attribution keyed by stored strings', () => {
it('recalculates weights without writing a __proto__ document key onto Object.prototype', async () => {
const entries = Array.from({ length: 5 }, (_, i) => ({
...entry(`e${i}`, `2026-01-0${i + 1}T00:00:00.000Z`, 'sounds_like_me'),
documentsUsed: ['__proto__'],
}));
await writeFile(FILE, JSON.stringify(doc(entries, '2026-01-06T00:00:00.000Z')));
const result = await feedback.recalculateWeights();
expect(result.adjustments).toEqual({});
expect(Object.prototype).not.toHaveProperty('total');
expect(Object.prototype).not.toHaveProperty('positive');
});
});
describe('feedback peer merge vs local submit', () => {
it('keeps synced entries when a local submit follows a peer merge (stale cache)', async () => {
await feedback.submitFeedback({ contentType: 'enrichment', validation: 'sounds_like_me', contentSnippet: 'a' }); // primes cache
expect(await merge(doc([entry('remote-1', '2026-01-01T00:00:00.000Z')], '2026-01-01T00:00:00.000Z'))).toBe(true);
await feedback.submitFeedback({ contentType: 'enrichment', validation: 'not_quite', contentSnippet: 'b' });
const ids = (await readDisk()).entries.map((e) => e.id);
expect(ids).toContain('remote-1');
expect(ids).toHaveLength(3);
expect((await feedback.getRecentFeedback()).length).toBe(3);
});
it('converges to the union when both machines recorded feedback between syncs', async () => {
const a = doc([entry('shared', '2026-01-01T00:00:00.000Z'), entry('a-only', '2026-01-02T00:00:00.000Z')], '2026-01-03T00:00:00.000Z');
const b = doc([entry('shared', '2026-01-01T00:00:00.000Z'), entry('b-only', '2026-01-02T12:00:00.000Z')], '2026-01-02T12:00:00.000Z');
await writeFile(FILE, JSON.stringify(a));
await merge(b);
const onA = (await readDisk()).entries.map((e) => e.id).sort();
await writeFile(FILE, JSON.stringify(b));
await merge(a);
const onB = (await readDisk()).entries.map((e) => e.id).sort();
expect(onA).toEqual(['a-only', 'b-only', 'shared']);
expect(onB).toEqual(onA);
});
it('does not rewrite when the remote adds nothing', async () => {
const a = doc([entry('x', '2026-01-01T00:00:00.000Z')], '2026-01-03T00:00:00.000Z');
await writeFile(FILE, JSON.stringify(a));
expect(await merge(doc([entry('x', '2026-01-01T00:00:00.000Z')], '2026-01-04T00:00:00.000Z'))).toBe(false);
});
it('refuses to overwrite a malformed feedback.json on merge or submit', async () => {
await writeFile(FILE, '{ not json');
await expect(merge(doc([entry('r', '2026-01-01T00:00:00.000Z')], 'x'))).rejects.toThrow();
await expect(feedback.submitFeedback({ contentType: 'enrichment', validation: 'sounds_like_me', contentSnippet: 'c' })).rejects.toThrow();
expect(await readFile(FILE, 'utf-8')).toBe('{ not json');
});
it('initializes a missing file on first submit', async () => {
await feedback.submitFeedback({ contentType: 'enrichment', validation: 'sounds_like_me', contentSnippet: 'c' });
expect((await readDisk()).stats.totalFeedback).toBe(1);
});
});