mirror of
https://github.com/teamchong/pxpipe.git
synced 2026-07-22 02:02:51 +02:00
99bb931014
- code, tests, docs, README, wrangler.toml, help text - data dir ~/.pixelpipe -> ~/.pxpipe (PXPIPE_LOG env var) - rebuilt dist; 323/323 tests
154 lines
5.4 KiB
TypeScript
154 lines
5.4 KiB
TypeScript
/**
|
|
* Tests for the new /api/* dashboard endpoints. We instantiate a
|
|
* DashboardState directly against a tmpdir SessionsPaths and call its
|
|
* serve* methods, then assert on the JSON body. No real HTTP server — the
|
|
* route dispatch lives in node.ts and would just be a thin re-export of the
|
|
* same calls.
|
|
*/
|
|
|
|
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
|
import * as fs from 'node:fs';
|
|
import * as path from 'node:path';
|
|
import * as os from 'node:os';
|
|
import { DashboardState, dashboardPath } from '../src/dashboard.js';
|
|
import type { SessionsPaths } from '../src/sessions.js';
|
|
import type { TrackEvent } from '../src/core/tracker.js';
|
|
|
|
function makeTmp(): SessionsPaths {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'pxpipe-dashapi-'));
|
|
return {
|
|
eventsFile: path.join(dir, 'events.jsonl'),
|
|
sidecarDir: path.join(dir, '4xx-bodies'),
|
|
};
|
|
}
|
|
|
|
function ev(p: Partial<TrackEvent>): TrackEvent {
|
|
return {
|
|
ts: '2026-05-19T00:00:00Z',
|
|
method: 'POST',
|
|
path: '/v1/messages',
|
|
status: 200,
|
|
duration_ms: 100,
|
|
...p,
|
|
};
|
|
}
|
|
|
|
function writeEvents(paths: SessionsPaths, events: TrackEvent[]): void {
|
|
fs.mkdirSync(path.dirname(paths.eventsFile), { recursive: true });
|
|
fs.writeFileSync(
|
|
paths.eventsFile,
|
|
events.map((e) => JSON.stringify(e)).join('\n') + '\n',
|
|
);
|
|
}
|
|
|
|
let tmp: SessionsPaths;
|
|
let dash: DashboardState;
|
|
beforeEach(() => {
|
|
tmp = makeTmp();
|
|
// Inject an empty Claude Code map so tests don't scan the developer's real
|
|
// ~/.claude/projects/ directory (slow + flaky depending on which machine
|
|
// the suite runs on). Tests that need a populated map can re-construct.
|
|
dash = new DashboardState(tmp, async () => new Map());
|
|
});
|
|
afterEach(() => {
|
|
try {
|
|
fs.rmSync(path.dirname(tmp.eventsFile), { recursive: true, force: true });
|
|
} catch {
|
|
/* leak the tmpdir; OS will reap */
|
|
}
|
|
});
|
|
|
|
// ---- dashboardPath route table -------------------------------------------
|
|
|
|
describe('dashboardPath()', () => {
|
|
it('matches the main HTML routes', () => {
|
|
expect(dashboardPath('/')?.kind).toBe('html');
|
|
expect(dashboardPath('/dashboard')?.kind).toBe('html');
|
|
});
|
|
|
|
it('matches the legacy live-poll routes', () => {
|
|
expect(dashboardPath('/proxy-stats')?.kind).toBe('stats');
|
|
expect(dashboardPath('/proxy-recent')?.kind).toBe('recent');
|
|
expect(dashboardPath('/proxy-latest-png')?.kind).toBe('png');
|
|
});
|
|
|
|
it('matches the new /api/* routes', () => {
|
|
expect(dashboardPath('/api/sessions.json')?.kind).toBe('api-sessions');
|
|
expect(dashboardPath('/api/stats.json')?.kind).toBe('api-stats');
|
|
});
|
|
|
|
it('returns null for unknown paths', () => {
|
|
expect(dashboardPath('/v1/messages')).toBeNull();
|
|
expect(dashboardPath('/api/whatever.json')).toBeNull();
|
|
// The per-session detail routes were cut — these no longer match.
|
|
expect(dashboardPath('/api/sessions/abc12345.json')).toBeNull();
|
|
expect(dashboardPath('/sessions/abc12345')).toBeNull();
|
|
});
|
|
});
|
|
|
|
// ---- /api/sessions.json --------------------------------------------------
|
|
|
|
describe('serveSessionsJson', () => {
|
|
it('returns a list of grouped sessions with claudeCode null when no ~/.claude/projects/ match', async () => {
|
|
writeEvents(tmp, [
|
|
ev({ first_user_sha8: 'aaaaaaaa', cwd: '/x', ts: '2026-05-19T00:00:00Z' }),
|
|
ev({ first_user_sha8: 'aaaaaaaa', cwd: '/x', ts: '2026-05-19T00:01:00Z' }),
|
|
ev({ first_user_sha8: 'bbbbbbbb', cwd: '/y', ts: '2026-05-19T00:02:00Z' }),
|
|
]);
|
|
const res = await dash.serveSessionsJson();
|
|
expect(res.status).toBe(200);
|
|
const body = await res.json();
|
|
expect(body.count).toBe(2);
|
|
expect(body.sessions).toHaveLength(2);
|
|
// Most-recent-first
|
|
expect(body.sessions[0].id).toBe('bbbbbbbb');
|
|
expect(body.sessions[1].id).toBe('aaaaaaaa');
|
|
expect(body.sessions[0].claudeCode).toBeNull();
|
|
});
|
|
|
|
it('respects ?project filtering', async () => {
|
|
writeEvents(tmp, [
|
|
ev({ first_user_sha8: 'aaaaaaaa', cwd: '/Users/me/code/pxpipe' }),
|
|
ev({ first_user_sha8: 'bbbbbbbb', cwd: '/Users/me/code/other' }),
|
|
]);
|
|
const res = await dash.serveSessionsJson({ project: 'pxpipe' });
|
|
const body = await res.json();
|
|
expect(body.count).toBe(1);
|
|
expect(body.sessions[0].id).toBe('aaaaaaaa');
|
|
});
|
|
|
|
it('returns 503 when DashboardState was built without paths', async () => {
|
|
const bare = new DashboardState();
|
|
const res = await bare.serveSessionsJson();
|
|
expect(res.status).toBe(503);
|
|
});
|
|
});
|
|
|
|
// ---- /api/stats.json ------------------------------------
|
|
|
|
describe('serveApiStats', () => {
|
|
it('aggregates the events file into a Summary-shaped JSON', async () => {
|
|
writeEvents(tmp, [
|
|
ev({ status: 200, compressed: true, orig_chars: 1000, image_bytes: 200 }),
|
|
ev({ status: 200, compressed: true, orig_chars: 2000, image_bytes: 300 }),
|
|
ev({ status: 400, compressed: false }),
|
|
]);
|
|
const res = await dash.serveApiStats();
|
|
expect(res.status).toBe(200);
|
|
const body = await res.json();
|
|
expect(body.parsed).toBe(3);
|
|
expect(body.summary.total).toBe(3);
|
|
expect(body.summary.ok2xx).toBe(2);
|
|
expect(body.summary.err4xx).toBe(1);
|
|
expect(body.summary.compressed).toBe(2);
|
|
expect(body.summary.passthrough).toBe(1);
|
|
expect(body.summary.origCharsTotal).toBe(3000);
|
|
expect(body.summary.imageBytesTotal).toBe(500);
|
|
});
|
|
|
|
it('404s when no events file exists', async () => {
|
|
const res = await dash.serveApiStats();
|
|
expect(res.status).toBe(404);
|
|
});
|
|
});
|