Skip to content

Testing

Tests are how we build quality in instead of inspecting it afterwards (see Principles). A change isn’t done until it’s tested and CI is green.

Test-first by default

Our default is test-driven development:

  1. Write a test that describes the behaviour you want, and watch it fail.
  2. Write the simplest code that makes it pass.
  3. Refactor, with the test keeping you safe.

It’s most valuable where mistakes are expensive and behaviour is easy to describe:

  • Detection rules: a test with the traffic or event that must (and must not) raise an alert. A false positive can stop a production line.
  • Compliance controls: a test with a passing and a failing resource for each control.
  • Bug fixes: first a test that reproduces the bug, then the fix. The bug can’t come back unnoticed.

Where tests live

In safetybitsio/safetybits:

KindWhereHow
Unit tests (Go)Next to the code, as *_test.go files in the same packagemake test
Integration tests (Go)The PostgreSQL layers in backend and intelRun against a real PostgreSQL database with the migrations applied, not mocks
Detection rulesagent/internal/network/detection/rulesOne test file per rule or family of rules
Compliance controlsbackend/internal/platform/risk/controlsTests per control
FrontendNo automated tests yet: lint and a production build only

Prefer testing behaviour through public functions over testing private details, so refactoring doesn’t break tests that should still pass.

What CI checks

Every pull request runs the CI workflow. All checks must pass before merging:

JobChecks
Backendmake build, database migrations for backend and intel, then make test
Backend / Lintgolangci-lint
Frontendyarn lint and yarn build
PR TitleThe title follows Conventional Commits

If CI fails on main, fixing it comes before any new work.

Before you open a pull request

  • Run make test locally for the parts you changed.
  • Add or update tests for the behaviour you changed. A PR that changes behaviour without touching a test needs a reason in its description.
  • Don’t skip or delete a failing test to get CI green. Fix it, or open an issue and explain why in the PR.

Gaps we know about

  • Frontend: no automated tests. Until we add them, test UI changes by hand and describe what you checked in the PR.
  • End-to-end: no automated tests of a full deployment (sensor to console). New plant deployments follow the New Plant Deployment checklist.