Files
sofa/packages/core/test/cron.test.ts
T
jakeandClaude Opus 4.6 373a5d3caf test: add integrations, cache, cron, and telemetry tests
- New integrations.test.ts: 13 tests covering CRUD, token generation,
  token lookup, regeneration, and multi-user isolation
- New cache.test.ts: 3 tests for purgeMetadataCache (shell title
  deletion, fully-fetched title preservation, empty case)
- New cron.test.ts: 5 tests for cron run lifecycle (start, complete,
  fail with Error and string)
- New telemetry.test.ts: 7 tests for isTelemetryEnabled toggle and
  performTelemetryReport (disabled skip, enabled send, 24h interval
  throttle, interval expiry, fetch failure resilience)

345 → 386 tests passing across 27 files

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-22 12:39:04 -04:00

54 lines
1.7 KiB
TypeScript

import { beforeEach, describe, expect, test } from "vitest";
import { cronRuns } from "@sofa/db/schema";
import { clearAllTables, eq, testDb } from "@sofa/test/db";
import { completeCronRun, failCronRun, startCronRun } from "../src/cron";
beforeEach(() => {
clearAllTables();
});
describe("startCronRun", () => {
test("inserts a cron run record", () => {
const run = startCronRun("metadata-refresh");
expect(run.id).toBeDefined();
expect(run.jobName).toBe("metadata-refresh");
const row = testDb.select().from(cronRuns).where(eq(cronRuns.id, run.id)).get();
expect(row).toBeDefined();
expect(row?.status).toBe("running");
});
});
describe("completeCronRun", () => {
test("marks a run as successful with duration", () => {
const run = startCronRun("test-job");
completeCronRun(run.id, 1500);
const row = testDb.select().from(cronRuns).where(eq(cronRuns.id, run.id)).get();
expect(row?.status).toBe("success");
expect(row?.durationMs).toBe(1500);
});
});
describe("failCronRun", () => {
test("marks a run as failed with error message", () => {
const run = startCronRun("test-job");
failCronRun(run.id, 500, new Error("Something broke"));
const row = testDb.select().from(cronRuns).where(eq(cronRuns.id, run.id)).get();
expect(row?.status).toBe("error");
expect(row?.durationMs).toBe(500);
expect(row?.errorMessage).toBe("Something broke");
});
test("handles non-Error objects", () => {
const run = startCronRun("test-job");
failCronRun(run.id, 100, "string error");
const row = testDb.select().from(cronRuns).where(eq(cronRuns.id, run.id)).get();
expect(row?.errorMessage).toBe("string error");
});
});