package artifactmodel import ( "encoding/json" "reflect" "testing" ) func TestArtifactMetadataJSON(t *testing.T) { type envelope struct { Contract *ContractMetadata `json:"contract,omitempty"` ExternalProvenance *ExternalProvenance `json:"external_provenance,omitempty"` } complete, err := json.Marshal(envelope{ Contract: &ContractMetadata{ MediaType: "application/json", SchemaID: "notarius.dnd.npc_registry", SchemaVersion: "v1", ModuleKey: "dnd/npc-registry", }, ExternalProvenance: &ExternalProvenance{ System: "notarius", RunID: "run-123", PipelineID: "dnd-session", ArtifactID: "npc-registry", }, }) if err != nil { t.Fatalf("Marshal() complete metadata error = %v", err) } var got map[string]any if err := json.Unmarshal(complete, &got); err != nil { t.Fatal(err) } want := map[string]any{ "contract": map[string]any{"media_type": "application/json", "schema_id": "notarius.dnd.npc_registry", "schema_version": "v1", "module_key": "dnd/npc-registry"}, "external_provenance": map[string]any{"system": "notarius", "run_id": "run-123", "pipeline_id": "dnd-session", "artifact_id": "npc-registry"}, } if !reflect.DeepEqual(got, want) { t.Fatalf("complete metadata = %#v, want %#v", got, want) } omitted, err := json.Marshal(envelope{}) if err != nil { t.Fatalf("Marshal() omitted metadata error = %v", err) } got = nil if err := json.Unmarshal(omitted, &got); err != nil { t.Fatal(err) } if len(got) != 0 { t.Fatalf("omitted metadata JSON = %s, want {}", omitted) } withoutModule, err := json.Marshal(envelope{Contract: &ContractMetadata{ MediaType: "application/json", SchemaID: "notarius.dnd.npc_registry", SchemaVersion: "v1", }}) if err != nil { t.Fatalf("Marshal() contract without module key error = %v", err) } got = nil if err := json.Unmarshal(withoutModule, &got); err != nil { t.Fatal(err) } want = map[string]any{"contract": map[string]any{"media_type": "application/json", "schema_id": "notarius.dnd.npc_registry", "schema_version": "v1"}} if !reflect.DeepEqual(got, want) { t.Fatalf("metadata without module = %#v, want %#v", got, want) } }