Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
266 changes: 140 additions & 126 deletions console-ui/__tests__/earn-page.test.tsx
Original file line number Diff line number Diff line change
@@ -1,163 +1,177 @@
import { fireEvent, render, screen } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { dedupeModelVariants, baseModelKey, buildCatalogModels } from "@/app/earn/calc";

const apiMocks = vi.hoisted(() => ({
fetchModels: vi.fn(),
fetchPricing: vi.fn(),
}));
import { createRequire } from "node:module";
import { describe, expect, it, vi } from "vitest";
import {
CALCULATOR_MODELS,
DEFAULT_DUTY_CYCLE_PERCENT,
DECODE_BANDWIDTH_EFFICIENCY,
HARDWARE_OPTIONS,
calculateCapacityRevenue,
} from "@/app/earn/calc";
import {
MIN_PROVIDER_MEMORY_GB,
PROVIDER_HARDWARE_OPTIONS,
} from "@/app/earn/providerReadiness";

const MACBOOK_PRO = "MacBook Pro";
const MAC_STUDIO = "Mac Studio";
const M4_MAX = "M4 Max (16-core CPU)";
const BEST_ESTIMATE = "Best current estimate";
const MONTHLY_EARNING = "Estimated monthly earning";
const QWEN_DISPLAY_NAME = "Qwen3.6 35B A3B";

vi.mock("@/components/TopBar", () => ({
TopBar: ({ title }: { title?: string }) => (
<div data-testid="topbar">{title}</div>
),
TopBar: ({ title }: { title?: string }) => <div data-testid="topbar">{title}</div>,
}));

vi.mock("@/hooks/useAuth", () => ({
useAuth: () => ({
ready: true,
authenticated: true,
login: vi.fn(),
}),
}));

vi.mock("@/lib/google-analytics", () => ({
trackEvent: vi.fn(),
}));

vi.mock("@/lib/api", () => ({
fetchModels: apiMocks.fetchModels,
fetchPricing: apiMocks.fetchPricing,
useAuth: () => ({ ready: true, authenticated: true, login: vi.fn() }),
}));

beforeEach(() => {
apiMocks.fetchModels.mockReset();
apiMocks.fetchPricing.mockReset();
apiMocks.fetchModels.mockResolvedValue([
{
id: "gpt-oss-20b",
object: "model",
display_name: "GPT-OSS 20B",
size_gb: 12.1,
min_ram_gb: 24,
architecture: "MoE",
},
{
id: "gemma-4-26b",
object: "model",
display_name: "Gemma 4 26B",
size_gb: 28,
min_ram_gb: 36,
architecture: "MoE",
},
]);
apiMocks.fetchPricing.mockResolvedValue({
prices: [
{ model: "gemma-4-26b", input_price: 65_000, output_price: 200_000, input_usd: "$0.0650", output_usd: "$0.2000" },
],
vi.mock("@/lib/google-analytics", () => ({ trackEvent: vi.fn() }));

const requireFromTest = createRequire(import.meta.url);
const landingCore = requireFromTest("../../landing/earn-calculator-core.js") as {
HARDWARE_OPTIONS: typeof HARDWARE_OPTIONS;
MIN_PROVIDER_MEMORY_GB: number;
PROVIDER_HARDWARE_OPTIONS: typeof PROVIDER_HARDWARE_OPTIONS;
CALCULATOR_MODELS: typeof CALCULATOR_MODELS;
calculateCapacityRevenue: typeof calculateCapacityRevenue;
};

function selectMac(macType = MACBOOK_PRO, chip = M4_MAX, ram = 48) {
fireEvent.change(screen.getByLabelText("Mac model"), { target: { value: macType } });
fireEvent.change(screen.getByLabelText("Chip family"), { target: { value: chip } });
fireEvent.change(screen.getByLabelText("Unified memory"), {
target: { value: String(ram) },
});
}

describe("earnings projection", () => {
it("pins the three supported models, active weights, and output prices", () => {
expect(CALCULATOR_MODELS.map((model) => model.displayName)).toEqual([
QWEN_DISPLAY_NAME,
"Gemma 4 26B A4B",
"GPT-OSS 20B",
]);
expect(CALCULATOR_MODELS.map((model) => model.activeParameterCount)).toEqual([
3_000_000_000,
4_000_000_000,
3_600_000_000,
]);
expect(CALCULATOR_MODELS.map((model) => model.outputPriceMicroUSDPerMillion)).toEqual([
700_000,
220_000,
69_000,
]);
});
});

describe("model variant dedupe", () => {
const variants = [
{ id: "gpt-oss-20b", object: "model", display_name: "GPT-OSS 20B", family: "gpt-oss" },
{ id: "gemma-4-26b-qat-4bit", object: "model", display_name: "Gemma 4 26B", family: "gemma" },
{ id: "gemma-4-26b", object: "model", display_name: "Gemma 4 26B", family: "gemma" },
{ id: "gemma-4-26b-8bit", object: "model", display_name: "Gemma 4 26B 8-bit (rollback)", family: "gemma" },
];

it("strips quant / build suffixes to a base key", () => {
expect(baseModelKey("gemma-4-26b-qat-4bit")).toBe("gemma-4-26b");
expect(baseModelKey("gemma-4-26b-8bit")).toBe("gemma-4-26b");
expect(baseModelKey("gemma-4-26b")).toBe("gemma-4-26b");
expect(baseModelKey("gpt-oss-20b")).toBe("gpt-oss-20b");
it("uses 65% of bandwidth, active weights, output pricing, and duty cycle", () => {
const hardware = HARDWARE_OPTIONS.find(
(option) => option.macType === MACBOOK_PRO && option.chip === M4_MAX,
)!;
const model = CALCULATOR_MODELS[0];
const estimate = calculateCapacityRevenue(model, hardware, 48, 50)!;
expect(estimate.activeWeightGBPerToken).toBeCloseTo((3 * 22) / 35, 12);
expect(estimate.decodeTokensPerSecond).toBeCloseTo(
(hardware.bandwidthGBs * DECODE_BANDWIDTH_EFFICIENCY) / ((3 * 22) / 35),
12,
);
expect(estimate.activeSecondsPerMonth).toBe(360 * 60 * 60);
expect(estimate.outputPriceUSDPerMillion).toBe(0.7);
});

it("collapses the catalog to one canonical entry per base model", () => {
const out = dedupeModelVariants(variants);
expect(out.map((m) => m.id).sort()).toEqual(["gemma-4-26b", "gpt-oss-20b"]);
it("keeps the console and homepage data mapping and projection identical", () => {
const hardware = HARDWARE_OPTIONS.find(
(option) => option.macType === MACBOOK_PRO && option.chip === M4_MAX,
)!;
expect(landingCore.CALCULATOR_MODELS).toEqual(CALCULATOR_MODELS);
expect(
landingCore.calculateCapacityRevenue(
landingCore.CALCULATOR_MODELS[0],
hardware,
48,
50,
),
).toEqual(
calculateCapacityRevenue(CALCULATOR_MODELS[0], hardware, 48, 50),
);
});

it("buildCatalogModels yields exactly two clean models", () => {
const built = buildCatalogModels(variants, null);
expect(built.map((m) => m.name).sort()).toEqual(["GPT-OSS 20B", "Gemma 4 26B"]);
it("keeps only supported provider families and includes the new profiles", () => {
expect(landingCore.HARDWARE_OPTIONS).toEqual(HARDWARE_OPTIONS);
expect(landingCore.MIN_PROVIDER_MEMORY_GB).toBe(MIN_PROVIDER_MEMORY_GB);
expect(landingCore.PROVIDER_HARDWARE_OPTIONS).toEqual(PROVIDER_HARDWARE_OPTIONS);
expect(new Set(PROVIDER_HARDWARE_OPTIONS.map((option) => option.macType))).toEqual(
new Set([MACBOOK_PRO, "Mac Mini", MAC_STUDIO, "Mac Pro"]),
);
expect(
HARDWARE_OPTIONS.find((option) => option.macType === "Mac Mini" && option.chip === "M6"),
).toMatchObject({ bandwidthGBs: 170, ramOptions: [16, 24, 32] });
expect(
HARDWARE_OPTIONS.find(
(option) => option.macType === MAC_STUDIO && option.chip === "M5 Ultra",
),
).toMatchObject({ bandwidthGBs: 1200, ramOptions: [96, 256, 512] });
});
});

describe("EarnPage", () => {
it("shows the floor-only hero when no catalog model fits the selected hardware", async () => {
it("requires separate Mac, chip, and memory choices", async () => {
const EarnPage = (await import("@/app/earn/page")).default;
render(<EarnPage />);
await screen.findAllByText(/Runs in your 48 GB/);

fireEvent.change(screen.getByLabelText("Chip"), { target: { value: "M1" } });
fireEvent.change(screen.getByLabelText("Unified memory"), { target: { value: "16" } });

// Nothing fits in 16 GB (min RAM is 24/36 in the fixture) and the 16 GB
// floor tier is $0 — the hero degrades honestly instead of disappearing.
expect(
await screen.findByText(/No catalog model fits in 16 GB/)
).toBeInTheDocument();
expect(screen.getByText("Needs 24 GB+ of unified memory")).toBeInTheDocument();
expect(screen.getByText("Needs 36 GB+ of unified memory")).toBeInTheDocument();
expect(screen.getByText("Your estimate will appear here")).toBeInTheDocument();
expect(screen.getByLabelText("Chip family")).toBeDisabled();
expect(screen.getByLabelText("Unified memory")).toBeDisabled();
selectMac();
expect(await screen.findByText(BEST_ESTIMATE)).toBeInTheDocument();
expect(screen.getByLabelText("Duty cycle")).toHaveValue(
String(DEFAULT_DUTY_CYCLE_PERCENT),
);
});

it("lets under-provisioned machines register interest in smaller models", async () => {
window.localStorage.clear();
it("offers only Mac families that can enter the provider flow", async () => {
const EarnPage = (await import("@/app/earn/page")).default;
render(<EarnPage />);
await screen.findAllByText(/Runs in your 48 GB/);

fireEvent.change(screen.getByLabelText("Chip"), { target: { value: "M1" } });
fireEvent.change(screen.getByLabelText("Unified memory"), { target: { value: "16" } });

const notifyButton = await screen.findByRole("button", {
name: /Notify me when smaller models launch/,
});
fireEvent.click(notifyButton);

expect(
await screen.findByText(/You're on the list — we'll notify you when smaller models go live/)
).toBeInTheDocument();
expect(window.localStorage.getItem("darkbloom.smallModelsInterest")).toContain('"chip":"M1"');
Array.from(
(screen.getByLabelText("Mac model") as HTMLSelectElement).options,
(option) => option.textContent,
),
).toEqual(["Select model", MACBOOK_PRO, "Mac Mini", MAC_STUDIO, "Mac Pro"]);
});

it("always prices the best-earning model automatically (read-only list, no selection)", async () => {
it("shows the capacity flow, prominent caveat, and setup CTA in order", async () => {
const EarnPage = (await import("@/app/earn/page")).default;
render(<EarnPage />);

// GPT-OSS 20B out-earns Gemma on the default M4 Max (fewer active params →
// higher decode throughput) so it gets the badge; both fit in 48 GB.
expect(await screen.findByText("Best earner")).toBeInTheDocument();
expect(screen.getByText(/Runs in your 48 GB \(12 GB weights\)/)).toBeInTheDocument();
expect(screen.getByText(/Runs in your 48 GB \(28 GB weights\)/)).toBeInTheDocument();

// The list is read-only — no model checkboxes/buttons to mis-toggle.
expect(screen.queryByRole("button", { name: /GPT-OSS 20B/ })).not.toBeInTheDocument();
selectMac();
expect(await screen.findByText(BEST_ESTIMATE)).toBeInTheDocument();
expect(screen.getAllByText(QWEN_DISPLAY_NAME).length).toBeGreaterThan(0);
expect(screen.getByText("3. Single-stream decode speed")).toBeInTheDocument();
expect(screen.getByText("6. OpenRouter output pricing")).toBeInTheDocument();
expect(screen.getByRole("note")).toHaveTextContent("Estimated earning, not guaranteed.");
const setup = screen.getByText("Turn your Mac into a provider");
const flow = screen.getByText("How this estimate is calculated");
expect(setup.compareDocumentPosition(flow) & Node.DOCUMENT_POSITION_FOLLOWING).not.toBe(0);
});

it("presents earnings as a floor→estimate range with the base reward additive", async () => {
it("updates the earning when duty cycle changes", async () => {
const EarnPage = (await import("@/app/earn/page")).default;
render(<EarnPage />);

// Default hardware is M4 Max / 48GB → 48GB base-reward tier = $16/mo floor
// (appears in both the hero chip and the formula breakdown).
expect((await screen.findAllByText(/\$16\/mo/)).length).toBeGreaterThan(0);
expect(screen.getByText("Base rewards (earnings floor)")).toBeInTheDocument();
// Assumptions decomposition shows the floor added on top of usage.
expect(screen.getByText("+ $16")).toBeInTheDocument();
selectMac();
await screen.findByText(BEST_ESTIMATE);
const before = screen.getByText(MONTHLY_EARNING).parentElement?.textContent;
fireEvent.change(screen.getByLabelText("Duty cycle"), { target: { value: "25" } });
const after = screen.getByText(MONTHLY_EARNING).parentElement?.textContent;
expect(after).not.toBe(before);
});

it("bakes electricity in as a fixed assumption with no user input", async () => {
const { DEFAULT_ELEC_COST_PER_KWH } = await import("@/app/earn/calc");
const { useEarningsCalculator } = await import("@/app/earn/useEarningsCalculator");
const { renderHook } = await import("@testing-library/react");
const { result } = renderHook(() => useEarningsCalculator());

expect(result.current.elecCostNum).toBe(DEFAULT_ELEC_COST_PER_KWH);

it("invites interest instead of enrollment below 48 GB", async () => {
const EarnPage = (await import("@/app/earn/page")).default;
render(<EarnPage />);
expect(screen.queryByLabelText(/Electricity cost/i)).not.toBeInTheDocument();
selectMac(MACBOOK_PRO, "M4 Pro", 24);
expect(
await screen.findByText("We're starting with Macs that have 48 GB or more"),
).toBeInTheDocument();
expect(screen.getByRole("button", { name: "Register your interest" })).toBeInTheDocument();
expect(screen.queryByText(MONTHLY_EARNING)).not.toBeInTheDocument();
});
});
Loading
Loading