import "../../setup.ts"; import { assertEquals } from "@std/assert"; import { DatabaseSync } from "node:sqlite"; import { up as up0010 } from "../../../db/migrations/0010_youtube_embed_start.ts"; const SCHEMA = Deno.readTextFileSync( new URL("../../../db/schema.sql", import.meta.url), ); function freshDb(): DatabaseSync { const db = new DatabaseSync(":memory:"); db.exec(SCHEMA); return db; } function insertRich(db: DatabaseSync, id: string, url: string, rich: unknown) { db.prepare( `INSERT INTO users (id, username, password_hash, created_at, email) VALUES ('u', 'u', 'x', 'now', 'u@x') ON CONFLICT DO NOTHING;`, ).run(); db.prepare( `INSERT INTO dumps (id, kind, title, user_id, created_at, url, rich_content) VALUES (?, 'url', 't', 'u', 'now', ?, ?);`, ).run(id, url, typeof rich === "string" ? rich : JSON.stringify(rich)); } function richOf(db: DatabaseSync, id: string): Record { return JSON.parse( (db.prepare(`SELECT rich_content FROM dumps WHERE id = ?;`).get(id) as { rich_content: string; }).rich_content, ); } Deno.test("0010: adds the start offset to YouTube embeds that lost it", () => { const db = freshDb(); const embed = "https://www.youtube.com/embed/abc?rel=0"; insertRich(db, "hms", "https://youtu.be/abc?t=1h2m3s", { type: "youtube", embedUrl: embed, }); insertRich(db, "plain", "https://youtu.be/abc?start=42", { type: "youtube", embedUrl: embed, }); insertRich(db, "none", "https://youtu.be/abc", { type: "youtube", embedUrl: embed, }); insertRich(db, "has", "https://youtu.be/abc?t=5", { type: "youtube", embedUrl: `${embed}&start=9`, }); insertRich(db, "other", "https://example.com/?t=5", { type: "generic", embedUrl: embed, }); insertRich(db, "bad", "https://youtu.be/abc?t=5", "{not json"); up0010(db); up0010(db); assertEquals(richOf(db, "hms").embedUrl, `${embed}&start=3723`); assertEquals(richOf(db, "plain").embedUrl, `${embed}&start=42`); assertEquals(richOf(db, "none").embedUrl, embed); assertEquals(richOf(db, "has").embedUrl, `${embed}&start=9`); assertEquals(richOf(db, "other").embedUrl, embed); db.close(); });