1
0
Fork 0
polaris-task-force/tests/int/npc-dialogue-attachments.int.spec.ts
Z8MB1E 18ea696d1b feat(npc-dialogue): voice clips on dialogue lines
A line can now carry a recorded voice, played when it lands with the text
paced to the clip so speech and text finish together. Clips live in media and
flowData keeps only a pointer, so a deleted file goes quiet instead of
breaking the line; resolveVoiceClip turns it into a URL per read.

Authors get two sources in the node editor: a direct upload, and an accepted
voiceover take imported from the existing submission flow. Both convert to mono
MP3 through the voiceover transcoder, because browser recordings are WebM and
iOS will not always play them, and a failed transcode stores the original
rather than losing the take.

Players control it twice: users.preferences.dialogue.voice (default on) is the
durable choice, and the speaker button in the conversation header mutes for
that conversation. Autoplay is not guaranteed, so a rejected play() shows a
Play voice button on the line rather than failing silently.
2026-11-24 17:59:36 -05:00

136 lines
3.9 KiB
TypeScript

import { getTestPayload } from "../helpers/get-test-payload";
import { Payload } from "payload";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { resolveAttachments, resolveVoiceClip } from "@/lib/npc-dialogue/attachments";
let payload: Payload;
const TIMEOUT = 30_000;
/** Create a tiny media doc and return its id. */
async function createMedia(name: string): Promise<number> {
const doc = await payload.create({
collection: "media",
data: { alt: name },
file: {
data: Buffer.from(`attachment fixture: ${name}`),
mimetype: "text/plain",
name,
size: 24,
},
overrideAccess: true,
});
return doc.id;
}
describe("resolveAttachments", () => {
beforeAll(async () => {
payload = await getTestPayload();
}, TIMEOUT);
afterAll(async () => {
// Nothing persists: each test cleans up its own docs below.
});
it(
"returns nothing for missing or empty input",
async () => {
expect(await resolveAttachments(payload, undefined)).toEqual([]);
expect(await resolveAttachments(payload, [])).toEqual([]);
},
TIMEOUT,
);
it(
"resolves pointers to renderable references, honouring custom labels",
async () => {
const id = await createMedia("b2-int-transcript.txt");
try {
const [resolved] = await resolveAttachments(payload, [
{ mediaId: id, label: "Intercept transcript" },
]);
expect(resolved).toMatchObject({
label: "Intercept transcript",
url: "/api/media/file/b2-int-transcript.txt",
filename: "b2-int-transcript.txt",
mimeType: "text/plain",
});
} finally {
await payload.delete({ collection: "media", id, overrideAccess: true });
}
},
TIMEOUT,
);
it(
"falls back to the stored filename when no label is authored",
async () => {
const id = await createMedia("b2-int-order.txt");
try {
const [resolved] = await resolveAttachments(payload, [{ mediaId: id }]);
expect(resolved?.label).toBe("b2-int-order.txt");
} finally {
await payload.delete({ collection: "media", id, overrideAccess: true });
}
},
TIMEOUT,
);
it(
"skips deleted media and malformed pointers instead of rendering dead chips",
async () => {
const doomed = await createMedia("b2-int-doomed.txt");
await payload.delete({ collection: "media", id: doomed, overrideAccess: true });
const resolved = await resolveAttachments(payload, [
{ mediaId: doomed },
{ mediaId: 0 },
{ mediaId: "not-a-number" },
{ mediaId: -3 },
]);
expect(resolved).toEqual([]);
},
TIMEOUT,
);
});
describe("resolveVoiceClip", () => {
beforeAll(async () => {
payload = await getTestPayload();
}, TIMEOUT);
it(
"returns a playable reference for a stored clip",
async () => {
const id = await createMedia("b2-int-line.mp3");
try {
const clip = await resolveVoiceClip(payload, { mediaId: id });
expect(clip).toMatchObject({
url: "/api/media/file/b2-int-line.mp3",
filename: "b2-int-line.mp3",
});
} finally {
await payload.delete({ collection: "media", id, overrideAccess: true });
}
},
TIMEOUT,
);
it(
"stays silent rather than erroring on absent, deleted, or malformed pointers",
async () => {
expect(await resolveVoiceClip(payload, undefined)).toBeNull();
const doomed = await createMedia("b2-int-doomed.mp3");
await payload.delete({ collection: "media", id: doomed, overrideAccess: true });
expect(await resolveVoiceClip(payload, { mediaId: doomed })).toBeNull();
expect(await resolveVoiceClip(payload, { mediaId: 0 })).toBeNull();
expect(
await resolveVoiceClip(payload, { mediaId: "nope" as unknown as number }),
).toBeNull();
},
TIMEOUT,
);
});