// gallery-dl's Instagram rows as Posts (instagramNormalize.ts), on invented runs. import { test } from "node:test"; import assert from "node:assert/strict"; import { parsePost } from "../lib/posts"; import { instagramPkFromShortcode, instagramShortcodeFromPk } from "../lib/instagram/ids"; import { instagramDateToIso, parseInstagramRun, quoteBigIntegers } from "./instagramNormalize"; import { IG_CDN, IG_HANDLE, igError, igPk, igRows, igStdout } from "./__fixtures__/instagramRun"; const ctx = { channelSlug: "example-ig", handle: IG_HANDLE }; test("a picture, a carousel and a reel: one Post each, keyed by the pk, with its files in order", () => { const rows = igRows([ { n: 3, date: "2026-03-03 10:00:00", text: "a reel https://example.com/a.", media: ["video"], likes: 7 }, { n: 2, date: "2026-03-02 09:30:00", text: "three of them", media: ["image", "video", "image"] }, { n: 1, date: "2026-03-01 08:00:00", text: "" }, ]); const run = parseInstagramRun(igStdout(rows), ctx)!; assert.equal(run.emitted, 3); assert.equal(run.error, undefined); const [reel, carousel, picture] = run.posts; assert.equal(reel!.post.id, igPk(3)); assert.equal(reel!.post.slug, `example-ig/${igPk(3)}`); assert.equal(reel!.post.platform, "instagram"); assert.equal(reel!.post.createdAt, "2026-03-03T10:00:00.000Z"); assert.equal(reel!.post.uploadDate, "20260303"); assert.equal(reel!.post.url, `https://www.instagram.com/reel/${instagramShortcodeFromPk(igPk(3))}/`); assert.deepEqual(reel!.post.instagram, { kind: "reel", shortcode: instagramShortcodeFromPk(igPk(3)) }); assert.deepEqual(reel!.post.links, ["https://example.com/a"]); assert.deepEqual(reel!.post.engagement, { likes: 7 }); assert.equal(reel!.post.mediaCount, 1); assert.deepEqual(reel!.files.map((f) => [f.num, f.kind, f.name]), [[1, "video", `01-${igPk(301)}.mp4`]]); assert.ok(reel!.files[0]!.url.startsWith(IG_CDN)); assert.equal(carousel!.post.instagram?.kind, "post"); assert.equal(carousel!.post.url, `https://www.instagram.com/p/${instagramShortcodeFromPk(igPk(2))}/`); assert.equal(carousel!.post.mediaCount, 3); assert.deepEqual(carousel!.files.map((f) => f.kind), ["image", "video", "image"]); assert.deepEqual(carousel!.files.map((f) => f.num), [1, 2, 3]); // The signed addresses are never part of the stored post. assert.equal(carousel!.post.media, undefined); assert.ok(!JSON.stringify(carousel!.post).includes(IG_CDN)); assert.equal(picture!.post.text, ""); assert.equal(picture!.post.isReply, false); // What is written reads back the same. for (const p of run.posts) assert.deepEqual(parsePost(JSON.parse(JSON.stringify(p.post))), p.post); // The shortcode in the URL is the pk. assert.equal(instagramPkFromShortcode(reel!.post.instagram!.shortcode!), reel!.post.id); }); test("a pk printed as a bare number beyond 2^53 is kept whole", () => { const pk = igPk(987654321); const text = igStdout(igRows([{ n: 987654321, date: "2026-03-01 08:00:00" }]), [pk]); assert.ok(text.includes(`"post_id": ${pk}`)); assert.notEqual(String(JSON.parse(text)[0][1].post_id), pk, "a plain parse rounds it"); assert.equal(parseInstagramRun(text, ctx)!.posts[0]!.post.id, pk); assert.equal(quoteBigIntegers('{"a": 123, "b": 1234567890123456789}'), '{"a": 123, "b": "1234567890123456789"}'); }); test("digits inside a caption are never rewritten: long runs, escaped quotes, unicode", () => { const captions = [ "call 12345678901234567890 now, or [12345678901234567, 99999999999999999]", 'she said "id: 1234567890123456789", then {"n":12345678901234567}', "a backslash at the end \\", "émoji 🌊 1234567890123456789 
 fin, 12345678901234567", ": 1234567890123456789,", ]; const rows = igRows(captions.map((text, i) => ({ n: 40 + i, date: "2026-03-01 08:00:00", text }))); const text = igStdout(rows, captions.map((_c, i) => igPk(40 + i))); for (let i = 0; i < captions.length; i++) assert.ok(text.includes(`"post_id": ${igPk(40 + i)}`), "the pks are bare numbers"); const run = parseInstagramRun(text, ctx); assert.ok(run, "the run parses"); assert.deepEqual(run!.posts.map((p) => p.post.text), captions); assert.deepEqual(run!.posts.map((p) => p.post.id), captions.map((_c, i) => igPk(40 + i))); // The scan itself: a bare long integer is quoted; a float, a short one and a string are not. assert.equal( quoteBigIntegers('[1234567890123456789, 1.5e300, 12, "1234567890123456789", -1234567890123456789, 12345678901234567.5]'), '["1234567890123456789", 1.5e300, 12, "1234567890123456789", "-1234567890123456789", 12345678901234567.5]', ); assert.equal(quoteBigIntegers('{"a":"x\\\\","b":1234567890123456789}'), '{"a":"x\\\\","b":"1234567890123456789"}'); assert.equal(quoteBigIntegers('{"a":"say \\"1234567890123456789\\"","b":1}'), '{"a":"say \\"1234567890123456789\\"","b":1}'); }); test("numbers printed as strings (output.num-to-str) read the same", () => { const rows = igRows([{ n: 6, date: "2026-03-06 00:00:00", media: ["image", "video"], likes: 12 }]) as [number, ...unknown[]][]; for (const row of rows) { const dict = row[row.length - 1] as Record; for (const k of ["likes", "count", "num", "width", "height"]) if (typeof dict[k] === "number") dict[k] = String(dict[k]); } const [p] = parseInstagramRun(igStdout(rows), ctx)!.posts; assert.deepEqual(p!.post.engagement, { likes: 12 }); assert.equal(p!.post.mediaCount, 2); assert.deepEqual(p!.files.map((f) => f.num), [1, 2]); assert.equal(p!.row, 0); }); test("a pinned post is flagged, and one met twice is kept once", () => { const rows = igRows([ { n: 1, date: "2025-01-01 00:00:00", pinned: true }, { n: 9, date: "2026-03-09 00:00:00" }, { n: 1, date: "2025-01-01 00:00:00", pinned: true }, ]); const run = parseInstagramRun(igStdout(rows), ctx)!; assert.equal(run.emitted, 3); assert.deepEqual(run.posts.map((p) => [p.post.id, p.pinned]), [[igPk(1), true], [igPk(9), false]]); assert.equal(run.posts[0]!.post.instagram?.pinned, true); assert.equal(run.posts[0]!.files.length, 1); }); test("a collaboration: the author is the owner, and the channel's account is among the coauthors", () => { const rows = igRows([ { n: 5, date: "2026-03-05 00:00:00", owner: "other.owner", ownerName: "Other Owner", coauthors: [{ username: IG_HANDLE, full_name: "Example Account", id: "4000000001" }] }, // The row names no coauthor, yet the post is on this account's grid. { n: 4, date: "2026-03-04 00:00:00", owner: "other.owner" }, // The channel's own post with an invited collaborator. { n: 3, date: "2026-03-03 00:00:00", coauthors: [{ username: "invited.one" }, { username: IG_HANDLE }] }, ]); const [a, b, c] = parseInstagramRun(igStdout(rows), ctx)!.posts; assert.equal(a!.post.author, "other.owner"); assert.equal(a!.post.authorName, "Other Owner"); assert.equal(a!.post.channelSlug, "example-ig"); assert.deepEqual(a!.post.instagram?.coauthors, [{ handle: IG_HANDLE, name: "Example Account", id: "4000000001" }]); assert.deepEqual(b!.post.instagram?.coauthors, [{ handle: IG_HANDLE }]); assert.equal(c!.post.author, IG_HANDLE); assert.deepEqual(c!.post.instagram?.coauthors, [{ handle: "invited.one" }]); }); test("the exception row is read; unreadable rows and stories are counted, never guessed", () => { const rows = [ ...igRows([{ n: 2, date: "2026-03-02 00:00:00" }, { n: 1, date: "not a date" }, { n: 7, date: "2026-03-07 00:00:00", type: "story" }]), igError("HttpError", "'429 Too Many Requests' for 'https://www.instagram.com/api/v1/feed/user/4000000001/?count=30'"), ]; const run = parseInstagramRun(igStdout(rows), ctx)!; assert.equal(run.posts.length, 1); assert.equal(run.unreadable, 1); assert.equal(run.otherKinds, 1); assert.deepEqual(run.error, { error: "HttpError", message: "'429 Too Many Requests' for 'https://www.instagram.com/api/v1/feed/user/4000000001/?count=30'" }); // A file row after an unread post is not given to the post before it. assert.equal(run.posts[0]!.files.length, 1); }); test("what is not gallery-dl's array reads as nothing", () => { assert.equal(parseInstagramRun("", ctx), null); assert.equal(parseInstagramRun("\nKeyboardInterrupt", ctx), null); assert.equal(parseInstagramRun('{"a":1}', ctx), null); assert.deepEqual(parseInstagramRun("[]", ctx), { posts: [], emitted: 0, unreadable: 0, otherKinds: 0 }); }); test("gallery-dl's dates are UTC", () => { assert.equal(instagramDateToIso("2026-03-01 08:00:00"), "2026-03-01T08:00:00.000Z"); assert.equal(instagramDateToIso("2026-03-01T08:00:00+02:00"), "2026-03-01T06:00:00.000Z"); assert.equal(instagramDateToIso("yesterday"), null); assert.equal(instagramDateToIso(undefined), null); });