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

39 lines
882 B
TypeScript

import { assertEquals } from "assert";
import { stub } from "mock";
import parseHash from "../../public/png/parseHash.js";
Deno.test("returns null if hash cannot be parsed", () => {
const testCases = [
// Missing fields
"#null",
"#{}",
"#{%22name%22:%22image.png%22}",
"#{%22bytes%22:%22AQID%22}",
// Invalid JSON
"",
"#{%22name%22:%22small.png%22,%22bytes%22:%22iAQID",
];
const warnStub = stub(console, "warn");
try {
for (const testCase of testCases) {
assertEquals(
parseHash(testCase),
null,
`Parsing ${testCase} should fail`,
);
}
} finally {
warnStub.restore();
}
});
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]),
});
});