2.0 KiB
2.0 KiB
General Test Guide
Test Context
All tests have access to ctx which contains:
ctx.org- Test organizationctx.db- Database connectionctx.features- Organization features
Initializing Autumn Clients
Secret Key (Default)
const autumnV1 = new AutumnInt({ version: ApiVersion.V1_2 });
Public Key
const autumnPublic = new AutumnInt({
version: ApiVersion.V1_2,
secretKey: ctx.org.test_pkey!,
});
With Custom Config
const autumn = new AutumnInt({
version: ApiVersion.V1_2,
orgConfig: { include_past_due: true },
});
API Versions
ApiVersion.V0_2- Legacy v0 APIApiVersion.V1_2- Current v1 API
Common Test Patterns
Wait for Async Processing
await new Promise((resolve) => setTimeout(resolve, 2000));
Get Customer with Feature Balance
const customer: any = await autumn.customers.get(customerId);
const balance = customer.features[TestFeature.Messages].balance;
const used = customer.features[TestFeature.Messages].used;
Expect Error
import { expectAutumnError } from "tests/utils/expectUtils/expectErrUtils.js";
await expectAutumnError({
errCode: ErrCode.CustomerNotFound,
func: async () => {
await autumn.customers.get("invalid-id");
},
});
Public Key Restrictions
Public keys can only access:
GET /v1/productsPOST /v1/entitledPOST /v1/checkPOST /v1/attachGET /v1/customers/:customerId
Public keys CANNOT:
- Send events (
send_event: trueis silently ignored) - Access other endpoints
Test Organization
beforeAll- Setup (create customers, products, attach)test- Individual test cases- Use descriptive test names with
chalk.yellowBright()
Imports
import { beforeAll, describe, expect, test } from "bun:test";
import { ApiVersion, ErrCode } from "@autumn/shared";
import chalk from "chalk";
import ctx from "tests/utils/testInitUtils/createTestContext.js";
import { AutumnInt } from "@/external/autumn/autumnCli.js";