formats.exposed/test/png/parseHash.test.ts

36 lines
849 B
TypeScript

import { assertEquals } from "assert";
import { stubbingWarn } from "../helpers.ts";
import parseHash from "../../public/png/parseHash.js";
Deno.test(
"returns null if hash cannot be parsed",
stubbingWarn(() => {
const testCases = [
// Missing fields
"#null",
"#{}",
"#{%22name%22:%22image.png%22}",
"#{%22bytes%22:%22AQID%22}",
// Invalid JSON
"",
"#{%22name%22:%22small.png%22,%22bytes%22:%22iAQID",
];
for (const testCase of testCases) {
assertEquals(
parseHash(testCase),
null,
`Parsing ${testCase} should fail`,
);
}
}),
);
Deno.test("parses hashes", () => {
const hash = "#{%22name%22:%22small.png%22,%22bytes%22:%22AQID%22}";
assertEquals(parseHash(hash), {
name: "small.png",
bytes: new Uint8Array([1, 2, 3]),
});
});