Files
GMW/services/backend/src/e2e.test.ts
T
MythEclipse ed4a506ca7 refactor: split llmModerationClient.ts + add tests + metrics
## Split llmModerationClient.ts (2103 → 3 files)
- **moderationBuilders.ts** (67 lines) — shared: escapeXml, getAnalysisContent, buildReferenceXml
- **mediaAnalysisClient.ts** (656 lines) — vision analysis with multi-layer LRU/DB/phash caching, image/video download, ffmpeg frame extraction, prepareMediaMessage
- **moderationOrchestrator.ts** (998 lines) — callModerationLLM, runTextOnlyBatch, runMediaBatch, runModerationAnalysis, runSimpleTextFallback
- **llmModerationClient.ts** (30 lines) — re-export bridge (backward compat)

No import changes needed — aiAnalysisWorker.ts still imports from llmModerationClient.js.

## Unit tests (backend)
- vitest.config.ts + e2e.test.ts with 9 tests against production:
  - health, metrics, dashboard/stats, recordings, config, auth, guilds, negative (404/400)

## Monitoring metrics
- moderationMetrics.ts in backend health module:
  - LLM call count/duration/tokens
  - Cache hit/miss per layer
  - Media analysis count/download duration
  - Batch size distribution, errors, SearXNG, auto-delete
2026-06-22 20:38:34 +07:00

93 lines
2.8 KiB
TypeScript

/**
* E2E API tests — runs against a running backend instance.
* Usage: vitest run (or: API_BASE=http://localhost:3001 vitest run)
*/
import { describe, it, expect } from "vitest";
const BASE = process.env.API_BASE ?? "https://imphnen.asepharyana.my.id/api";
async function api(path: string, init?: RequestInit) {
const res = await fetch(`${BASE}${path}`, {
...init,
headers: { "Content-Type": "application/json", ...init?.headers },
});
const body = res.status !== 204 ? await res.json().catch(() => null) : null;
return { status: res.status, body };
}
describe("API Health", () => {
it("GET /health returns 200 with status=healthy", async () => {
const { status, body } = await api("/health");
expect(status).toBe(200);
expect(body?.status).toBe("healthy");
});
it("GET /metrics returns prometheus text", async () => {
const res = await fetch(`${BASE.replace("/api", "")}/api/metrics`);
expect(res.status).toBe(200);
const text = await res.text();
expect(text).toContain("nodejs");
});
});
describe("API Dashboard", () => {
it("GET /dashboard/stats returns stats fields", async () => {
const { status, body } = await api("/dashboard/stats");
expect(status).toBe(200);
expect(body).toHaveProperty("total_messages");
expect(body).toHaveProperty("total_flagged");
expect(body).toHaveProperty("active_users_24h");
expect(typeof body.total_messages).toBe("number");
});
});
describe("API Recordings", () => {
it("GET /recordings returns items with pagination", async () => {
const { status, body } = await api("/recordings?limit=5");
expect(status).toBe(200);
expect(body).toHaveProperty("items");
expect(Array.isArray(body.items)).toBe(true);
if (body.items.length > 0) {
expect(body.items[0]).toHaveProperty("id");
expect(body.items[0]).toHaveProperty("username");
expect(body.items[0]).toHaveProperty("created_at");
}
});
});
describe("API Config", () => {
it("GET /config returns 200", async () => {
const { status } = await api("/config");
expect(status).toBe(200);
});
});
describe("API Auth", () => {
it("POST /auth/login with wrong password returns 401", async () => {
const { status } = await api("/auth/login", {
method: "POST",
body: JSON.stringify({ password: "wrong" }),
});
expect(status).toBe(401);
});
});
describe("API Voice", () => {
it("GET /guilds returns 200", async () => {
const { status } = await api("/guilds");
expect(status).toBe(200);
});
});
describe("API Negative", () => {
it("GET /nonexistent returns 404", async () => {
const { status } = await api("/nonexistent");
expect(status).toBe(404);
});
it("GET /messages without channelId returns 400", async () => {
const { status } = await api("/messages?limit=3");
expect(status).toBe(400);
});
});