211 lines
7.4 KiB
TypeScript
211 lines
7.4 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import {
|
|
AbortMultipartUploadCommand,
|
|
CompleteMultipartUploadCommand,
|
|
CreateMultipartUploadCommand,
|
|
UploadPartCopyCommand,
|
|
createS3StorageAdapter,
|
|
type S3StorageAdapterConfig,
|
|
} from "./index.js";
|
|
|
|
// AK-P4a §3: server-side chunk assembly via S3 multipart UploadPartCopy. The
|
|
// create -> uploadPartCopy -> complete -> abort sequence is exercised here with
|
|
// a fake S3 client so no network round-trip is required.
|
|
const baseConfig: S3StorageAdapterConfig = {
|
|
endpoint: "https://s3.example.test",
|
|
region: "us-east-1",
|
|
bucket: "uploads",
|
|
accessKeyId: "access-key",
|
|
secretAccessKey: "secret-key",
|
|
publicBaseUrl: "https://cdn.example.test/assets",
|
|
};
|
|
|
|
describe("createS3StorageAdapter multipart", () => {
|
|
it("createMultipartUpload sends a CreateMultipartUploadCommand and returns the UploadId", async () => {
|
|
const send = vi.fn().mockResolvedValue({ UploadId: "upload-1" });
|
|
const adapter = createS3StorageAdapter({ ...baseConfig, client: { send } });
|
|
|
|
await expect(
|
|
adapter.createMultipartUpload({
|
|
key: "big/object.bin",
|
|
contentType: "application/octet-stream",
|
|
metadata: { tenant: "acme" },
|
|
}),
|
|
).resolves.toEqual({ bucket: "uploads", key: "big/object.bin", uploadId: "upload-1" });
|
|
|
|
const command = send.mock.calls[0]?.[0];
|
|
expect(command).toBeInstanceOf(CreateMultipartUploadCommand);
|
|
expect(command.input).toEqual({
|
|
Bucket: "uploads",
|
|
Key: "big/object.bin",
|
|
ContentType: "application/octet-stream",
|
|
Metadata: { tenant: "acme" },
|
|
});
|
|
});
|
|
|
|
it("createMultipartUpload fails when S3 returns no UploadId", async () => {
|
|
const adapter = createS3StorageAdapter({
|
|
...baseConfig,
|
|
client: { send: vi.fn().mockResolvedValue({}) },
|
|
});
|
|
|
|
await expect(
|
|
adapter.createMultipartUpload({ key: "big/object.bin" }),
|
|
).rejects.toMatchObject({
|
|
code: "put_failed",
|
|
details: { operation: "createMultipartUpload", bucket: "uploads", key: "big/object.bin" },
|
|
});
|
|
});
|
|
|
|
it("createMultipartUpload wraps upstream errors with redacted diagnostics", async () => {
|
|
const upstreamError = Object.assign(new Error("denied for credential secret-key"), {
|
|
name: "AccessDenied",
|
|
$metadata: { httpStatusCode: 403, requestId: "req-create" },
|
|
});
|
|
const adapter = createS3StorageAdapter({
|
|
...baseConfig,
|
|
client: { send: vi.fn().mockRejectedValue(upstreamError) },
|
|
});
|
|
|
|
await expect(
|
|
adapter.createMultipartUpload({ key: "big/object.bin" }),
|
|
).rejects.toMatchObject({
|
|
code: "put_failed",
|
|
details: {
|
|
operation: "createMultipartUpload",
|
|
upstream: { name: "AccessDenied", message: "denied for credential [redacted]", status: 403, requestId: "req-create" },
|
|
},
|
|
});
|
|
});
|
|
|
|
it("uploadPartCopy URL-encodes the CopySource and returns the part ETag", async () => {
|
|
const send = vi.fn().mockResolvedValue({ CopyPartResult: { ETag: '"etag-part-1"' } });
|
|
const adapter = createS3StorageAdapter({ ...baseConfig, client: { send } });
|
|
|
|
await expect(
|
|
adapter.uploadPartCopy({
|
|
key: "big/object.bin",
|
|
uploadId: "upload-1",
|
|
partNumber: 1,
|
|
sourceKey: "tmp/chunk one",
|
|
}),
|
|
).resolves.toEqual({ partNumber: 1, etag: '"etag-part-1"' });
|
|
|
|
const command = send.mock.calls[0]?.[0];
|
|
expect(command).toBeInstanceOf(UploadPartCopyCommand);
|
|
expect(command.input).toMatchObject({
|
|
Bucket: "uploads",
|
|
Key: "big/object.bin",
|
|
UploadId: "upload-1",
|
|
PartNumber: 1,
|
|
// "uploads/tmp/chunk one" with each path segment percent-encoded.
|
|
CopySource: "uploads/tmp/chunk%20one",
|
|
});
|
|
});
|
|
|
|
it("uploadPartCopy fails when S3 returns no ETag", async () => {
|
|
const adapter = createS3StorageAdapter({
|
|
...baseConfig,
|
|
client: { send: vi.fn().mockResolvedValue({ CopyPartResult: {} }) },
|
|
});
|
|
|
|
await expect(
|
|
adapter.uploadPartCopy({ key: "k", uploadId: "u", partNumber: 1, sourceKey: "src" }),
|
|
).rejects.toMatchObject({ code: "put_failed", details: { operation: "uploadPartCopy" } });
|
|
});
|
|
|
|
it("uploadPartCopy wraps upstream errors", async () => {
|
|
const adapter = createS3StorageAdapter({
|
|
...baseConfig,
|
|
client: { send: vi.fn().mockRejectedValue(new Error("copy boom")) },
|
|
});
|
|
|
|
await expect(
|
|
adapter.uploadPartCopy({ key: "k", uploadId: "u", partNumber: 2, sourceKey: "src" }),
|
|
).rejects.toMatchObject({ code: "put_failed", details: { operation: "uploadPartCopy" } });
|
|
});
|
|
|
|
it("completeMultipartUpload sorts parts by number and returns the public URL", async () => {
|
|
const send = vi.fn().mockResolvedValue({});
|
|
const adapter = createS3StorageAdapter({ ...baseConfig, client: { send } });
|
|
|
|
await expect(
|
|
adapter.completeMultipartUpload({
|
|
key: "big/object.bin",
|
|
uploadId: "upload-1",
|
|
// Intentionally out of order to prove the adapter sorts them.
|
|
parts: [
|
|
{ partNumber: 2, etag: "etag-2" },
|
|
{ partNumber: 1, etag: "etag-1" },
|
|
],
|
|
}),
|
|
).resolves.toEqual({
|
|
bucket: "uploads",
|
|
key: "big/object.bin",
|
|
publicUrl: "https://cdn.example.test/assets/big/object.bin",
|
|
});
|
|
|
|
const command = send.mock.calls[0]?.[0];
|
|
expect(command).toBeInstanceOf(CompleteMultipartUploadCommand);
|
|
expect(command.input.MultipartUpload.Parts).toEqual([
|
|
{ ETag: "etag-1", PartNumber: 1 },
|
|
{ ETag: "etag-2", PartNumber: 2 },
|
|
]);
|
|
});
|
|
|
|
it("completeMultipartUpload wraps upstream errors", async () => {
|
|
const adapter = createS3StorageAdapter({
|
|
...baseConfig,
|
|
client: { send: vi.fn().mockRejectedValue(new Error("complete boom")) },
|
|
});
|
|
|
|
await expect(
|
|
adapter.completeMultipartUpload({ key: "k", uploadId: "u", parts: [] }),
|
|
).rejects.toMatchObject({ code: "put_failed", details: { operation: "completeMultipartUpload" } });
|
|
});
|
|
|
|
it("abortMultipartUpload sends an AbortMultipartUploadCommand", async () => {
|
|
const send = vi.fn().mockResolvedValue({});
|
|
const adapter = createS3StorageAdapter({ ...baseConfig, client: { send } });
|
|
|
|
await expect(
|
|
adapter.abortMultipartUpload({ key: "big/object.bin", uploadId: "upload-1" }),
|
|
).resolves.toBeUndefined();
|
|
|
|
const command = send.mock.calls[0]?.[0];
|
|
expect(command).toBeInstanceOf(AbortMultipartUploadCommand);
|
|
expect(command.input).toEqual({
|
|
Bucket: "uploads",
|
|
Key: "big/object.bin",
|
|
UploadId: "upload-1",
|
|
});
|
|
});
|
|
|
|
it("abortMultipartUpload wraps upstream errors", async () => {
|
|
const adapter = createS3StorageAdapter({
|
|
...baseConfig,
|
|
client: { send: vi.fn().mockRejectedValue(new Error("abort boom")) },
|
|
});
|
|
|
|
await expect(
|
|
adapter.abortMultipartUpload({ key: "k", uploadId: "u" }),
|
|
).rejects.toMatchObject({ code: "put_failed", details: { operation: "abortMultipartUpload" } });
|
|
});
|
|
|
|
it("omits upstream diagnostics when the thrown cause is not an object", async () => {
|
|
const adapter = createS3StorageAdapter({
|
|
...baseConfig,
|
|
client: { send: vi.fn().mockRejectedValue("plain string failure") },
|
|
});
|
|
|
|
try {
|
|
await adapter.putObject({ key: "x.txt", body: "x" });
|
|
throw new Error("expected putObject to fail");
|
|
} catch (error) {
|
|
expect(error).toMatchObject({ code: "put_failed" });
|
|
expect((error as { details: { upstream?: unknown } }).details.upstream).toBeUndefined();
|
|
}
|
|
});
|
|
});
|