package responseschema import ( "crypto/sha256" "encoding/hex" "testing" ) const ( expectedCorrectionSetSHA256 = "05f8ff3fa04f68115c0cb1859d2656f51aa5c0bae8ff2470b2d4f6f531953195" expectedValidatorDecisionSetSHA256 = "b73f4790b98fbb955f0aec5496dd8ce9a8fe14aa2f35c700b4b4e5634f106fd5" ) func TestLookupKnownSchemas(t *testing.T) { correction, ok := Lookup(CorrectionSetKey) if !ok { t.Fatalf("expected correction-set schema to be registered") } if correction.ID != correctionSetSchemaID { t.Fatalf("unexpected correction-set schema id %q", correction.ID) } if correction.Version != schemaVersionV1 { t.Fatalf("unexpected correction-set schema version %q", correction.Version) } if correction.Name != "audita_correction_set_v1" { t.Fatalf("unexpected correction-set schema name %q", correction.Name) } validation, ok := Lookup(ValidatorDecisionSetKey) if !ok { t.Fatalf("expected validator-decision schema to be registered") } if validation.ID != validatorDecisionSchemaID { t.Fatalf("unexpected validator-decision schema id %q", validation.ID) } if validation.Version != schemaVersionV1 { t.Fatalf("unexpected validator-decision schema version %q", validation.Version) } if validation.Name != "audita_validator_decision_set_v1" { t.Fatalf("unexpected validator-decision schema name %q", validation.Name) } } func TestLookupUnknownSchema(t *testing.T) { if _, ok := Lookup(Key("missing")); ok { t.Fatalf("expected unknown schema lookup to fail") } } func TestSchemaHashesMatchRegisteredJSON(t *testing.T) { expectedByKey := map[Key]string{ CorrectionSetKey: expectedCorrectionSetSHA256, ValidatorDecisionSetKey: expectedValidatorDecisionSetSHA256, } for key, expectedHash := range expectedByKey { schema, ok := Lookup(key) if !ok { t.Fatalf("missing schema %q", key) } sum := sha256.Sum256(schema.JSONSchema) expected := hex.EncodeToString(sum[:]) if schema.SHA256 != expectedHash { t.Fatalf("unexpected stable hash for %q: got %q want %q", key, schema.SHA256, expectedHash) } if schema.SHA256 != expected { t.Fatalf("unexpected hash for %q: got %q want %q", key, schema.SHA256, expected) } } } func TestLookupReturnsSchemaCopy(t *testing.T) { schema, ok := Lookup(CorrectionSetKey) if !ok { t.Fatalf("missing correction-set schema") } if len(schema.JSONSchema) == 0 { t.Fatalf("expected non-empty schema payload") } schema.JSONSchema[0] = 'x' again, ok := Lookup(CorrectionSetKey) if !ok { t.Fatalf("missing correction-set schema on second lookup") } if len(again.JSONSchema) == 0 { t.Fatalf("unexpected empty schema payload") } if again.JSONSchema[0] != '{' { t.Fatalf("expected lookup to return independent schema copy") } }