OKHAM.org Documentation EnglishCastellano

February 2026 edition

Testing Specification

Disclaimer: This document is non-normative and describes recommended practices for the OKHAM ecosystem.

Version 1.0.0

Status: DRAFT

This document specifies the testing requirements, coverage expectations, and patterns for all OKHAM capabilities.


Testing Requirements by Component

Component Unit Tests Integration Tests E2E Tests
Capability handlers REQUIRED RECOMMENDED OPTIONAL
Runtime (pkg/capability-runtime) REQUIRED RECOMMENDED OPTIONAL
UI components OPTIONAL OPTIONAL RECOMMENDED
Bus interactions OPTIONAL RECOMMENDED OPTIONAL
HTTP endpoints REQUIRED RECOMMENDED OPTIONAL

Coverage Expectations

Component Minimum Target
pkg/capability-runtime/ 80% 90%
internal/capabilities/*/ 70% 80%
internal/ui/ 60% 70%
Overall project 70% 80%

Test File Naming

file.go           → file_test.go
handlers_http.go  → handlers_http_test.go
service.go        → service_test.go

Test Function Naming

// Pattern: Test<Function>_<Scenario>
func TestHandleListCameras_ReturnsEmptyList(t *testing.T)
func TestHandleListCameras_ReturnsAllCameras(t *testing.T)
func TestHandleListCameras_ErrorOnDBFailure(t *testing.T)

// Table-driven tests
func TestParseConfig(t *testing.T) {
    tests := []struct {
        name    string
        input   string
        want    Config
        wantErr bool
    }{
        {"valid config", "...", Config{}, false},
        {"empty input", "", Config{}, true},
    }
    // ...
}

Test Categories

Unit Tests

  • Test single functions/methods in isolation
  • Mock external dependencies
  • Fast execution (<1s per test)
  • No network/database calls
func TestValidateCoordinates_ValidInput(t *testing.T) {
    err := ValidateCoordinates(40.7128, -74.0060)
    assert.NoError(t, err)
}

Integration Tests

  • Test interactions between components
  • May use test database/NATS
  • Moderate execution time (<10s per test)
  • Use build tag: //go:build integration
//go:build integration

func TestCameraRegistration_EndToEnd(t *testing.T) {
    // Setup test NATS, test DB
    // Register camera via API
    // Verify in database
    // Verify event published
}

E2E Tests

  • Test full system behavior
  • Use real or containerized infrastructure
  • Use build tag: //go:build e2e
  • Run in CI, not pre-commit
//go:build e2e

func TestAdminUI_CameraList(t *testing.T) {
    // Start containers
    // Navigate to UI
    // Verify cameras displayed
}

Mocking Guidelines

Use Interfaces for Dependencies

// REQUIRED - Interface allows mocking
type CameraStore interface {
    Get(id string) (*Camera, error)
    List() ([]*Camera, error)
}

// Test with mock
type mockStore struct {
    cameras []*Camera
}

func (m *mockStore) List() ([]*Camera, error) {
    return m.cameras, nil
}

Mock External Services

// Mock NATS for bus tests
mockBus := &MockBusClient{
    PublishFunc: func(subject string, data []byte) error {
        // Capture published messages
        return nil
    },
}

Test Data

Use Fixtures

internal/capabilities/camera-mgmt/
├── testdata/
│   ├── camera_valid.json
│   ├── camera_invalid.json
│   └── import_sample.csv

Load Test Data

func loadTestCamera(t *testing.T) *Camera {
    data, err := os.ReadFile("testdata/camera_valid.json")
    require.NoError(t, err)
    var cam Camera
    require.NoError(t, json.Unmarshal(data, &cam))
    return &cam
}

Test Isolation

Tests MUST be isolated:

  • No shared state between tests
  • Use t.Parallel() where safe
  • Clean up resources in t.Cleanup()
func TestCameraOperations(t *testing.T) {
    t.Parallel()

    db := setupTestDB(t)
    t.Cleanup(func() { db.Close() })

    // Test code...
}

Flaky Tests

Flaky tests are NOT acceptable:

  1. If a test fails intermittently, fix it immediately
  2. Do not skip flaky tests without issue tracking
  3. Time-dependent tests must use mocked time
// BAD: Time-dependent
func TestExpiry(t *testing.T) {
    time.Sleep(2 * time.Second)  // Flaky!
}

// GOOD: Mocked time
func TestExpiry(t *testing.T) {
    clock := &MockClock{Now: time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)}
    // Advance clock as needed
}

Pre-Commit Requirements

Before committing, developers MUST run:

# Unit tests (fast)
go test ./...

# With coverage
go test -cover ./...