File size: 2,822 Bytes
96f2542 8122b04 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | import { beforeEach, describe, expect, it } from "vitest";
import {
addHistory,
addVoice,
db,
deleteVoice,
listHistory,
listVoices,
setFavorite,
HISTORY_CAP,
} from "@/lib/idb";
beforeEach(async () => {
await db.voices.clear();
await db.history.clear();
});
describe("voices", () => {
it("adds and lists voices ordered by createdAt desc", async () => {
await addVoice({ name: "A", blob: new Blob(["a"]), sampleRate: 24000, durationMs: 1000 });
await new Promise((r) => setTimeout(r, 5));
await addVoice({ name: "B", blob: new Blob(["b"]), sampleRate: 24000, durationMs: 1500 });
const out = await listVoices();
expect(out.map((v) => v.name)).toEqual(["B", "A"]);
});
it("setFavorite toggles", async () => {
const id = await addVoice({ name: "A", blob: new Blob(["a"]), sampleRate: 24000, durationMs: 1000 });
await setFavorite(id, true);
const v = (await listVoices()).find((x) => x.id === id)!;
expect(v.isFavorite).toBe(true);
});
it("deleteVoice removes", async () => {
const id = await addVoice({ name: "A", blob: new Blob(["a"]), sampleRate: 24000, durationMs: 1000 });
await deleteVoice(id);
expect(await listVoices()).toEqual([]);
});
});
describe("history", () => {
it("caps at HISTORY_CAP entries (oldest evicted)", async () => {
for (let i = 0; i < HISTORY_CAP + 5; i++) {
await addHistory({
text: `t${i}`,
modelId: "x",
voiceId: undefined,
language: undefined,
params: {},
audioBlob: new Blob([`${i}`]),
});
}
const items = await listHistory();
expect(items.length).toBe(HISTORY_CAP);
expect(items[0].text).toBe(`t${HISTORY_CAP + 4}`);
});
});
describe("history v2", () => {
it("stores seedUsed and kind on a row", async () => {
const id = await addHistory({
text: "x",
modelId: "m",
voiceId: undefined,
language: undefined,
params: {},
audioBlob: new Blob([""]),
kind: "single",
seedUsed: 12345,
});
const items = await listHistory();
const item = items.find((h) => h.id === id)!;
expect(item.seedUsed).toBe(12345);
expect(item.kind).toBe("single");
});
it("stores speakers list on a dialog row", async () => {
const id = await addHistory({
text: "SPEAKER A: hi",
modelId: "m",
voiceId: undefined,
language: undefined,
params: {},
audioBlob: new Blob([""]),
kind: "dialog",
seedUsed: 7,
speakers: [
{ letter: "A", voiceId: 1 },
{ letter: "B", voiceId: 2 },
],
});
const items = await listHistory();
const item = items.find((h) => h.id === id)!;
expect(item.speakers).toEqual([
{ letter: "A", voiceId: 1 },
{ letter: "B", voiceId: 2 },
]);
});
});
|