package contracts

import "testing"

func TestDigestArtifactSchemaUsesExactBytes(t *testing.T) {
	first := ArtifactSchema{JSONSchema: []byte(`{"type":"object"}`)}
	second := ArtifactSchema{JSONSchema: []byte("{\n  \"type\": \"object\"\n}")}

	if got := DigestArtifactSchema(first); got != "sha256:a2c799262a3ce3c19ef5cdd983bf3d12b43ab3c426227091b909dcb7054738c0" {
		t.Fatalf("DigestArtifactSchema() = %q, want stable SHA-256", got)
	}
	if DigestArtifactSchema(first) == DigestArtifactSchema(second) {
		t.Fatal("schema digests match for different exact bytes")
	}
}

func TestArtifactCloneHelpersOwnSlicesAndMaps(t *testing.T) {
	schema := ArtifactSchema{ID: "notes.v1", Name: "notes", Version: "v1", JSONSchema: []byte(`{"type":"object"}`)}
	artifact := SerializedArtifact{
		Kind:      "test/notes",
		Schema:    schema,
		MediaType: "application/json",
		Content:   []byte(`{"items":["one"]}`),
		Metadata:  map[string]any{"origin": "test"},
	}

	clonedSchema := CloneArtifactSchema(schema)
	cloned := CloneSerializedArtifact(artifact)
	schema.JSONSchema[0] = '['
	artifact.Content[0] = '['
	artifact.Metadata["origin"] = "changed"

	if string(clonedSchema.JSONSchema) != `{"type":"object"}` {
		t.Fatalf("cloned schema = %q, want original bytes", clonedSchema.JSONSchema)
	}
	if string(cloned.Schema.JSONSchema) != `{"type":"object"}` {
		t.Fatalf("serialized artifact schema = %q, want original bytes", cloned.Schema.JSONSchema)
	}
	if string(cloned.Content) != `{"items":["one"]}` {
		t.Fatalf("cloned content = %q, want original bytes", cloned.Content)
	}
	if cloned.Metadata["origin"] != "test" {
		t.Fatalf("cloned metadata = %#v, want independent map", cloned.Metadata)
	}
}
