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 { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import express from 'express';
import { realpathSync } from 'fs';
import { homedir } from 'os';
import { errorMiddleware } from '../lib/errorHandler.js';
import { request } from '../lib/testHelper.js';
import detectRoutes from './detect.js';
vi.mock('../services/aiDetect.js', () => ({
detectAppWithAi: vi.fn()
}));
// `listProcessesStrict` returns `null` on a FAILED PM2 read (distinct from a
// successful `[]` empty read) — the absent-vs-empty contract from issue #968.
vi.mock('../services/pm2.js', () => ({
listProcessesStrict: vi.fn()
}));
import { detectAppWithAi } from '../services/aiDetect.js';
import { listProcessesStrict } from '../services/pm2.js';
describe('Detect Routes', () => {
let app;
beforeEach(() => {
app = express();
app.use(express.json());
app.use('/api/detect', detectRoutes);
app.use(errorMiddleware);
vi.clearAllMocks();
});
it('keeps an expected AI detection refusal as a structured 200 outcome', async () => {
detectAppWithAi.mockResolvedValue({ success: false, error: 'No AI provider configured' });
const response = await request(app)
.post('/api/detect/ai')
.send({ path: '/example/project' });
expect(response.status).toBe(200);
expect(response.body).toEqual({ success: false, error: 'No AI provider configured' });
});
// #9008: whether a non-tool-free provider may run is decided from the
// server-derived authority, never from anything the caller sends.
it('hands detection the server-derived host-control verdict, ignoring the body', async () => {
detectAppWithAi.mockResolvedValue({ success: false, error: 'No AI provider configured' });
const withAuth = (authContext) => {
const scoped = express();
scoped.use(express.json());
scoped.use((req, _res, next) => { req.portosAuthContext = authContext; next(); });
scoped.use('/api/detect', detectRoutes);
scoped.use(errorMiddleware);
return scoped;
};
// Password-free install, loopback connection: the operator.
await request(withAuth({ enabled: false })).post('/api/detect/ai').send({ path: '/example/project' });
expect(detectAppWithAi).toHaveBeenLastCalledWith('/example/project', undefined, { hasHostControl: true });
// A peer credential on a password-gated install never qualifies.
await request(withAuth({ enabled: true, authenticated: true, method: 'basic' }))
.post('/api/detect/ai').send({ path: '/example/project', hasHostControl: true });
expect(detectAppWithAi).toHaveBeenLastCalledWith('/example/project', undefined, { hasHostControl: false });
});
it('returns the standard error envelope when AI detection throws', async () => {
detectAppWithAi.mockRejectedValue(new Error('provider crashed'));
const response = await request(app)
.post('/api/detect/ai')
.send({ path: '/example/project' });
expect(response.status).toBe(500);
expect(response.body).toMatchObject({ error: 'provider crashed', code: 'INTERNAL_ERROR' });
expect(response.body).not.toHaveProperty('success');
});
});
describe('POST /api/detect/pm2', () => {
let app;
beforeEach(() => {
app = express();
app.use(express.json());
app.use('/api/detect', detectRoutes);
app.use(errorMiddleware);
vi.clearAllMocks();
});
it('rejects a wrong-typed process name before reading PM2', async () => {
const response = await request(app)
.post('/api/detect/pm2')
.send({ name: { $ne: 'x' } });
expect(response.status).toBe(400);
expect(response.body.code).toBe('VALIDATION_ERROR');
expect(listProcessesStrict).not.toHaveBeenCalled();
});
it('reports exists:false only for a successful read with no matching process', async () => {
listProcessesStrict.mockResolvedValue([{ name: 'other-app', status: 'online', pid: 123, pm_id: 0 }]);
const response = await request(app)
.post('/api/detect/pm2')
.send({ name: 'example-app' });
expect(response.status).toBe(200);
expect(response.body).toEqual({ name: 'example-app', exists: false, process: null });
});
it('reports the matched process status/pid from the mapped shape', async () => {
listProcessesStrict.mockResolvedValue([{ name: 'example-app', status: 'online', pid: 123, pm_id: 0 }]);
const response = await request(app)
.post('/api/detect/pm2')
.send({ name: 'example-app' });
expect(response.status).toBe(200);
expect(response.body).toEqual({
name: 'example-app',
exists: true,
process: { name: 'example-app', status: 'online', pid: 123, pm_id: 0 }
});
});
// Issue #8164: a FAILED PM2 read must not be reported as exists:false —
// that would tell a caller the process is confidently absent when its
// status is actually unavailable.
it('returns a 503 instead of exists:false when the PM2 read fails', async () => {
listProcessesStrict.mockResolvedValue(null);
const response = await request(app)
.post('/api/detect/pm2')
.send({ name: 'example-app' });
expect(response.status).toBe(503);
expect(response.body).toMatchObject({ code: 'PM2_UNAVAILABLE' });
expect(response.body).not.toHaveProperty('exists');
});
});
// A directory that exists on the host but sits OUTSIDE DEFAULT_WORKSPACE_ROOTS,
// so the only thing that can admit it is PORTOS_WORKSPACE_ROOTS. Deliberately not
// a mkdtemp() directory: `tmpdir()` is itself a default root on Linux (/tmp) and
// Windows, so a temp dir is always allowed there and would make the refusal test
// pass only on macOS. realpathSync because macOS's /etc is a symlink to
// /private/etc and the route realpath()s before checking containment.
const OUTSIDE_DEFAULT_ROOTS = realpathSync(
process.platform === 'win32' ? (process.env.SystemRoot || 'C:\\Windows') : '/etc'
);
// PORTOS_WORKSPACE_ROOTS is read once at module load, so these stub the env and
// re-import a fresh copy of the route graph. The module-registry reset drops the
// hoisted vi.mock above with it, so the AI service is re-mocked per load via
// vi.doMock (which applies to subsequent dynamic imports).
describe('POST /api/detect/ai workspace-root confinement', () => {
const loadApp = async (workspaceRoots) => {
vi.resetModules();
vi.stubEnv('PORTOS_WORKSPACE_ROOTS', workspaceRoots);
const detect = vi.fn();
vi.doMock('../services/aiDetect.js', () => ({ detectAppWithAi: detect }));
const routes = await import('./detect.js');
const errors = await import('../lib/errorHandler.js');
const scoped = express();
scoped.use(express.json());
scoped.use('/api/detect', routes.default);
scoped.use(errors.errorMiddleware);
return { app: scoped, detect };
};
afterEach(() => {
vi.doUnmock('../services/aiDetect.js');
vi.unstubAllEnvs();
vi.resetModules();
});
it('refuses a path outside the configured roots without invoking the provider', async () => {
const { app: scoped, detect } = await loadApp(homedir());
const response = await request(scoped)
.post('/api/detect/ai')
.send({ path: OUTSIDE_DEFAULT_ROOTS });
expect(response.status).toBe(400);
expect(response.body).toMatchObject({ code: 'PATH_OUTSIDE_WORKSPACE_ROOTS' });
// The refusal must not echo the rejected filesystem path back to the caller.
expect(JSON.stringify(response.body)).not.toContain(OUTSIDE_DEFAULT_ROOTS);
// The regression this uniquely catches: no file was read and nothing was
// shipped to a (possibly hosted) AI provider.
expect(detect).not.toHaveBeenCalled();
});
it('allows a path the configured roots admit but the defaults would not', async () => {
const { app: scoped, detect } = await loadApp(OUTSIDE_DEFAULT_ROOTS);
detect.mockResolvedValue({ success: true, app: { name: 'example' } });
const response = await request(scoped)
.post('/api/detect/ai')
.send({ path: OUTSIDE_DEFAULT_ROOTS });
expect(response.status).toBe(200);
expect(response.body).toEqual({ success: true, app: { name: 'example' } });
expect(detect).toHaveBeenCalledWith(OUTSIDE_DEFAULT_ROOTS, undefined, { hasHostControl: false });
});
it('stays unrestricted when PORTOS_WORKSPACE_ROOTS is unset', async () => {
const { app: scoped, detect } = await loadApp('');
detect.mockResolvedValue({ success: true, app: { name: 'example' } });
const response = await request(scoped)
.post('/api/detect/ai')
.send({ path: OUTSIDE_DEFAULT_ROOTS });
expect(response.status).toBe(200);
expect(detect).toHaveBeenCalledWith(OUTSIDE_DEFAULT_ROOTS, undefined, { hasHostControl: false });
});
});