Files
sofa/packages/core/test/thumbhash.test.ts
T
jakeandClaude Opus 4.6 5f3e14b415 feat: migrate test runner from Bun to Vitest
- Replace bun:test with Vitest across all 22 test files
- Create @sofa/test package with shared setup and DB test helpers
  - setup.ts: vi.mock for @sofa/db/client, Bun.randomUUIDv7 polyfill
  - db.ts: in-memory SQLite via better-sqlite3, seed helpers
- Add per-project vitest configs (packages/core, apps/web)
- Add root vitest.config.ts with projects and v8 coverage
- Set up @vitest/browser + Playwright for web component tests
- Move validateBackupDatabase from core/backup.ts to @sofa/db/client
- Derive required backup tables from schema instead of hard-coded list
- Update CI workflow with Playwright install and Codecov upload
- Update CLAUDE.md documentation for Vitest

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-22 12:03:06 -04:00

68 lines
2.0 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { episodes, seasons, titles } from "@sofa/db/schema";
import { clearAllTables, eq, testDb } from "@sofa/test/db";
const TINY_PNG = Buffer.from(
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQIHWP4//8/AwAI/AL+X2NDNwAAAABJRU5ErkJggg==",
"base64",
);
let nextBuffer: Buffer | null = TINY_PNG;
import { generateEpisodeThumbHash, generateThumbHash } from "../src/thumbhash";
beforeEach(() => {
clearAllTables();
process.env.IMAGE_CACHE_ENABLED = "false";
nextBuffer = TINY_PNG;
vi.spyOn(globalThis, "fetch").mockImplementation((async (
_input: string | URL | Request,
_init?: RequestInit,
) => {
if (!nextBuffer) {
return new Response(null, { status: 404 });
}
return new Response(nextBuffer, {
status: 200,
headers: { "content-type": "image/png" },
});
}) as typeof fetch);
});
afterEach(() => {
delete process.env.IMAGE_CACHE_ENABLED;
});
describe("thumbhash generation", () => {
test("generates a thumbhash from a loaded image buffer", async () => {
const hash = await generateThumbHash("/poster.png", "posters");
expect(hash).toBeTypeOf("string");
expect(hash).not.toBe("");
});
test("clears a stale episode thumbhash when regeneration fails", async () => {
testDb.insert(titles).values({ id: "tv-1", tmdbId: 100, type: "tv", title: "Show" }).run();
testDb.insert(seasons).values({ id: "season-1", titleId: "tv-1", seasonNumber: 1 }).run();
testDb
.insert(episodes)
.values({
id: "ep-1",
seasonId: "season-1",
episodeNumber: 1,
stillPath: "/old-still.png",
stillThumbHash: "stale-hash",
})
.run();
nextBuffer = null;
const hash = await generateEpisodeThumbHash("ep-1", "/new-still.png");
const episode = testDb.select().from(episodes).where(eq(episodes.id, "ep-1")).get();
expect(hash).toBeNull();
expect(episode?.stillThumbHash).toBeNull();
});
});