import { Autumn } from "autumn-js"; const FAKE_URL = "https://totallynotarealdomaingeneratedbyclaudetomakethetestsworktofail.co"; let passed = 0; let failed = 0; function assert(label: string, condition: boolean) { if (condition) { console.log(` PASS: ${label}`); passed++; } else { console.error(` FAIL: ${label}`); failed++; } } async function testCheckFailOpen() { console.log("\n--- check() fail-open (enabled by default) ---"); const autumn = new Autumn({ secretKey: "sk_fake", serverURL: FAKE_URL, failOpen: true, }); const result = await autumn.check({ customerId: "cus_123", featureId: "messages", }); assert("allowed is true", result.allowed === true); assert("customerId is empty string", result.customerId === ""); assert("balance is null", result.balance === null); console.log("Response:", JSON.stringify(result, null, 2)); } async function testTrackFailOpen() { console.log("\n--- track() fail-open (enabled by default) ---"); const autumn = new Autumn({ secretKey: "sk_fake", serverURL: FAKE_URL, }); const result = await autumn.track({ customerId: "cus_123", featureId: "messages", value: 1, }); assert("customerId is empty string", result.customerId === ""); assert("value is 0", result.value === 0); assert("balance is null", result.balance === null); console.log("Response:", JSON.stringify(result, null, 2)); } async function testGetCustomerFailOpen() { console.log( "\n--- customers.getOrCreate() fail-open (enabled by default) ---", ); const autumn = new Autumn({ secretKey: "sk_fake", serverURL: FAKE_URL, }); const result = await autumn.customers.getOrCreate({ customerId: "cus_123", }); assert("id is null", result.id === null); assert("createdAt is 0", result.createdAt === 0); assert( "subscriptions is empty array", Array.isArray(result.subscriptions) && result.subscriptions.length === 0, ); assert("balances is empty object", Object.keys(result.balances).length === 0); console.log("Response:", JSON.stringify(result, null, 2)); } async function testFailOpenDisabled() { console.log("\n--- check() with failOpen: false (should throw) ---"); const autumn = new Autumn({ secretKey: "sk_fake", serverURL: FAKE_URL, failOpen: false, }); try { await autumn.check({ customerId: "cus_123", featureId: "messages", }); assert("should have thrown", false); } catch (err) { assert("correctly threw error", true); console.log(` Error: ${(err as Error).message}`); } } async function main() { console.log("=== Autumn SDK Fail-Open Tests ==="); await testCheckFailOpen(); await testTrackFailOpen(); await testGetCustomerFailOpen(); await testFailOpenDisabled(); console.log(`\n=== Results: ${passed} passed, ${failed} failed ===`); if (failed > 0) { process.exit(1); } } main().catch(console.error);