Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,29 @@ bun run dev # build sidecar → Tauri dev (Vite on :1420)
bun run dev:ui # Vite UI only; pair with VITE_SIDECAR_URL
bun run dev:sidecar # go run the sidecar on 127.0.0.1:7420
bun run build # tsc --noEmit && vite build (the UI typecheck gate)
bun run lint # biome check . (the UI lint + format gate)
bun run lint:fix # biome check --write . — fixes what CI's `biome ci .` would fail on
bun run build:sidecar # scripts/build-sidecar.sh → src-tauri/binaries/
bun run version 0.1.0-x # sync package.json + Cargo.toml
bun run version:check # fail if Cargo.toml ≠ root package.json version
bun run tauri:build # sidecar + installers (.dmg/.app, .deb/AppImage)

cd sidecar && go build ./... && go vet ./... # sidecar checks
cd sidecar && go build ./... && go vet ./... && go test ./... # sidecar checks
```

There are **no tests yet** — 32 Go files, zero `*_test.go`, and no JS test runner. Verification
is the build: `bun run build` for the UI, `go build ./... && go vet ./...` for the sidecar. CI
(`.github/workflows/ci.yml`) runs exactly those plus `go test ./...` and `bun run version:check`.
CI also gates `gofmt` on `sidecar/` — fix a failure with `gofmt -w sidecar/`. There is no
formatter or linter configured for the TypeScript side; `tsc --noEmit` is the only gate there.
The sidecar has Go tests; the frontend has **no test runner at all**. Coverage is deliberately
partial — pure logic that is security-relevant or easy to get subtly wrong (`server/auth`,
`k8s/resources` redaction, `audit`, `engine`, `compose`, `helm`, `domains/proxy`, and
`docker/{client,run,stats,volume_ops}`). Anything needing a live daemon is untested, so add a
`*_test.go` next to the logic you touch rather than assuming one exists. Where a method shells
out and then parses the output, split the parser into its own function (`parseComposeLs`,
`parseComposePs`, `parseReleaseList`) so it can be tested without a `docker` or `helm` binary.
Verification for the UI is `bun run build` (typecheck) plus `bun run lint`; for the sidecar it is
`go build ./... && go vet ./... && go test ./...`.

CI (`.github/workflows/ci.yml`) runs exactly those, plus `bun run version:check`. It gates
`gofmt` on `sidecar/` — fix a failure with `gofmt -w sidecar/` — and gates the TypeScript side
with `biome ci .`, which checks lint *and* formatting; fix a failure with `bun run lint:fix`.

xterm.js is code-split behind `ExecTerminalLazy` (~85 KB gzipped, the largest single dependency).
Import that wrapper rather than `ExecTerminal` directly, or the terminal lands back in the initial
Expand Down
136 changes: 136 additions & 0 deletions sidecar/internal/audit/audit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package audit

import (
"errors"
"os"
"path/filepath"
"testing"
"time"
)

func newTestLogger(t *testing.T) *Logger {
t.Helper()
return New(filepath.Join(t.TempDir(), "nested", "audit.jsonl"))
}

func TestNewCreatesParentDirectory(t *testing.T) {
l := newTestLogger(t)
if _, err := os.Stat(filepath.Dir(l.Path())); err != nil {
t.Fatalf("parent directory not created: %v", err)
}
}

func TestLogAndTailRoundTrip(t *testing.T) {
l := newTestLogger(t)
l.Log("container.remove", "web", "force=true", nil)
l.Log("volume.prune", "", "", errors.New("daemon unreachable"))

events, err := l.Tail(0)
if err != nil {
t.Fatalf("Tail: %v", err)
}
if len(events) != 2 {
t.Fatalf("Tail returned %d events, want 2", len(events))
}

ok := events[0]
if ok.Action != "container.remove" || ok.Target != "web" || ok.Detail != "force=true" {
t.Errorf("first event = %+v, want the container.remove fields", ok)
}
if !ok.OK || ok.Error != "" {
t.Errorf("nil error should record OK with no message, got ok=%v error=%q", ok.OK, ok.Error)
}
if _, err := time.Parse(time.RFC3339, ok.Time); err != nil {
t.Errorf("Time %q is not RFC3339: %v", ok.Time, err)
}

failed := events[1]
if failed.OK {
t.Error("an event logged with an error should not be OK")
}
if failed.Error != "daemon unreachable" {
t.Errorf("Error = %q, want the error's message", failed.Error)
}
}

func TestTailReturnsTheLastNEvents(t *testing.T) {
l := newTestLogger(t)
for _, action := range []string{"a", "b", "c", "d"} {
l.Log(action, "", "", nil)
}

events, err := l.Tail(2)
if err != nil {
t.Fatalf("Tail: %v", err)
}
if len(events) != 2 || events[0].Action != "c" || events[1].Action != "d" {
t.Fatalf("Tail(2) = %+v, want the newest two (c, d)", events)
}

// A request for more than exists is clamped rather than erroring, so the
// UI's fixed tail size works against a short log.
all, err := l.Tail(100)
if err != nil {
t.Fatalf("Tail: %v", err)
}
if len(all) != 4 {
t.Errorf("Tail(100) returned %d events, want all 4", len(all))
}
}

func TestTailOnMissingLogIsNotAnError(t *testing.T) {
// The audit log does not exist until the first mutating operation, and the
// UI polls GET /api/audit before then.
l := &Logger{path: filepath.Join(t.TempDir(), "absent.jsonl")}
events, err := l.Tail(10)
if err != nil {
t.Fatalf("Tail on a missing file: %v", err)
}
if len(events) != 0 {
t.Errorf("Tail on a missing file returned %d events, want none", len(events))
}
}

func TestTailSkipsUnparseableLines(t *testing.T) {
path := filepath.Join(t.TempDir(), "audit.jsonl")
// A truncated write (crash mid-append) must not poison the whole tail.
body := `{"time":"2026-01-01T00:00:00Z","action":"good","ok":true}` + "\n" +
"{not json" + "\n" +
"\n" +
`{"time":"2026-01-01T00:00:01Z","action":"also-good","ok":true}` + "\n"
if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
t.Fatal(err)
}

events, err := (&Logger{path: path}).Tail(0)
if err != nil {
t.Fatalf("Tail: %v", err)
}
if len(events) != 2 {
t.Fatalf("Tail returned %d events, want the 2 parseable ones", len(events))
}
if events[0].Action != "good" || events[1].Action != "also-good" {
t.Errorf("Tail = %+v, want the two well-formed events in order", events)
}
}

func TestLogFileIsOwnerOnly(t *testing.T) {
// The log records container names, image refs, and host paths; it is
// created 0600 so other local accounts cannot read it.
l := newTestLogger(t)
l.Log("image.pull", "nginx:latest", "", nil)

st, err := os.Stat(l.Path())
if err != nil {
t.Fatal(err)
}
if perm := st.Mode().Perm(); perm != 0o600 {
t.Errorf("audit log mode = %o, want 600", perm)
}
}

func TestNilLoggerIsANoOp(t *testing.T) {
// Handlers call s.audit.Log unconditionally, so a nil logger must not panic.
var l *Logger
l.Log("container.stop", "web", "", nil)
}
13 changes: 13 additions & 0 deletions sidecar/internal/compose/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ func (s *Service) List(ctx context.Context) ([]Project, error) {
if err != nil {
return nil, err
}
return parseComposeLs(out)
}

// parseComposeLs turns `docker compose ls --format json` output into projects.
// Split out from List so it can be exercised without a docker binary.
func parseComposeLs(out string) ([]Project, error) {
out = strings.TrimSpace(out)
if out == "" || out == "null" {
return []Project{}, nil
Expand Down Expand Up @@ -239,6 +245,13 @@ func (s *Service) Services(ctx context.Context, req UpRequest) ([]ServiceInfo, e
if err != nil {
return nil, err
}
return parseComposePs(out)
}

// parseComposePs turns `docker compose ps --format json` output into services.
// Docker emits either a JSON array or NDJSON depending on version, so both
// shapes are handled; split out from Services to be testable without docker.
func parseComposePs(out string) ([]ServiceInfo, error) {
out = strings.TrimSpace(out)
if out == "" || out == "null" {
return []ServiceInfo{}, nil
Expand Down
Loading
Loading