-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfetch.test.ts
141 lines (129 loc) · 3.9 KB
/
fetch.test.ts
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { afterEach, describe, expect, it, vi } from "vitest";
import FetchStore from "../src/fetch.js";
// `vitest --api` exposes the port 51204
// ref: https://vitest.dev/config/#api
let href = "http://localhost:51204/fixtures/v3/data.zarr";
describe("FetchStore", () => {
afterEach(() => {
vi.restoreAllMocks();
});
it("reads a file from string url", async () => {
let store = new FetchStore(href);
let bytes = await store.get("/zarr.json");
expect(bytes).toBeInstanceOf(Uint8Array);
expect(JSON.parse(new TextDecoder().decode(bytes))).toMatchInlineSnapshot(`
{
"attributes": {},
"node_type": "group",
"zarr_format": 3,
}
`);
});
it("reads a file from URL", async () => {
let store = new FetchStore(new URL(href));
let bytes = await store.get("/zarr.json");
expect(bytes).toBeInstanceOf(Uint8Array);
expect(JSON.parse(new TextDecoder().decode(bytes))).toMatchInlineSnapshot(`
{
"attributes": {},
"node_type": "group",
"zarr_format": 3,
}
`);
});
it("reads multi-part path", async () => {
let store = new FetchStore(href);
let bytes = await store.get("/1d.chunked.i2/zarr.json");
expect(bytes).toBeInstanceOf(Uint8Array);
expect(JSON.parse(new TextDecoder().decode(bytes))).toMatchInlineSnapshot(`
{
"attributes": {},
"chunk_grid": {
"configuration": {
"chunk_shape": [
2,
],
},
"name": "regular",
},
"chunk_key_encoding": {
"configuration": {
"separator": "/",
},
"name": "default",
},
"codecs": [
{
"configuration": {
"endian": "little",
},
"name": "bytes",
},
{
"configuration": {
"blocksize": 0,
"clevel": 5,
"cname": "zstd",
"shuffle": "noshuffle",
"typesize": 4,
},
"name": "blosc",
},
],
"data_type": "int16",
"dimension_names": null,
"fill_value": 0,
"node_type": "array",
"shape": [
4,
],
"zarr_format": 3,
}
`);
});
it("returns undefined for missing file", async () => {
let store = new FetchStore(href);
expect(await store.get("/some/random/file/missing.duh")).toBeUndefined();
});
it("forwards request options to fetch", async () => {
let headers = { "x-test": "test" };
let store = new FetchStore(href);
let spy = vi.spyOn(globalThis, "fetch");
await store.get("/zarr.json", { headers });
expect(spy).toHaveBeenCalledWith(href + "/zarr.json", { headers });
});
it("forwards request options to fetch when configured globally", async () => {
let headers = { "x-test": "test" };
let store = new FetchStore(href, { overrides: { headers } });
let spy = vi.spyOn(globalThis, "fetch");
await store.get("/zarr.json");
expect(spy).toHaveBeenCalledWith(href + "/zarr.json", { headers });
});
it("merges request options", async () => {
let overrides: RequestInit = {
headers: { "x-test": "root", "x-test2": "root" },
cache: "no-cache",
};
let store = new FetchStore(href, { overrides });
let spy = vi.spyOn(globalThis, "fetch");
await store.get("/zarr.json", { headers: { "x-test": "override" } });
expect(spy).toHaveBeenCalledWith(href + "/zarr.json", {
headers: { "x-test": "override", "x-test2": "root" },
cache: "no-cache",
});
});
it("reads partial - suffixLength", async () => {
let store = new FetchStore(href);
let bytes = await store.getRange("/zarr.json", { suffixLength: 50 });
expect(new TextDecoder().decode(bytes)).toMatchInlineSnapshot(
`"utes": {}, "zarr_format": 3, "node_type": "group"}"`,
);
});
it("reads partial - offset, length", async () => {
let store = new FetchStore(href);
let bytes = await store.getRange("/zarr.json", { offset: 4, length: 50 });
expect(new TextDecoder().decode(bytes)).toMatchInlineSnapshot(
`"tributes": {}, "zarr_format": 3, "node_type": "gro"`,
);
});
});