This repository was archived by the owner on Aug 7, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathinterop.test.ts
More file actions
59 lines (52 loc) · 2.68 KB
/
Copy pathinterop.test.ts
File metadata and controls
59 lines (52 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { describe, it, expect } from "vitest";
import { readFileSync } from "node:fs";
import * as rumcodecs from "../src/index.js";
import type { Codec, CodecConstructor } from "../src/types.js";
// Buffers in ./fixtures were written by Python numcodecs, not by this package.
// They are the only thing standing behind the claim that a Zarr chunk written by
// Python decodes here byte for byte — a round-trip suite would keep passing even
// if both directions drifted off the numcodecs format together.
// See ./fixtures/README.md to regenerate them.
const fixture = (name: string) =>
new Uint8Array(readFileSync(new URL(`./fixtures/${name}`, import.meta.url)));
const manifest = JSON.parse(
readFileSync(new URL("./fixtures/fixtures.json", import.meta.url), "utf8"),
) as {
numcodecsVersion: string;
codecs: Record<string, { config: { id: string } & Record<string, unknown> }>;
};
// Derived from the package's own exports rather than written out by hand. A
// hand-kept list is only ever as complete as the last person to edit it: add a
// codec, forget both this list and a fixture, and the coverage check below
// compares two lists that are wrong in the same way and passes. Reading the
// exports means a new codec has to be given a fixture or fail here.
const REGISTRY: Record<string, CodecConstructor<any>> = Object.fromEntries(
Object.values(rumcodecs)
.filter(
(v): v is CodecConstructor<any> =>
typeof v === "function" && typeof (v as any).codecId === "string",
)
.map((c) => [(c as any).codecId as string, c]),
);
describe(`decodes Python numcodecs ${manifest.numcodecsVersion} output`, () => {
const expected = Array.from(fixture("source.u2.bin"));
for (const [name, { config }] of Object.entries(manifest.codecs)) {
it(name, async () => {
const constructor = REGISTRY[config.id];
expect(constructor, `no codec registered for id '${config.id}'`).toBeDefined();
// Built from the config numcodecs itself recorded, the way a Zarr reader
// builds a codec out of the stored chunk metadata.
const codec: Codec = constructor.fromConfig(config as any);
const decoded = await codec.decode(fixture(`${name}.bin`));
expect(Array.from(decoded)).toEqual(expected);
});
}
it("covers every codec this package exports", () => {
const ids = new Set(Object.values(manifest.codecs).map((c) => c.config.id));
const exported = Object.keys(REGISTRY).sort();
// Guards the derivation itself: if the export scan ever silently found
// nothing, every other assertion here would vacuously agree with it.
expect(exported).toEqual(["blosc", "gzip", "lz4", "zlib", "zstd"]);
expect([...ids].sort()).toEqual(exported);
});
});