package main import ( "bytes" "strings" "testing" "time" ) func storeReplyWithScanning(sc *apiScanBlock) apiStoreReply { at := time.Date(2026, 9, 1, 10, 0, 0, 0, time.UTC) return apiStoreReply{ App: "titogo", Backend: "aws", Declared: &apiStoreDeclared{EnvPrefix: "CARLOS_STORE", By: "m@example.com", At: at}, Provisioned: &apiStoreProvisioned{ Bucket: "carlos-bab-titogo", Region: "eu-west-1", IAMUser: "carlos-bab-titogo", KeyID: "AKIA1", At: at, }, EnvNames: []string{"CARLOS_STORE_BUCKET"}, MissingVars: []string{}, Scanning: sc, } } // store status must mention scanning once it exists, and must not imply // coverage — or enforcement — for a store that only has it recorded. func TestPrintStoreStatusShowsScanState(t *testing.T) { at := time.Date(2026, 9, 1, 10, 0, 0, 0, time.UTC) // Enabled but NOT enforcing: the line must say infected objects are not // blocked. Someone reading "scanning: clamav" and assuming otherwise is // the exact misunderstanding this line exists to prevent. var b bytes.Buffer printStoreStatus(&b, storeReplyWithScanning(&apiScanBlock{ Requested: &apiScanRequested{At: at, By: "m@example.com"}, Enabled: &apiScanEnabled{Engine: "clamav", At: at, By: "op@example.com"}, })) if out := b.String(); !strings.Contains(out, "recording only") || !strings.Contains(out, "NOT blocked") { t.Fatalf("an enabled-but-not-enforcing store did not say so:\n%s", out) } // Enforcing. var b2 bytes.Buffer printStoreStatus(&b2, storeReplyWithScanning(&apiScanBlock{ Requested: &apiScanRequested{At: at, By: "m@example.com"}, Enabled: &apiScanEnabled{Engine: "clamav", At: at, By: "op@example.com"}, Enforce: &apiScanEnforce{Key: "carlos-scan", Value: "infected"}, })) if out := b2.String(); !strings.Contains(out, "enforcing") { t.Fatalf("an enforcing store did not say so:\n%s", out) } // Requested only: a member waiting on an operator, with no claim of // anything having looked at an object. var b3 bytes.Buffer printStoreStatus(&b3, storeReplyWithScanning(&apiScanBlock{ Requested: &apiScanRequested{At: at, By: "m@example.com"}, })) out3 := b3.String() if !strings.Contains(out3, "no engine enabled yet") { t.Fatalf("a requested-only store did not say it is waiting:\n%s", out3) } if strings.Contains(out3, "enforcing") { t.Fatalf("a requested-only store implied enforcement:\n%s", out3) } // Requested and attested encrypted. var b4 bytes.Buffer printStoreStatus(&b4, storeReplyWithScanning(&apiScanBlock{ Requested: &apiScanRequested{At: at, By: "m@example.com", Encrypted: true}, })) if out := b4.String(); !strings.Contains(out, "no coverage will be claimed") { t.Fatalf("an encrypted store's status implied coverage:\n%s", out) } } // A store with no scanning block prints exactly what it prints today — every // existing deployment sees no change at all. func TestPrintStoreStatusUnchangedWithoutScanning(t *testing.T) { var b bytes.Buffer printStoreStatus(&b, storeReplyWithScanning(nil)) if strings.Contains(b.String(), "scanning") { t.Fatalf("a store that never opted in grew a scanning line:\n%s", b.String()) } // An empty block is the same case: a record can carry `scanning: {}` after // a revoke cleared both halves, and that is not a state worth a line. var b2 bytes.Buffer printStoreStatus(&b2, storeReplyWithScanning(&apiScanBlock{})) if strings.Contains(b2.String(), "scanning") { t.Fatalf("an emptied scanning block still printed a line:\n%s", b2.String()) } }