Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥
TypeScript aiai-agentsai-crawlerai-scrapingai-searchcrawlerdata-extractionhtml-to-markdownllmmarkdownscraperscrapingweb-crawlerweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
import { vi } from "vitest";
// vi.mock is hoisted; factory-referenced values must be created in vi.hoisted().
const { withAuth, queueBillingOperation, trackCredits, refundCredits } =
vi.hoisted(() => ({
withAuth: vi.fn((fn: any) => fn),
queueBillingOperation: vi.fn<(args: any[]) => Promise<any>>(),
trackCredits: vi.fn<(args: any) => Promise<boolean>>(),
refundCredits: vi.fn<(args: any) => Promise<void>>(),
}));
vi.mock("../../../lib/withAuth", () => ({
withAuth,
}));
vi.mock("../batch_billing", () => ({
queueBillingOperation: (...args: any[]) => queueBillingOperation(args),
}));
vi.mock("../../autumn/autumn.service", () => ({
autumnService: {
trackCredits,
refundCredits,
hostedMcpFeatureId: async () => "MCP_CREDITS",
},
featureIdForBillingEndpoint: (endpoint?: string) =>
endpoint === "search" ? "SEARCH_CREDITS" : "CREDITS",
}));
vi.mock("../../notification/email_notification", () => ({
sendNotification: vi.fn(),
}));
vi.mock("../../redis", () => ({
getValue: vi.fn(),
setValue: vi.fn(),
}));
vi.mock("../../../lib/logger", () => ({
logger: {
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
},
}));
import { billTeam } from "../credit_billing";
beforeEach(() => {
vi.clearAllMocks();
queueBillingOperation.mockResolvedValue({ success: true });
trackCredits.mockResolvedValue(true);
refundCredits.mockResolvedValue(undefined);
});
describe("billTeam", () => {
it("carries the caller's external request id on the charge, and omits it without one", async () => {
await billTeam("team-1", "org-1", 3, 123, {
endpoint: "scrape",
jobId: "job-9",
chargeId: "job-9",
externalRequestId: "partner-op-42",
});
expect(trackCredits).toHaveBeenLastCalledWith(
expect.objectContaining({ externalRequestId: "partner-op-42" }),
);
await billTeam("team-1", "org-1", 3, 123, {
endpoint: "scrape",
jobId: "job-9",
externalRequestId: null,
});
expect(trackCredits).toHaveBeenLastCalledWith(
expect.objectContaining({ externalRequestId: undefined }),
);
});
it("derives firebill idempotency keys from chargeId, and omits them without one", async () => {
await billTeam("team-1", "org-1", 3, 123, {
endpoint: "search",
jobId: "job-9",
chargeId: "job-9",
});
expect(trackCredits).toHaveBeenLastCalledWith(
expect.objectContaining({ idempotencyKey: "fc:track:search:job-9" }),
);
await billTeam("team-1", "org-1", 3, 123, {
endpoint: "search",
jobId: "job-9",
});
expect(trackCredits).toHaveBeenLastCalledWith(
expect.objectContaining({ idempotencyKey: undefined }),
);
});
it("skips the compensating refund when queueing fails — the charge stands", async () => {
queueBillingOperation.mockResolvedValueOnce({
success: false,
message: "enqueue failed",
});
trackCredits.mockResolvedValueOnce(true);
await billTeam("team-1", "org-1", 3, 123, {
endpoint: "map",
jobId: "map-1",
chargeId: "map-1",
});
expect(refundCredits).not.toHaveBeenCalled();
});
it("marks billing as already tracked when request tracking succeeds", async () => {
await billTeam("team-1", "org-1", 3, 123, {
endpoint: "search",
jobId: "job-1",
});
expect(queueBillingOperation).toHaveBeenCalledWith([
"team-1",
"org-1",
3,
123,
{ endpoint: "search", jobId: "job-1" },
false,
true,
]);
expect(trackCredits).toHaveBeenCalledWith({
teamId: "team-1",
orgId: "org-1",
value: 3,
properties: {
source: "billTeam",
endpoint: "search",
jobId: "job-1",
apiKeyId: 123,
},
featureId: "SEARCH_CREDITS",
});
});
// preview/keyless teams have no org, and this is the one boundary that
// accepts that: the operation is still enqueued, Autumn is simply not told.
it("skips Autumn entirely when the team has no org", async () => {
await billTeam("preview_abc", null, 3, null, {
endpoint: "search",
jobId: "job-1",
});
expect(trackCredits).not.toHaveBeenCalled();
expect(refundCredits).not.toHaveBeenCalled();
expect(queueBillingOperation).toHaveBeenCalledWith([
"preview_abc",
null,
3,
null,
{ endpoint: "search", jobId: "job-1" },
false,
false,
]);
});
it("leaves batch tracking enabled when request tracking is off", async () => {
trackCredits.mockResolvedValueOnce(false);
await billTeam("team-1", "org-1", 3, 123, {
endpoint: "search",
jobId: "job-1",
});
expect(queueBillingOperation).toHaveBeenCalledWith([
"team-1",
"org-1",
3,
123,
{ endpoint: "search", jobId: "job-1" },
false,
false,
]);
expect(refundCredits).not.toHaveBeenCalled();
});
});
describe("billTeam for a hosted MCP call", () => {
const hostedMcpScrape = {
endpoint: "scrape" as const,
jobId: "job-1",
chargeId: "job-1",
hostedMcp: true,
};
it("charges the whole call on MCP credits and queues the billing operation", async () => {
await billTeam("team-1", "org-1", 3, 123, hostedMcpScrape);
expect(trackCredits).toHaveBeenCalledExactlyOnceWith(
expect.objectContaining({
value: 3,
featureId: "MCP_CREDITS",
idempotencyKey: "fc:track:scrape:job-1",
}),
);
expect(queueBillingOperation).toHaveBeenCalledWith([
"team-1",
"org-1",
3,
123,
hostedMcpScrape,
false,
true,
]);
});
});