|
| 1 | +import { createPayloadJobsRunner } from "@focus-reactive/payload-plugin-translator"; |
| 2 | +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; |
| 3 | + |
| 4 | +import { bootTestPayload } from "./bootTestPayload"; |
| 5 | +import type { TestPayload } from "./bootTestPayload"; |
| 6 | +import { callEndpoint } from "./callEndpoint"; |
| 7 | + |
| 8 | +/* |
| 9 | + * Every other cancel spec enqueues first, so a queue row always exists by construction. This one |
| 10 | + * takes the queue row away and leaves the run records open — the state a document is left in when |
| 11 | + * something removed the work without anyone recording what became of it. |
| 12 | + */ |
| 13 | +type Seen = { targetLng: string }; |
| 14 | + |
| 15 | +const onCancelled = vi.fn<(task: Seen) => void>(); |
| 16 | + |
| 17 | +let ctx: TestPayload; |
| 18 | + |
| 19 | +const outcomesFor = async (id: string): Promise<Array<string | null | undefined>> => { |
| 20 | + const found = await ctx.payload.find({ |
| 21 | + collection: "translator-runs", |
| 22 | + depth: 0, |
| 23 | + pagination: false, |
| 24 | + overrideAccess: true, |
| 25 | + where: { collectionId: { equals: id } }, |
| 26 | + }); |
| 27 | + return found.docs.map((row) => (row as { outcome?: string | null }).outcome).sort(); |
| 28 | +}; |
| 29 | + |
| 30 | +const openRunsFor = async (id: string): Promise<number> => { |
| 31 | + const found = await ctx.payload.find({ |
| 32 | + collection: "translator-runs", |
| 33 | + depth: 0, |
| 34 | + pagination: false, |
| 35 | + overrideAccess: true, |
| 36 | + where: { and: [{ collectionId: { equals: id } }, { settledAt: { exists: false } }] }, |
| 37 | + }); |
| 38 | + return found.docs.length; |
| 39 | +}; |
| 40 | + |
| 41 | +describe("cancelling a run whose queue row is already gone", () => { |
| 42 | + let id: string; |
| 43 | + |
| 44 | + beforeAll(async () => { |
| 45 | + ctx = await bootTestPayload({ |
| 46 | + runner: createPayloadJobsRunner({ autoRun: false }), |
| 47 | + lifecycle: { onCancelled }, |
| 48 | + }); |
| 49 | + const made = await ctx.payload.create({ |
| 50 | + collection: "docs", |
| 51 | + locale: "en", |
| 52 | + data: { _status: "published", title: "Left behind", note: "A note" }, |
| 53 | + }); |
| 54 | + id = String(made.id); |
| 55 | + |
| 56 | + await callEndpoint(ctx.payload, "post", "/translate/enqueue", { |
| 57 | + body: { |
| 58 | + source_lng: "en", |
| 59 | + target_lng: ["de", "fr"], |
| 60 | + collection_slug: "docs", |
| 61 | + collection_id: [id], |
| 62 | + publish_on_translation: false, |
| 63 | + }, |
| 64 | + }); |
| 65 | + |
| 66 | + // What the sandbox was left with: the work is gone, the records still say it is under way. |
| 67 | + await ctx.payload.delete({ collection: "payload-jobs", where: {}, overrideAccess: true }); |
| 68 | + }); |
| 69 | + |
| 70 | + afterAll(async () => { |
| 71 | + await ctx?.cleanup(); |
| 72 | + }); |
| 73 | + |
| 74 | + it("closes the run records, so the document stops reading as busy", async () => { |
| 75 | + expect(await openRunsFor(id), "the document starts out held by two open records").toBe(2); |
| 76 | + |
| 77 | + const response = await callEndpoint(ctx.payload, "delete", "/translate/cancel", { |
| 78 | + body: { collection_slug: "docs", collection_id: [id] }, |
| 79 | + }); |
| 80 | + |
| 81 | + expect(response.status).toBe(204); |
| 82 | + expect( |
| 83 | + { open: await openRunsFor(id), outcomes: await outcomesFor(id) }, |
| 84 | + "a record nobody closes keeps the panel saying the document is translating, for ever" |
| 85 | + ).toStrictEqual({ open: 0, outcomes: ["cancelled", "cancelled"] }); |
| 86 | + }); |
| 87 | + |
| 88 | + it("tells the host what became of each locale", async () => { |
| 89 | + expect(onCancelled.mock.calls.map(([task]) => task.targetLng).sort()).toEqual(["de", "fr"]); |
| 90 | + }); |
| 91 | +}); |
0 commit comments