aegida-console / lib / storage / s3.test.ts
s3.test.ts
Raw
// @vitest-environment node

import { afterEach, describe, expect, it, vi } from "vitest";

const { send } = vi.hoisted(() => ({ send: vi.fn() }));

vi.mock("server-only", () => ({}));
vi.mock("@aws-sdk/client-s3", () => ({
  GetObjectCommand: class GetObjectCommand {
    constructor(readonly input: unknown) {}
  },
  S3Client: class S3Client {
    send = send;
  },
}));

import { getAttachmentObject } from "@/lib/storage/s3";

afterEach(() => {
  vi.unstubAllEnvs();
  send.mockReset();
});

describe("legacy S3 attachment storage", () => {
  it("streams an existing private object-key row during migration retention", async () => {
    vi.stubEnv("S3_ENDPOINT", "http://localhost:9000");
    vi.stubEnv("S3_BUCKET", "chat-attachments");
    vi.stubEnv("S3_ACCESS_KEY_ID", "test");
    vi.stubEnv("S3_SECRET_ACCESS_KEY", "test");
    const stream = new ReadableStream<Uint8Array>();
    send.mockResolvedValue({ Body: { transformToWebStream: () => stream } });

    await expect(
      getAttachmentObject("users/user-1/attachments/legacy-object"),
    ).resolves.toBe(stream);

    expect(send).toHaveBeenCalledWith(
      expect.objectContaining({
        input: {
          Bucket: "chat-attachments",
          Key: "users/user-1/attachments/legacy-object",
        },
      }),
    );
  });
});