import { expect, test } from "bun:test";
import { randomUUID } from "node:crypto";
import { rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { deleteObject, putObject, resolveReadable } from "../src/storage";

test("resolveReadable returns a local path as-is when STORAGE_DRIVER=local", () => {
  process.env.STORAGE_DRIVER = "local";
  expect(resolveReadable("/tmp/x.csv")).toBe("/tmp/x.csv");
});
test("deleteObject is a no-op for local driver and does not throw", async () => {
  process.env.STORAGE_DRIVER = "local";
  await deleteObject("/tmp/x.csv");
  expect(true).toBe(true);
});
test("putObject writes bytes to disk (incl. intermediate dirs) for local driver", async () => {
  process.env.STORAGE_DRIVER = "local";
  const dir = join(tmpdir(), "analytics-storage-test", randomUUID());
  const path = join(dir, "a.gz");
  try {
    await putObject(path, new Uint8Array([1, 2, 3]));
    expect(await Bun.file(path).exists()).toBe(true);
    expect(new Uint8Array(await Bun.file(path).arrayBuffer())).toEqual(
      new Uint8Array([1, 2, 3]),
    );
  } finally {
    await rm(dir, { recursive: true, force: true });
  }
});
