|
| 1 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +import FetchStore from "../src/fetch.js"; |
| 4 | + |
| 5 | +// `vitest --api` exposes the port 51204 |
| 6 | +// ref: https://vitest.dev/config/#api |
| 7 | +let href = "http://localhost:51204/fixtures/v3/data.zarr"; |
| 8 | + |
| 9 | +describe("FetchStore", () => { |
| 10 | + afterEach(() => { |
| 11 | + vi.restoreAllMocks(); |
| 12 | + }); |
| 13 | + |
| 14 | + it("reads a file from string url", async () => { |
| 15 | + let store = new FetchStore(href); |
| 16 | + let bytes = await store.get("/zarr.json"); |
| 17 | + expect(bytes).toBeInstanceOf(Uint8Array); |
| 18 | + expect(JSON.parse(new TextDecoder().decode(bytes))).toMatchInlineSnapshot(` |
| 19 | + { |
| 20 | + "attributes": {}, |
| 21 | + "node_type": "group", |
| 22 | + "zarr_format": 3, |
| 23 | + } |
| 24 | + `); |
| 25 | + }); |
| 26 | + |
| 27 | + it("reads a file from URL", async () => { |
| 28 | + let store = new FetchStore(new URL(href)); |
| 29 | + let bytes = await store.get("/zarr.json"); |
| 30 | + expect(bytes).toBeInstanceOf(Uint8Array); |
| 31 | + expect(JSON.parse(new TextDecoder().decode(bytes))).toMatchInlineSnapshot(` |
| 32 | + { |
| 33 | + "attributes": {}, |
| 34 | + "node_type": "group", |
| 35 | + "zarr_format": 3, |
| 36 | + } |
| 37 | + `); |
| 38 | + }); |
| 39 | + |
| 40 | + it("reads multi-part path", async () => { |
| 41 | + let store = new FetchStore(href); |
| 42 | + let bytes = await store.get("/1d.chunked.i2/zarr.json"); |
| 43 | + expect(bytes).toBeInstanceOf(Uint8Array); |
| 44 | + expect(JSON.parse(new TextDecoder().decode(bytes))).toMatchInlineSnapshot(` |
| 45 | + { |
| 46 | + "attributes": {}, |
| 47 | + "chunk_grid": { |
| 48 | + "configuration": { |
| 49 | + "chunk_shape": [ |
| 50 | + 2, |
| 51 | + ], |
| 52 | + }, |
| 53 | + "name": "regular", |
| 54 | + }, |
| 55 | + "chunk_key_encoding": { |
| 56 | + "configuration": { |
| 57 | + "separator": "/", |
| 58 | + }, |
| 59 | + "name": "default", |
| 60 | + }, |
| 61 | + "codecs": [ |
| 62 | + { |
| 63 | + "configuration": { |
| 64 | + "endian": "little", |
| 65 | + }, |
| 66 | + "name": "endian", |
| 67 | + }, |
| 68 | + { |
| 69 | + "configuration": { |
| 70 | + "blocksize": 0, |
| 71 | + "clevel": 5, |
| 72 | + "cname": "zstd", |
| 73 | + "shuffle": "noshuffle", |
| 74 | + "typesize": 4, |
| 75 | + }, |
| 76 | + "name": "blosc", |
| 77 | + }, |
| 78 | + ], |
| 79 | + "data_type": "int16", |
| 80 | + "dimension_names": null, |
| 81 | + "fill_value": 0, |
| 82 | + "node_type": "array", |
| 83 | + "shape": [ |
| 84 | + 4, |
| 85 | + ], |
| 86 | + "zarr_format": 3, |
| 87 | + } |
| 88 | + `); |
| 89 | + }); |
| 90 | + |
| 91 | + it("returns undefined for missing file", async () => { |
| 92 | + let store = new FetchStore(href); |
| 93 | + expect(await store.get("/missing.json")).toBeUndefined(); |
| 94 | + }); |
| 95 | + |
| 96 | + it("forwards request options to fetch", async () => { |
| 97 | + let headers = { "x-test": "test" }; |
| 98 | + let store = new FetchStore(href); |
| 99 | + let spy = vi.spyOn(globalThis, "fetch"); |
| 100 | + await store.get("/zarr.json", { headers }); |
| 101 | + expect(spy).toHaveBeenCalledWith(href + "/zarr.json", { headers }); |
| 102 | + }); |
| 103 | + |
| 104 | + it("forwards request options to fetch when configured globally", async () => { |
| 105 | + let headers = { "x-test": "test" }; |
| 106 | + let store = new FetchStore(href, { headers }); |
| 107 | + let spy = vi.spyOn(globalThis, "fetch"); |
| 108 | + await store.get("/zarr.json"); |
| 109 | + expect(spy).toHaveBeenCalledWith(href + "/zarr.json", { headers }); |
| 110 | + }); |
| 111 | + |
| 112 | + it("overrides request options", async () => { |
| 113 | + let opts: RequestInit = { |
| 114 | + headers: { "x-test": "root", "x-test2": "root" }, |
| 115 | + cache: "no-cache", |
| 116 | + }; |
| 117 | + let store = new FetchStore(href, opts); |
| 118 | + let spy = vi.spyOn(globalThis, "fetch"); |
| 119 | + await store.get("/zarr.json", { headers: { "x-test": "override" } }); |
| 120 | + expect(spy).toHaveBeenCalledWith(href + "/zarr.json", { |
| 121 | + headers: { "x-test": "override" }, |
| 122 | + cache: "no-cache", |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + it("checks if key exists", async () => { |
| 127 | + let store = new FetchStore(href); |
| 128 | + expect(await store.has("/zarr.json")).toBe(true); |
| 129 | + expect(await store.has("/missing.json")).toBe(false); |
| 130 | + }); |
| 131 | +}); |
0 commit comments