accounting_automation: этап 1 бухгалтерской автоматизации (инвойсы из почты, категоризация, ручной разбор, MCP), интервальные задания scheduler, миграции 033/034/038
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
76
packages/accounting_automation/src/http-api.test.ts
Normal file
76
packages/accounting_automation/src/http-api.test.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import type { Pool } from "khb_common";
|
||||
import { buildApi, type JobHandlers } from "./http-api.js";
|
||||
|
||||
const settings = { http: { host: "127.0.0.1", port: 0, token: "svc-token" }, reviewers: [], mcp: { enabled: false, staticTokens: [] } };
|
||||
const auth = { authorization: "Bearer svc-token" };
|
||||
|
||||
function mk(over: Partial<JobHandlers> = {}) {
|
||||
const calls: string[] = [];
|
||||
const jobs: JobHandlers = {
|
||||
"invoices-mail": async () => { calls.push("mail"); return { created: 1 }; },
|
||||
finalize: async () => { calls.push("finalize"); return { finalized: 0 }; },
|
||||
categorize: async () => { calls.push("categorize"); return { status: "ok" }; },
|
||||
summary: async () => { calls.push("summary"); return { sent: true }; },
|
||||
...over,
|
||||
};
|
||||
return { app: buildApi({ pool: {} as Pool, settings, jobs }), calls };
|
||||
}
|
||||
|
||||
describe("служебные ручки accounting_automation", () => {
|
||||
it("healthz открыт без токена", async () => {
|
||||
const { app } = mk();
|
||||
expect((await app.inject({ method: "GET", url: "/healthz" })).statusCode).toBe(200);
|
||||
});
|
||||
|
||||
it("задания закрыты токеном", async () => {
|
||||
const { app, calls } = mk();
|
||||
expect((await app.inject({ method: "POST", url: "/jobs/finalize" })).statusCode).toBe(401);
|
||||
expect((await app.inject({ method: "POST", url: "/jobs/finalize", headers: { authorization: "Bearer bad" } })).statusCode).toBe(401);
|
||||
expect(calls).toEqual([]);
|
||||
});
|
||||
|
||||
it("неизвестное задание - 404 (в том числе служебные имена объекта)", async () => {
|
||||
const { app } = mk();
|
||||
expect((await app.inject({ method: "POST", url: "/jobs/nope", headers: auth })).statusCode).toBe(404);
|
||||
expect((await app.inject({ method: "POST", url: "/jobs/constructor", headers: auth })).statusCode).toBe(404);
|
||||
});
|
||||
|
||||
it("по умолчанию задание стартует в фоне: 202 и итог в журнале", async () => {
|
||||
const { app, calls } = mk();
|
||||
const r = await app.inject({ method: "POST", url: "/jobs/categorize", headers: auth });
|
||||
expect(r.statusCode).toBe(202);
|
||||
expect(r.json()).toEqual({ job: "categorize", started: true });
|
||||
await new Promise((res) => setTimeout(res, 20));
|
||||
expect(calls).toEqual(["categorize"]);
|
||||
});
|
||||
|
||||
it("wait=true ждёт итог; ошибка задания - 502", async () => {
|
||||
const { app } = mk({ summary: async () => { throw new Error("slack не настроен"); } });
|
||||
const ok = await app.inject({ method: "POST", url: "/jobs/finalize?wait=true", headers: auth });
|
||||
expect(ok.statusCode).toBe(200);
|
||||
expect(ok.json()).toEqual({ job: "finalize", result: { finalized: 0 } });
|
||||
const bad = await app.inject({ method: "POST", url: "/jobs/summary?wait=true", headers: auth });
|
||||
expect(bad.statusCode).toBe(502);
|
||||
expect(bad.json().message).toContain("slack");
|
||||
});
|
||||
|
||||
it("то же задание параллельно - 409, после завершения снова доступно", async () => {
|
||||
let release!: () => void;
|
||||
const gate = new Promise<void>((r) => (release = r));
|
||||
const { app } = mk({ categorize: async () => { await gate; return {}; } });
|
||||
const first = app.inject({ method: "POST", url: "/jobs/categorize?wait=true", headers: auth });
|
||||
await new Promise((res) => setTimeout(res, 20));
|
||||
expect((await app.inject({ method: "POST", url: "/jobs/categorize", headers: auth })).statusCode).toBe(409);
|
||||
release();
|
||||
expect((await first).statusCode).toBe(200);
|
||||
expect((await app.inject({ method: "POST", url: "/jobs/categorize?wait=true", headers: auth })).statusCode).toBe(200);
|
||||
});
|
||||
|
||||
it("упавшее задание освобождает блокировку", async () => {
|
||||
let n = 0;
|
||||
const { app } = mk({ finalize: async () => { if (n++ === 0) throw new Error("boom"); return {}; } });
|
||||
expect((await app.inject({ method: "POST", url: "/jobs/finalize?wait=true", headers: auth })).statusCode).toBe(502);
|
||||
expect((await app.inject({ method: "POST", url: "/jobs/finalize?wait=true", headers: auth })).statusCode).toBe(200);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user