Files
cfw-autumn/server/tests
2025-11-11 18:04:55 +00:00
..
wip
2025-11-03 17:36:41 +00:00
2025-11-07 16:52:17 +00:00
2025-11-08 19:31:38 +00:00
2025-09-22 14:45:25 +01:00
2025-10-08 09:16:42 +01:00
2025-10-22 22:03:52 +01:00
2025-10-24 10:11:16 +01:00
2025-10-31 19:40:25 +00:00
2025-10-24 10:11:16 +01:00

Autumn Test Suite

This directory contains all tests for the Autumn server application.

Quick Start

# Run setup to initialize the test organization
bun tests setup

# Run a specific test group
bun tests g1

# Run a test group with setup
bun tests g1 setup

# Run a specific test file (fuzzy matching)
bun tests basic1

Test Structure

Test Organization

Tests are organized into logical groups that can be run via shell scripts in server/shell/:

  • g1.sh - Check API tests
  • g2.sh - Attach API tests (basic, upgrade, downgrade)
  • g3.sh - Attach API tests (checkout, entities, free, addOn)
  • g4.sh - Product and pricing tests
  • g5.sh - Customer and subscription tests
  • g6.sh - Reward and referral tests

Test Files

  • setupMain.ts - Main setup script that clears and initializes the test organization
  • global.ts - Shared test data (features, products, rewards, etc.)
  • utils/ - Test utilities and helper functions
    • setupUtils/clearOrg.ts - Clears test org data
    • setupUtils/setupOrg.ts - Sets up test org with features and products
    • setup.ts - Re-exports setup utilities
    • init.ts - Test initialization helpers
    • stripeUtils.ts - Stripe-specific test utilities

Test Setup Process

The setup process (setupMain.ts) does the following:

  1. Clears the test organization (clearOrg):

    • Reconnects Stripe with test credentials
    • Resets the default Stripe account (automatically removes all Stripe resources)
    • Deletes all customers from database
    • Deletes all products from database
    • Deletes all rewards from database
    • Deletes all features from database
  2. Sets up the test organization (setupOrg):

    • Creates all test features
    • Creates all test products with pricing and entitlements
    • Creates all test rewards/coupons
    • Creates all reward triggers (referral programs)
    • Initializes Stripe products if running in parallel mode

Running Tests

Setup Command

The setup command runs the setupMain.ts script to initialize the test environment:

bun tests setup

This is required before running any tests to ensure a clean state.

Shell Scripts

Shell scripts in server/shell/ define test groups. Each script:

  • Can run setup automatically if "setup" is passed as an argument
  • Runs multiple test files in parallel or serial mode
  • Uses configuration from server/shell/config.sh

Example:

# Run g1 tests with setup
bun tests g1 setup

# Run g1 tests without setup (assumes setup was already run)
bun tests g1

Individual Test Files

You can run individual test files by name (fuzzy matching):

# Runs the first matching test file
bun tests basic1

# Runs with path pattern
bun tests attach/basic1

The test runner will:

  1. Search for matching test files
  2. Automatically detect if it's a Bun test or Mocha test
  3. Run with the appropriate test runner

Test Frameworks

Tests can use either:

  • Bun Test - Files with from "bun:test" import (faster)
  • Mocha - Traditional mocha tests (legacy)

The test runner auto-detects which framework to use based on imports.

Environment Variables

Required environment variables for tests:

  • TESTS_ORG - Test organization slug (e.g., "unit-test-org")
  • TESTS_ORG_ID - Test organization ID
  • STRIPE_TEST_KEY - Stripe test secret key
  • UNIT_TEST_AUTUMN_SECRET_KEY - Autumn API secret key for tests
  • UNIT_TEST_AUTUMN_PUBLIC_KEY - Autumn API public key for tests

Configuration

Test configuration is defined in server/shell/config.sh:

MOCHA_SETUP="bun tests/setupMain.ts"
MOCHA_CMD="bunx mocha --parallel -j 6 --timeout 10000000 --ignore tests/00_setup.ts"
BUN_CMD="bun test --concurrent"

Best Practices

  1. Always run setup first - Use bun tests setup or bun tests <group> setup to ensure clean state
  2. Use parallel execution - Set MOCHA_PARALLEL=true for faster test runs
  3. Isolate test data - Each test should be independent and not rely on other tests
  4. Clean up resources - Tests should clean up any resources they create
  5. Use descriptive names - Test files should have clear, descriptive names

Internal API Endpoints

The test suite uses internal API endpoints for efficiency:

  • POST /organization/reset_default_account - Resets the default Stripe account (clears all Stripe data)
    • Only works for TESTS_ORG_ID
    • Only works in sandbox environment
    • Faster than manually deleting Stripe resources

Troubleshooting

Tests failing with "Org not found"

Run setup: bun tests setup

Stripe errors

Check that STRIPE_TEST_KEY is set correctly

Cache issues

The setup script automatically invalidates cache for test API keys

Parallel execution issues

If tests interfere with each other, try running in serial mode by setting MOCHA_PARALLEL=false