package store import ( "bytes" "crypto/sha256" "testing" "github.com/google/uuid" ) func TestCanonicalAttachmentIDsSortsAndCopies(t *testing.T) { first, second, third := uuid.MustParse("00000000-0000-0000-0000-000000000003"), uuid.MustParse("00000000-0000-0000-0000-000000000001"), uuid.MustParse("00000000-0000-0000-0000-000000000002") input := []uuid.UUID{first, second, third} got := canonicalAttachmentIDs(input) want := []uuid.UUID{second, third, first} if !equalIDs(got, want) { t.Fatalf("canonicalAttachmentIDs = %v, want %v", got, want) } if !equalIDs(input, []uuid.UUID{first, second, third}) { t.Fatalf("canonicalAttachmentIDs mutated its input: %v", input) } } func TestCanonicalAttachmentIDsReturnsEmptySliceForNil(t *testing.T) { got := canonicalAttachmentIDs(nil) if got == nil { t.Fatal("canonicalAttachmentIDs(nil) = nil, want empty slice") } if len(got) != 0 { t.Fatalf("canonicalAttachmentIDs(nil) = %v, want empty", got) } } // TestMessageFingerprintWithoutAttachmentsPreservesLab3Hash is the // compatibility guard: idempotency rows written before attachments existed // must still match on replay. func TestMessageFingerprintWithoutAttachmentsPreservesLab3Hash(t *testing.T) { expected := sha256.Sum256([]byte("hello")) if got := messageFingerprint("hello", nil); !bytes.Equal(got, expected[:]) { t.Fatalf("fingerprint = %x, want %x", got, expected) } if got := messageFingerprint("hello", []uuid.UUID{}); !bytes.Equal(got, expected[:]) { t.Fatalf("empty slice fingerprint = %x, want %x", got, expected) } } func TestMessageFingerprintDependsOnSortedAttachments(t *testing.T) { first := uuid.MustParse("00000000-0000-0000-0000-000000000001") second := uuid.MustParse("00000000-0000-0000-0000-000000000002") // CreateMessage canonicalizes before hashing; the pair must therefore be // order independent even though messageFingerprint itself takes the list // as given. one := messageFingerprint("hello", canonicalAttachmentIDs([]uuid.UUID{first, second})) reordered := messageFingerprint("hello", canonicalAttachmentIDs([]uuid.UUID{second, first})) if !bytes.Equal(one, reordered) { t.Fatal("fingerprint depends on attachment order") } other := messageFingerprint("hello", canonicalAttachmentIDs([]uuid.UUID{first})) if bytes.Equal(one, other) { t.Fatal("fingerprint ignores attachment identity") } } func equalIDs(a, b []uuid.UUID) bool { if len(a) != len(b) { return false } for index := range a { if a[index] != b[index] { return false } } return true }