Files
khb_microservices/packages/todo_service/src/live.int.test.ts
2026-10-29 11:08:54 +03:00

65 lines
3.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { afterAll, describe, expect, it } from "vitest";
import { createLogger, createTestDb, jobtreadSchema, readSettingsFile, testDatabaseUrl, type TestDb } from "khb_common";
import { ADMIN } from "./access.js";
import { TodoCore } from "./core.js";
import { PaveJt } from "./jt.js";
import { todoServiceSchema } from "./settings.js";
/**
* Живая проверка против JobTread: только тестовый проект 22PdjKPJvzbk, названия с префиксом llmtest-, созданное удаляется.
* Запуск: KHB_LIVE_JT=1 npx vitest run packages/todo_service/src/live.int.test.ts (нужны settings.json5 и туннель к БД).
*/
const TEST_JOB = "22PdjKPJvzbk";
const live = process.env.KHB_LIVE_JT === "1" && !!testDatabaseUrl();
const d = live ? describe : describe.skip;
d("todo_service: живой JobTread (тестовый проект)", () => {
const jtCfg = live ? jobtreadSchema.parse(readSettingsFile().jobtread) : { grantKey: "", organizationId: "" };
const jt = new PaveJt(jtCfg);
const created: string[] = [];
let db: TestDb | undefined;
afterAll(async () => {
for (const id of created) await jt.deleteTask(id).catch(() => undefined);
await db?.drop();
});
it("создать, прочитать, изменить, закрыть и удалить todo; startDate всегда равен endDate", async () => {
const t = await jt.createTask({ jobId: TEST_JOB, name: "llmtest-todo-service live", description: "live", endDate: "2026-12-01" });
created.push(t.id);
expect(t).toMatchObject({ jobId: TEST_JOB, isToDo: true, endDate: "2026-12-01", startDate: "2026-12-01", progress: null });
await jt.updateTask(t.id, { name: "llmtest-todo-service live 2", endDate: "2026-12-03", progress: 1 });
const g = await jt.getTask(t.id);
expect(g).toMatchObject({ name: "llmtest-todo-service live 2", endDate: "2026-12-03", startDate: "2026-12-03", progress: 1 });
await jt.deleteTask(t.id);
created.pop();
expect(await jt.getTask(t.id)).toBeNull();
});
it("ядро поверх JobTread: create / snooze / close, чтение через копию, события опроса", async () => {
db = await createTestDb(testDatabaseUrl()!);
const settings = todoServiceSchema.parse({ http: { token: "x" }, allowedJobIds: [TEST_JOB] });
const core = new TodoCore({ pool: db.pool, jt, settings, tz: "America/New_York", log: createLogger("t", "silent") });
const r = (await core.create(ADMIN, { jobId: TEST_JOB, name: "llmtest-todo-service core", kind: "reminder" }, "live-key")) as { id: string };
created.push(r.id);
const v = await core.get(ADMIN, r.id);
expect(v).toMatchObject({ kind: "reminder", status: "open", jobId: TEST_JOB });
await core.snooze(ADMIN, r.id, { until: "2026-12-10" });
expect((await core.get(ADMIN, r.id)).reviewDate).toBe("2026-12-10");
await core.close(ADMIN, r.id);
expect((await core.get(ADMIN, r.id)).status).toBe("done");
const ids = await jt.recentTaskEvents(new Date(Date.now() - 10 * 60_000).toISOString());
expect(ids.some((e) => e.taskId === r.id)).toBe(true);
expect(await core.pollEvents()).toBeGreaterThan(0);
await core.remove(ADMIN, r.id);
created.pop();
expect((await core.get(ADMIN, r.id).catch(() => ({ status: "deleted" }))).status).toBe("deleted");
});
it("открытые todo читаются постранично", async () => {
const open = await jt.listOpenTodos();
expect(open.length).toBeGreaterThan(10);
expect(open.every((t) => t.isToDo && (t.progress ?? 0) < 1)).toBe(true);
});
});