Automatiska tester, och rättningar som testerna hittade
Vitest med enhetstester utan databas och databastester mot en egen testdatabas vars namn måste sluta på _test. Rättar tomma MAP_*-värden i .env och säkerhetskopior med å, ä och ö i filnamnet. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4e8d79ee10
commit
dcb5d43429
30 files changed
+2872
-13
No files matched your search
@@ -0,0 +1,138 @@
|
||||
import { strToU8, zipSync } from "fflate";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { ImportError, parseExport, readImportFile, type ImportErrorCode } from "@/lib/book-transfer";
|
||||
import { entityRow, validExport } from "../helpers/fixtures";
|
||||
|
||||
function codeOf(fn: () => unknown): ImportErrorCode | "no-error" {
|
||||
try {
|
||||
fn();
|
||||
return "no-error";
|
||||
} catch (error) {
|
||||
if (error instanceof ImportError) return error.code;
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
const json = (data: unknown) => new TextEncoder().encode(JSON.stringify(data));
|
||||
const zip = (entries: Record<string, Uint8Array>) => zipSync(entries);
|
||||
|
||||
describe("parseExport", () => {
|
||||
it("godkänner en minimal exportfil", () => {
|
||||
const parsed = parseExport(validExport());
|
||||
expect(parsed.book.title).toBe("Testbok");
|
||||
expect(parsed.entities).toEqual([]);
|
||||
});
|
||||
|
||||
it("avvisar annat än en exportfil", () => {
|
||||
expect(codeOf(() => parseExport(null))).toBe("not-export");
|
||||
expect(codeOf(() => parseExport([]))).toBe("not-export");
|
||||
expect(codeOf(() => parseExport({ a: 1 }))).toBe("not-export");
|
||||
expect(codeOf(() => parseExport({ ...validExport(), format: "annat" }))).toBe("not-export");
|
||||
});
|
||||
|
||||
it("avvisar okänd version", () => {
|
||||
expect(codeOf(() => parseExport({ ...validExport(), version: 2 }))).toBe("version");
|
||||
});
|
||||
|
||||
it("kräver en boktitel", () => {
|
||||
expect(codeOf(() => parseExport({ ...validExport(), book: { title: " " } }))).toBe("invalid");
|
||||
expect(codeOf(() => parseExport({ ...validExport(), book: undefined }))).toBe("invalid");
|
||||
});
|
||||
|
||||
it("avvisar poster med okänd typ, saknat id eller saknat namn", () => {
|
||||
expect(codeOf(() => parseExport({ ...validExport(), entities: [{ id: "a", kind: "DRAGON", name: "X" }] }))).toBe("invalid");
|
||||
expect(codeOf(() => parseExport({ ...validExport(), entities: [{ kind: "CHARACTER", name: "X" }] }))).toBe("invalid");
|
||||
expect(codeOf(() => parseExport({ ...validExport(), entities: [{ id: "a", kind: "CHARACTER", name: "" }] }))).toBe("invalid");
|
||||
expect(codeOf(() => parseExport({ ...validExport(), entities: ["x"] }))).toBe("invalid");
|
||||
});
|
||||
|
||||
it("rensar och begränsar fält", () => {
|
||||
const parsed = parseExport({
|
||||
...validExport(),
|
||||
entities: [{ id: "a", kind: "CHARACTER", name: ` ${"x".repeat(300)} `, tags: ["ok", "", 5, "y".repeat(100)], data: "inte ett objekt" }],
|
||||
});
|
||||
expect(parsed.entities[0].name).toHaveLength(200);
|
||||
expect(parsed.entities[0].tags).toEqual(["ok", "y".repeat(40)]);
|
||||
expect(parsed.entities[0].data).toEqual({});
|
||||
});
|
||||
|
||||
it("avvisar för många poster", () => {
|
||||
expect(codeOf(() => parseExport({ ...validExport(), entities: new Array(20001).fill(0) }))).toBe("too-large");
|
||||
});
|
||||
|
||||
it("kontrollerar pass: veckodag och tider", () => {
|
||||
const shift = { personId: "a", workplaceId: null, weekday: 0, startTime: "08:00", endTime: "16:00", weeks: "ALL", label: null, scheduleCode: null };
|
||||
expect(parseExport({ ...validExport(), shifts: [shift] }).shifts).toHaveLength(1);
|
||||
expect(codeOf(() => parseExport({ ...validExport(), shifts: [{ ...shift, weekday: 7 }] }))).toBe("invalid");
|
||||
expect(codeOf(() => parseExport({ ...validExport(), shifts: [{ ...shift, startTime: "25:00" }] }))).toBe("invalid");
|
||||
expect(parseExport({ ...validExport(), shifts: [{ ...shift, weeks: "knas" }] }).shifts[0].weeks).toBe("ALL");
|
||||
});
|
||||
|
||||
it("kontrollerar kartor och markeringar", () => {
|
||||
const marker = { code: null, entityId: null, label: "M", note: null, color: "neon", lat: 1, lng: 2 };
|
||||
const map = { id: "m", code: null, name: "Karta", kind: "GEO", image: null, centerLat: null, centerLng: null, zoom: 3.7, markers: [marker] };
|
||||
const parsed = parseExport({ ...validExport(), maps: [map] });
|
||||
expect(parsed.maps[0].markers[0].color).toBe("blue"); // okänd färg blir standardfärg
|
||||
expect(parsed.maps[0].zoom).toBe(4);
|
||||
expect(codeOf(() => parseExport({ ...validExport(), maps: [{ ...map, markers: [{ ...marker, lat: "x" }] }] }))).toBe("invalid");
|
||||
expect(parseExport({ ...validExport(), maps: [{ ...map, kind: "vad som helst" }] }).maps[0].kind).toBe("GEO");
|
||||
});
|
||||
|
||||
it("hoppar över trasiga mallar utan att avvisa filen", () => {
|
||||
const parsed = parseExport({
|
||||
...validExport(),
|
||||
templates: [
|
||||
{ kind: "CHARACTER", fields: [{ key: "ok", label: "Ok", type: "text" }] },
|
||||
{ kind: "HOME", fields: "trasig" },
|
||||
{ kind: "NOPE", fields: [] },
|
||||
"skräp",
|
||||
],
|
||||
});
|
||||
expect(parsed.templates).toHaveLength(1);
|
||||
expect(parsed.templates[0].kind).toBe("CHARACTER");
|
||||
});
|
||||
});
|
||||
|
||||
describe("readImportFile", () => {
|
||||
const valid = validExport({ entities: [entityRow("e1", "CHARACTER", "Anna")] });
|
||||
|
||||
it("läser ren JSON, även med BOM", () => {
|
||||
expect(readImportFile(json(valid)).data.entities).toHaveLength(1);
|
||||
const withBom = new Uint8Array([0xef, 0xbb, 0xbf, ...json(valid)]);
|
||||
expect(readImportFile(withBom).data.book.title).toBe("Testbok");
|
||||
});
|
||||
|
||||
it("läser en zip med book.json och bilder", () => {
|
||||
const image = new Uint8Array([1, 2, 3]);
|
||||
const { data, files } = readImportFile(zip({ "book.json": json(valid), "files/a.png": image }));
|
||||
expect(data.entities[0].name).toBe("Anna");
|
||||
expect(Array.from(files.keys())).toEqual(["a.png"]);
|
||||
});
|
||||
|
||||
it("ignorerar allt i zip-filen utom book.json och files/", () => {
|
||||
const { files } = readImportFile(
|
||||
zip({
|
||||
"book.json": json(valid),
|
||||
"files/ok.png": new Uint8Array([1]),
|
||||
"files/undermapp/inne.png": new Uint8Array([2]),
|
||||
"annat/fil.png": new Uint8Array([3]),
|
||||
"hemlig.txt": new Uint8Array([4]),
|
||||
}),
|
||||
);
|
||||
expect(Array.from(files.keys())).toEqual(["ok.png"]);
|
||||
});
|
||||
|
||||
it("hoppar över bilder som är större än gränsen", () => {
|
||||
const big = new Uint8Array(17 * 1024 * 1024); // komprimeras till nästan inget men packas inte upp
|
||||
const { files } = readImportFile(zip({ "book.json": json(valid), "files/stor.png": big, "files/liten.png": new Uint8Array([1]) }));
|
||||
expect(Array.from(files.keys())).toEqual(["liten.png"]);
|
||||
});
|
||||
|
||||
it("ger tydliga fel", () => {
|
||||
expect(codeOf(() => readImportFile(new Uint8Array([])))).toBe("empty");
|
||||
expect(codeOf(() => readImportFile(new TextEncoder().encode("det här är inte JSON")))).toBe("invalid");
|
||||
expect(codeOf(() => readImportFile(new Uint8Array([0x50, 0x4b, 3, 4, 1, 2, 3])))).toBe("invalid"); // trasig zip
|
||||
expect(codeOf(() => readImportFile(zip({ "annat.txt": strToU8("hej") })))).toBe("not-export");
|
||||
expect(codeOf(() => readImportFile(json({ a: 1 })))).toBe("not-export");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user