import { describe, it, expect, vi, beforeEach } from "vitest";
import { GET } from "@/app/api/savings/route";

// Mock auth — authenticated session
vi.mock("@/lib/auth", () => ({
  auth: vi.fn(),
}));

import { auth } from "@/lib/auth";

describe("GET /api/savings", () => {
  beforeEach(() => {
    vi.clearAllMocks();
    vi.mocked(auth).mockResolvedValue({
      user: { id: "user_1", email: "test@test.com" },
    } as any);
  });

  it("returns 401 if no session", async () => {
    vi.mocked(auth).mockResolvedValue(null as any);
    const response = await GET();
    expect(response.status).toBe(401);
    const data = await response.json();
    expect(data.error).toBe("Unauthorized");
  });

  it("returns savings data with expected shape", async () => {
    const response = await GET();
    const data = await response.json();

    expect(response.status).toBe(200);
    expect(data.totalSavingsUsd).toBeGreaterThan(0);
    expect(data.totalCalls).toBeGreaterThan(0);
    expect(data.avgSavingsPct).toBeGreaterThan(0);
    expect(data.models.length).toBeGreaterThan(0);
    expect(data.dailySavings.length).toBeGreaterThan(0);
    expect(data.models[0]).toHaveProperty("savingsUsd");
    expect(data.models[0]).toHaveProperty("cloudCostUsd");
    expect(data.models[0]).toHaveProperty("localCostUsd");
  });
});
