2026-08-09 23:20:32 +00:00
|
|
|
package artifactmodel
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
2026-12-02 21:10:04 +00:00
|
|
|
"reflect"
|
2026-08-09 23:20:32 +00:00
|
|
|
"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)
|
|
|
|
|
}
|
2026-12-02 21:10:04 +00:00
|
|
|
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)
|
2026-08-09 23:20:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
omitted, err := json.Marshal(envelope{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Marshal() omitted metadata error = %v", err)
|
|
|
|
|
}
|
2026-12-02 21:10:04 +00:00
|
|
|
got = nil
|
|
|
|
|
if err := json.Unmarshal(omitted, &got); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if len(got) != 0 {
|
2026-08-09 23:20:32 +00:00
|
|
|
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)
|
|
|
|
|
}
|
2026-12-02 21:10:04 +00:00
|
|
|
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)
|
2026-08-09 23:20:32 +00:00
|
|
|
}
|
|
|
|
|
}
|