Skip to content

Testing

Test Suites

SuiteLocationRunnerCount
Protocol schemaspackages/protocol/src/__tests__/Vitest47
Common componentspackages/common/src/__tests__/Vitest + jsdom129
Rust integrationcrates/klaxon-server/tests/cargo test4

Total: 180 tests

Running Tests

bash
# Everything
pnpm test

# TypeScript only
pnpm test:ts

# Rust only (requires DATABASE_URL)
pnpm test:rust

# Single file
pnpm --filter @ottercoders/klaxon-common exec vitest run src/__tests__/FormField.test.tsx

Mocking Pattern

Component tests mock the api transport layer:

tsx
import { invoke, listen } from "../api";

vi.mock("../api", () => ({
  invoke: vi.fn(),
  listen: vi.fn(() => Promise.resolve(() => {})),
}));

const mockInvoke = vi.mocked(invoke);

beforeEach(() => {
  mockInvoke.mockResolvedValue(undefined);
});

The invoke mock is then configured per-test to return the expected data:

tsx
mockInvoke.mockImplementation(async cmd => {
  if (cmd === "klaxon_list_open") return { items: [testItem] };
  if (cmd === "settings_get") return defaultSettings;
});

MainWidget Mock Setup

MainWidget.test.tsx has a central setupInvokeMock() that handles all commands the widget calls. When adding a new command, add a handler here or tests fail.

Rust Integration Tests

Integration tests in crates/klaxon-server/tests/integration.rs run against a real PostgreSQL instance:

bash
DATABASE_URL=postgres://klaxon:klaxon@localhost:5432/klaxon cargo test

They validate:

  • All 18 tables exist after migration
  • Enum types have correct values
  • Item CRUD lifecycle works
  • Session token hashing is deterministic

Tests skip gracefully when DATABASE_URL is not set.

CI

The GitHub Actions workflow (.github/workflows/cloud.yml) runs:

  1. pnpm typecheck — all packages
  2. pnpm test:ts — protocol + common tests
  3. cargo check + cargo clippy + cargo fmt
  4. cargo sqlx migrate run — validates migrations against fresh Postgres
  5. cargo test — integration tests against Postgres service container