Skip to content

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

4.0 KB
Raw Blame
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest';
import { writeFileSync } from 'fs';
import { join } from 'path';
import express from 'express';
import { request } from '../lib/testHelper.js';
import { mockPathsDataRoot } from '../lib/mockPathsDataRoot.js';

const { tempRoot, makeProxy, cleanup } = mockPathsDataRoot({ prefix: 'portos-peer-sync-auth-' });

vi.mock('../lib/fileUtils.js', async () => {
  const actual = await vi.importActual('../lib/fileUtils.js');
  return makeProxy(actual);
});

vi.mock('../../lib/portosAuthCore.js', async () => {
  const actual = await vi.importActual('../../lib/portosAuthCore.js');
  const testParams = { N: 1024, r: 8, p: 1, maxmem: 8 * 1024 * 1024 };
  const hashPassword = (password, salt) =>
    actual.__hashPasswordWithParamsForTests(password, salt, testParams);
  return {
    ...actual,
    hashPassword,
    verifyPasswordAgainst: async (auth, password) => {
      if (!auth?.enabled || !auth.passwordHash || !auth.salt || typeof password !== 'string' || password.length === 0) return false;
      return actual.constantEqual(await hashPassword(password, auth.salt), auth.passwordHash);
    },
  };
});

vi.mock('../services/sharing/peerSync.js', () => ({
  listPeerSubscriptions: vi.fn(),
  subscribePeer: vi.fn(),
  unsubscribePeer: vi.fn(),
  applyIncomingPush: vi.fn(),
  forcePushRecord: vi.fn(),
  getRecordPayloadForPeer: vi.fn(),
  pullRecordFromPeer: vi.fn(),
  syncNowForPeer: vi.fn(),
  buildMediaLibraryManifest: vi.fn(),
  buildCosHistoryManifest: vi.fn(),
  buildCosTasksPayload: vi.fn(),
  ERR_NOT_FOUND: 'PEER_SYNC_SUBSCRIPTION_NOT_FOUND',
  ERR_VALIDATION: 'PEER_SYNC_SUBSCRIPTION_VALIDATION',
  ERR_SCHEMA_VERSION_AHEAD: 'PEER_SYNC_SCHEMA_VERSION_AHEAD',
  PEER_SUBSCRIBABLE_KINDS: Object.freeze(['universe', 'series', 'mediaCollection']),
}));

const resetSettings = () => {
  writeFileSync(join(tempRoot, 'settings.json'), '{}\n');
  writeFileSync(join(tempRoot, 'auth-sessions.json'), '{"tokens":[]}\n');
};

// Pay the route graph's cold transform+import at FILE SCOPE, before any test is
// registered (#7951). `beforeEach` calls `vi.resetModules()` so each test builds
// a fresh app, but only the FIRST load in the worker transforms the graph; the
// re-imports after it are sub-second. Left inside the test, that first load is
// charged against the 10s `testTimeout` and the file failed with "Test timed out
// in 10000ms" under a full-suite run while asserting nothing slow. Module
// collection is not budgeted, so warming it here leaves the tests paying only
// the cheap re-import. These instances are intentionally discarded by the
// `vi.resetModules()` in `beforeEach` — they exist for the transform cache
// alone. Guarded by `lib/importScoping.test.js`.
await import('./peerSync.js');
await import('../services/auth.js');

const buildApp = async () => {
  const { authGate } = await import('../services/authGate.js');
  const peerSyncRoutes = (await import('./peerSync.js')).default;
  const { errorMiddleware } = await import('../lib/errorHandler.js');
  const app = express();
  app.use(express.json());
  app.use(authGate);
  app.use('/api/peer-sync', peerSyncRoutes);
  app.use(errorMiddleware);
  return app;
};

beforeEach(() => {
  vi.resetModules();
  resetSettings();
});

afterAll(() => cleanup());

describe('peer-sync routes through authGate', () => {
  it('returns 401 before handling an unauthenticated push when instance auth is enabled', async () => {
    const auth = await import('../services/auth.js');
    await auth.setPassword({ newPassword: 'correct-horse' });
    const svc = await import('../services/sharing/peerSync.js');
    const app = await buildApp();

    const res = await request(app)
      .post('/api/peer-sync/push')
      .send({
        kind: 'universe',
        record: { id: 'u1' },
        assetManifest: [],
        sourceInstanceId: 'peer-a',
      });

    expect(res.status).toBe(401);
    expect(res.body.code).toBe('AUTH_REQUIRED');
    expect(svc.applyIncomingPush).not.toHaveBeenCalled();
  });
});