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

66 lines
2.6 KiB
TypeScript

import { PaveError, type JtApi, type JtEventRef, type JtTask, type TaskWrite } from "./jt.js";
/** Подмена JobTread для тестов: задачи в памяти, имитация недоступности и журнал вызовов. */
export class FakeJt implements JtApi {
organizationId = "org";
tasks = new Map<string, JtTask>();
calls: string[] = [];
/** Сколько следующих запросов записи завершатся ошибкой "JobTread недоступен" (retryable). */
failWrites = 0;
webhooks: { id: string; url: string }[] = [];
private seq = 0;
private guard(call: string): void {
this.calls.push(call);
if (this.failWrites > 0) {
this.failWrites--;
throw new PaveError("JobTread недоступен: test", true);
}
}
async getTask(id: string): Promise<JtTask | null> {
return this.tasks.get(id) ?? null;
}
async listOpenTodos(): Promise<JtTask[]> {
return [...this.tasks.values()].filter((t) => t.isToDo && (t.progress ?? 0) < 1);
}
async createTask(a: { jobId: string; name: string } & TaskWrite): Promise<JtTask> {
this.guard("createTask");
const id = `T${String(++this.seq).padStart(7, "0")}x`;
const t: JtTask = {
id, jobId: a.jobId, name: a.name, description: a.description ?? null, assigneeIds: a.assignedMembershipIds ?? [],
startDate: a.endDate ?? null, endDate: a.endDate ?? null, progress: null, isToDo: true, createdAt: new Date().toISOString(),
};
this.tasks.set(id, t);
return { ...t };
}
async updateTask(id: string, p: TaskWrite): Promise<void> {
this.guard("updateTask");
const t = this.tasks.get(id);
if (!t) throw new PaveError("not found");
if (p.name !== undefined) t.name = p.name;
if (p.description !== undefined) t.description = p.description;
if (p.endDate !== undefined) t.endDate = t.startDate = p.endDate;
if (p.progress !== undefined) t.progress = p.progress;
if (p.assignedMembershipIds !== undefined) t.assigneeIds = p.assignedMembershipIds;
}
async deleteTask(id: string): Promise<void> {
this.guard("deleteTask");
this.tasks.delete(id);
}
async recentTaskEvents(): Promise<JtEventRef[]> {
return [];
}
async listWebhooks(): Promise<{ id: string; url: string }[]> {
return this.webhooks;
}
async createWebhook(url: string): Promise<string> {
const id = `W${this.webhooks.length + 1}`;
this.webhooks.push({ id, url });
return id;
}
async deleteWebhook(id: string): Promise<void> {
this.webhooks = this.webhooks.filter((w) => w.id !== id);
}
}