fix: manage immich request lifecycles
This commit is contained in:
1 parent
a5973bfaf1
commit
653e5ca050
2 files changed
+295
-26
No files matched your search
@@ -18,6 +18,14 @@ beforeEach(() => server.listen({ onUnhandledRequest: "error" }));
|
||||
afterEach(() => server.resetHandlers());
|
||||
afterEach(() => server.close());
|
||||
|
||||
function wait(milliseconds: number): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, milliseconds));
|
||||
}
|
||||
|
||||
function albumsResponse(body: ReadableStream<Uint8Array> | string): Response {
|
||||
return new Response(body, { headers: { "content-type": "application/json" } });
|
||||
}
|
||||
|
||||
test("lists albums through the documented API with server-side authentication", async () => {
|
||||
server.use(
|
||||
http.get(`${baseUrl}/api/albums`, ({ request }) => {
|
||||
@@ -109,6 +117,136 @@ test("maps an aborted timeout signal to IMMICH_TIMEOUT", async () => {
|
||||
await expect(timedOutClient.listAlbums()).rejects.toMatchObject({ code: "IMMICH_TIMEOUT" });
|
||||
});
|
||||
|
||||
test("cleans up the request timeout after a successful JSON response", async () => {
|
||||
let requestSignal: AbortSignal | undefined;
|
||||
const fastClient = createImmichClient({
|
||||
baseUrl,
|
||||
apiKey,
|
||||
timeoutMs: 5,
|
||||
fetch: async (_input, init) => {
|
||||
requestSignal = init?.signal ?? undefined;
|
||||
return albumsResponse(JSON.stringify([immichAlbum]));
|
||||
},
|
||||
});
|
||||
|
||||
await expect(fastClient.listAlbums()).resolves.toEqual([immichAlbum]);
|
||||
await wait(20);
|
||||
expect(requestSignal?.aborted).toBe(false);
|
||||
});
|
||||
|
||||
test("clears the request timeout after binary headers so a long stream continues", async () => {
|
||||
let sourceController: ReadableStreamDefaultController<Uint8Array> | undefined;
|
||||
let requestAborts = 0;
|
||||
const streamingClient = createImmichClient({
|
||||
baseUrl,
|
||||
apiKey,
|
||||
timeoutMs: 5,
|
||||
fetch: async (_input, init) => {
|
||||
const body = new ReadableStream<Uint8Array>({
|
||||
start(controller) {
|
||||
sourceController = controller;
|
||||
init?.signal?.addEventListener("abort", () => {
|
||||
requestAborts += 1;
|
||||
controller.error(init.signal?.reason);
|
||||
});
|
||||
},
|
||||
});
|
||||
return new Response(body, { headers: { "content-type": "image/jpeg" } });
|
||||
},
|
||||
});
|
||||
|
||||
const original = await streamingClient.getOriginal("asset-1");
|
||||
await wait(20);
|
||||
sourceController?.enqueue(new Uint8Array([1, 2, 3]));
|
||||
sourceController?.close();
|
||||
|
||||
await expect(new Response(original.body).arrayBuffer()).resolves.toEqual(new Uint8Array([1, 2, 3]).buffer);
|
||||
expect(requestAborts).toBe(0);
|
||||
});
|
||||
|
||||
test("maps a caller abort during a binary stream to IMMICH_ABORTED", async () => {
|
||||
const caller = new AbortController();
|
||||
const streamingClient = createImmichClient({
|
||||
baseUrl,
|
||||
apiKey,
|
||||
fetch: async () => {
|
||||
const body = new ReadableStream<Uint8Array>();
|
||||
return new Response(body, { headers: { "content-type": "image/jpeg" } });
|
||||
},
|
||||
});
|
||||
|
||||
const original = await streamingClient.getOriginal("asset-1", caller.signal);
|
||||
const read = original.body.getReader().read();
|
||||
caller.abort();
|
||||
|
||||
await expect(read).rejects.toMatchObject({ code: "IMMICH_ABORTED" });
|
||||
});
|
||||
|
||||
test("preserves abort, timeout, and network failures while reading JSON bodies", async () => {
|
||||
const caller = new AbortController();
|
||||
const abortedClient = createImmichClient({
|
||||
baseUrl,
|
||||
apiKey,
|
||||
fetch: async (_input, init) =>
|
||||
albumsResponse(
|
||||
new ReadableStream({
|
||||
start(controller) {
|
||||
init?.signal?.addEventListener("abort", () => controller.error(init.signal?.reason));
|
||||
},
|
||||
}),
|
||||
),
|
||||
});
|
||||
const aborted = abortedClient.listAlbums(caller.signal);
|
||||
await Promise.resolve();
|
||||
caller.abort();
|
||||
await expect(aborted).rejects.toMatchObject({ code: "IMMICH_ABORTED" });
|
||||
|
||||
const timedOutClient = createImmichClient({
|
||||
baseUrl,
|
||||
apiKey,
|
||||
timeoutMs: 5,
|
||||
fetch: async (_input, init) =>
|
||||
albumsResponse(
|
||||
new ReadableStream({
|
||||
start(controller) {
|
||||
init?.signal?.addEventListener("abort", () => controller.error(init.signal?.reason));
|
||||
},
|
||||
}),
|
||||
),
|
||||
});
|
||||
await expect(timedOutClient.listAlbums()).rejects.toMatchObject({ code: "IMMICH_TIMEOUT" });
|
||||
|
||||
const unavailableClient = createImmichClient({
|
||||
baseUrl,
|
||||
apiKey,
|
||||
fetch: async () =>
|
||||
albumsResponse(
|
||||
new ReadableStream({
|
||||
start(controller) {
|
||||
queueMicrotask(() => controller.error(new TypeError("connection lost")));
|
||||
},
|
||||
}),
|
||||
),
|
||||
});
|
||||
await expect(unavailableClient.listAlbums()).rejects.toMatchObject({ code: "IMMICH_UNAVAILABLE" });
|
||||
});
|
||||
|
||||
test("maps malformed JSON and schema failures to IMMICH_INVALID_RESPONSE", async () => {
|
||||
const malformedJsonClient = createImmichClient({
|
||||
baseUrl,
|
||||
apiKey,
|
||||
fetch: async () => albumsResponse("{"),
|
||||
});
|
||||
await expect(malformedJsonClient.listAlbums()).rejects.toMatchObject({ code: "IMMICH_INVALID_RESPONSE" });
|
||||
|
||||
const malformedSchemaClient = createImmichClient({
|
||||
baseUrl,
|
||||
apiKey,
|
||||
fetch: async () => albumsResponse(JSON.stringify([{ id: "missing-required-fields" }])),
|
||||
});
|
||||
await expect(malformedSchemaClient.listAlbums()).rejects.toMatchObject({ code: "IMMICH_INVALID_RESPONSE" });
|
||||
});
|
||||
|
||||
test("streams binary endpoints without buffering and preserves safe response metadata", async () => {
|
||||
const bytes = new Uint8Array([1, 2, 3]);
|
||||
server.use(
|
||||
|
||||
Reference in new issue
Block a user