import { PaveError, type JtApi, type JtEventRef, type JtTask, type TaskWrite } from "./jt.js"; /** Подмена JobTread для тестов: задачи в памяти, имитация недоступности и журнал вызовов. */ export class FakeJt implements JtApi { organizationId = "org"; tasks = new Map(); 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 { return this.tasks.get(id) ?? null; } async listOpenTodos(): Promise { return [...this.tasks.values()].filter((t) => t.isToDo && (t.progress ?? 0) < 1); } async createTask(a: { jobId: string; name: string } & TaskWrite): Promise { 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 { 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 { this.guard("deleteTask"); this.tasks.delete(id); } async recentTaskEvents(): Promise { return []; } async listWebhooks(): Promise<{ id: string; url: string }[]> { return this.webhooks; } async createWebhook(url: string): Promise { const id = `W${this.webhooks.length + 1}`; this.webhooks.push({ id, url }); return id; } async deleteWebhook(id: string): Promise { this.webhooks = this.webhooks.filter((w) => w.id !== id); } }