repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by admin
extensions/bak/__tests__/s3.test.ts
Rawimport { describe, expect, it } from "vitest";
import { resolveE2eStorageConfig } from "../__e2e__/support.js";
import { S3Store } from "../s3.js";
const config = {
endpoint: "https://example.invalid",
bucket: "bucket",
region: "auto",
accessKeyId: "id",
secretAccessKey: "secret",
};
function recorder(response: () => Response) {
const calls: { url: string; init: RequestInit }[] = [];
const client = {
fetch: (input: RequestInfo | URL, init?: RequestInit) => {
calls.push({ url: String(input), init: init ?? {} });
return Promise.resolve(response());
},
} as unknown as Pick<import("aws4fetch").AwsClient, "fetch">;
return { calls, client };
}
function header(init: RequestInit, name: string): string | null {
return new Headers(init.headers).get(name);
}
describe("real S3 E2E opt-in", () => {
it("does not use credentials without explicit opt-in", () => {
const env = {
PI_BAK_S3_ENDPOINT: "https://example.invalid",
PI_BAK_S3_ACCESS_KEY_ID: "id",
PI_BAK_S3_SECRET_ACCESS_KEY: "secret",
};
expect(resolveE2eStorageConfig(env)).toBeUndefined();
expect(
resolveE2eStorageConfig({ ...env, PI_BAK_E2E_S3: "1" }),
).toMatchObject({
endpoint: env.PI_BAK_S3_ENDPOINT,
});
expect(() => resolveE2eStorageConfig({ PI_BAK_E2E_S3: "1" })).toThrow(
/Real S3 E2E requires/,
);
});
});
describe("S3Store weak ETag handling", () => {
it("requests identity encoding and strips weak validators on get", async () => {
const { calls, client } = recorder(
() =>
new Response("body", {
status: 200,
headers: { etag: 'W/"abc"' },
}),
);
const store = new S3Store(config, client);
const object = await store.get("hosts/host.json");
expect(object?.etag).toBe('"abc"');
expect(header(calls[0]?.init ?? {}, "accept-encoding")).toBe("identity");
});
it("sends a strong validator when given a weak ifMatch", async () => {
const { calls, client } = recorder(
() => new Response(null, { status: 200, headers: { etag: 'W/"def"' } }),
);
const store = new S3Store(config, client);
const etag = await store.put("hosts/host.json", "body", {
contentType: "application/json",
ifMatch: 'W/"abc"',
});
expect(header(calls[0]?.init ?? {}, "if-match")).toBe('"abc"');
expect(etag).toBe('"def"');
});
});