import { expect, test } from "bun:test";
import { createContext, createExecutionContext, executionContextFromHttp } from "../src/index.ts";

test("unified execution context spans HTTP and background operations", async () => {
  const http = createContext(
    new Request("https://app.test/users"),
    new URL("https://app.test/users"),
  );
  http.lang = "fr";
  http.user = { id: "u1" };
  http.locals.db = { users: true };
  const execution = executionContextFromHttp(http, "action", {
    authorize: (permission) => {
      expect(permission).toBe("users.create");
    },
  });
  await execution.authorize("users.create");
  expect(execution).toMatchObject({
    kind: "action",
    locale: "fr",
    user: { id: "u1" },
    db: { users: true },
  });
  execution.response.setStatus(201);
  expect(execution.response.status).toBe(201);
  const queue = createExecutionContext({ kind: "queue", metadata: { job: "email" } });
  expect(queue.request.url).toBe("https://execution.wrnexus.invalid/queue");
});
