Reorganize repo structure and standards references

This commit is contained in:
david committed 2026-06-29 05:03:42 +02:00
1 parent 3cb93a81be
commit be2054274d
5885 files changed
+2588 -195

No files matched your search

@@ -0,0 +1,104 @@
import { describe, it, expect, beforeEach, vi } from "vitest";
import { createDefaultMetrics, createMetricsRegistry, type DefaultMetrics, type MetricsRegistry } from "@toolctrl/metrics";
import { startMetricsScheduler } from "./metrics-scheduler.js";
let registry: MetricsRegistry;
let metrics: DefaultMetrics;
beforeEach(() => {
registry = createMetricsRegistry({ serviceName: "worker-test", collectDefaultProcessMetrics: false });
metrics = createDefaultMetrics(registry);
});
describe("startMetricsScheduler", () => {
it("schedules both pollers at the configured cadence", () => {
const setIntervalCalls: Array<{ ms: number }> = [];
const scheduler = startMetricsScheduler({
metrics,
fetchQueueDepth: async () => [],
fetchBusinessGauges: async () => ({ activeTenants: 0, activeUsers24h: 0, subscriptionActiveByPlanId: {} }),
setInterval: (_h, ms) => {
setIntervalCalls.push({ ms });
return { ms };
},
clearInterval: () => undefined,
queueIntervalMs: 30_000,
businessIntervalMs: 60_000,
});
expect(setIntervalCalls.map((c) => c.ms)).toEqual([30_000, 60_000]);
scheduler.stop();
});
it("refreshQueueDepth writes both active and waiting gauges", async () => {
const scheduler = startMetricsScheduler({
metrics,
fetchQueueDepth: async () => [
{ queue: "email", active: 2, waiting: 7 },
{ queue: "exports", active: 0, waiting: 3 },
],
fetchBusinessGauges: async () => ({ activeTenants: 0, activeUsers24h: 0, subscriptionActiveByPlanId: {} }),
setInterval: () => undefined,
clearInterval: () => undefined,
});
await scheduler.refreshQueueDepth();
const text = await registry.metrics();
expect(text).toMatch(/queue_jobs_active\{[^}]*queue="email"[^}]*\} 2/);
expect(text).toMatch(/queue_jobs_waiting\{[^}]*queue="email"[^}]*\} 7/);
expect(text).toMatch(/queue_jobs_active\{[^}]*queue="exports"[^}]*\} 0/);
expect(text).toMatch(/queue_jobs_waiting\{[^}]*queue="exports"[^}]*\} 3/);
scheduler.stop();
});
it("refreshBusinessGauges sets tenant / user / plan gauges", async () => {
const scheduler = startMetricsScheduler({
metrics,
fetchQueueDepth: async () => [],
fetchBusinessGauges: async () => ({
activeTenants: 42,
activeUsers24h: 137,
subscriptionActiveByPlanId: { pro: 5, free: 100 },
}),
setInterval: () => undefined,
clearInterval: () => undefined,
});
await scheduler.refreshBusinessGauges();
const text = await registry.metrics();
expect(text).toMatch(/active_tenants[^\n]* 42/);
expect(text).toMatch(/active_users_24h[^\n]* 137/);
expect(text).toMatch(/subscription_active\{[^}]*plan_id="pro"[^}]*\} 5/);
expect(text).toMatch(/subscription_active\{[^}]*plan_id="free"[^}]*\} 100/);
scheduler.stop();
});
it("swallows errors and logs them — gauges keep their last value", async () => {
const error = vi.fn();
const scheduler = startMetricsScheduler({
metrics,
fetchQueueDepth: async () => {
throw new Error("redis down");
},
fetchBusinessGauges: async () => ({ activeTenants: 0, activeUsers24h: 0, subscriptionActiveByPlanId: {} }),
logger: { error },
setInterval: () => undefined,
clearInterval: () => undefined,
});
await scheduler.refreshQueueDepth();
expect(error).toHaveBeenCalledTimes(1);
expect(error.mock.calls[0]![1]).toBe("metrics-scheduler queue-depth refresh failed");
scheduler.stop();
});
it("stop() is idempotent and clears both timers", () => {
const clears: unknown[] = [];
const scheduler = startMetricsScheduler({
metrics,
fetchQueueDepth: async () => [],
fetchBusinessGauges: async () => ({ activeTenants: 0, activeUsers24h: 0, subscriptionActiveByPlanId: {} }),
setInterval: () => ({ token: Math.random() }),
clearInterval: (h) => clears.push(h),
});
scheduler.stop();
scheduler.stop();
expect(clears.length).toBe(2);
});
});