Repository Public
1package testdb23import (4 "context"5 "crypto/rand"6 "encoding/hex"7 "sync"8 "testing"9 "time"1011 "github.com/google/uuid"12 "github.com/jackc/pgx/v5/pgxpool"1314 "clustr.spboucher.ai/mcc/internal/ids"15)1617// Synthetic fixtures for tests of the operations plane. Every node is tagged18// environment=test.1920func must(t testing.TB, err error) {21 t.Helper()22 if err != nil {23 t.Fatal(err)24 }25}2627// Node inserts a node with 10 cores.28func Node(t testing.TB, pool *pgxpool.Pool, alias string, tags ...string) uuid.UUID {29 t.Helper()30 id := ids.New()31 if tags == nil {32 tags = []string{}33 }34 _, err := pool.Exec(context.Background(), `insert into nodes(id, alias, registered_via, environment, cpu_cores, tags) values ($1,$2,'agent','test',10,$3)`, id, alias, tags)35 must(t, err)36 return id37}3839// Agent enrolls an agent on a node, seen at `seen`, declaring capabilities.40func Agent(t testing.TB, pool *pgxpool.Pool, node uuid.UUID, seen time.Time, caps ...string) uuid.UUID {41 t.Helper()42 id := ids.New()43 b := make([]byte, 8)44 rand.Read(b)45 if caps == nil {46 caps = []string{}47 }48 _, err := pool.Exec(context.Background(), `insert into agents(id, node_id, cert_serial, cert_fingerprint, cert_not_after, capabilities, last_seen_at, enrolled_at)49 values ($1,$2,$3,$4,$5,$6,$7,$7)`, id, node, hex.EncodeToString(b), b, seen.Add(30*24*time.Hour), caps, seen)50 must(t, err)51 return id52}5354// State writes the latest snapshot of a node.55func State(t testing.TB, pool *pgxpool.Pool, node uuid.UUID, at time.Time, values map[string]any) {56 t.Helper()57 if values == nil {58 values = map[string]any{}59 }60 _, err := pool.Exec(context.Background(), `insert into node_state(node_id, ts, received_at, values) values ($1,$2,$2,$3)61 on conflict (node_id) do update set ts=excluded.ts, received_at=excluded.received_at, values=excluded.values`, node, at, values)62 must(t, err)63 _, err = pool.Exec(context.Background(), `update agents set last_seen_at=$2 where node_id=$1 and revoked_at is null`, node, at)64 must(t, err)65}6667// Service inserts an observed service.68func Service(t testing.TB, pool *pgxpool.Pool, node uuid.UUID, name string, managed bool, state string) uuid.UUID {69 t.Helper()70 id := ids.New()71 _, err := pool.Exec(context.Background(), `insert into services(id, node_id, manager, name, state, managed, last_seen_at) values ($1,$2,'pm2',$3,$4,$5,now())`,72 id, node, name, state, managed)73 must(t, err)74 return id75}7677// User inserts an account directly (no passkey): for tests that do not go through HTTP.78func User(t testing.TB, pool *pgxpool.Pool, handle, role string) uuid.UUID {79 t.Helper()80 id := ids.New()81 wh := make([]byte, 32)82 rand.Read(wh)83 _, err := pool.Exec(context.Background(), `insert into users(id, handle, display_name, role, webauthn_handle) values ($1,$2,$3,$4::user_role,$5)`,84 id, handle, "User "+handle, role, wh)85 must(t, err)86 return id87}8889// Clock is a settable clock for tests, safe for concurrent use.90type Clock struct {91 mu sync.Mutex92 t time.Time93}9495func NewClock() *Clock { return &Clock{t: time.Now().UTC().Truncate(time.Microsecond)} }9697func (c *Clock) Now() time.Time {98 c.mu.Lock()99 defer c.mu.Unlock()100 return c.t101}102103func (c *Clock) Advance(d time.Duration) {104 c.mu.Lock()105 c.t = c.t.Add(d)106 c.mu.Unlock()107}108109func (c *Clock) Set(t time.Time) {110 c.mu.Lock()111 c.t = t112 c.mu.Unlock()113}114