Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions internal/kernel/usecases/export_snapshot_hash_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package usecases

import (
"testing"

"digiemu-core/internal/kernel/ports"
)

func TestSnapshotHashIgnoresCreatedAtUnix(t *testing.T) {
unit := ports.UnitDTO{
ID: "unit-1",
Key: "demo",
Title: "Demo",
Description: "Same deterministic state",
HeadVersionID: "version-1",
}

versionsA := []ports.VersionDTO{
{
ID: "version-1",
Label: "stable-label",
PrevVersionID: "",
ContentHash: "CONTENT_HASH_ABC",
ActorID: "actor-1",
CreatedAtUnix: 1000,
},
}

versionsB := []ports.VersionDTO{
{
ID: "version-1",
Label: "stable-label",
PrevVersionID: "",
ContentHash: "CONTENT_HASH_ABC",
ActorID: "actor-1",
CreatedAtUnix: 9999,
},
}

hashA := sha256HexFromLines(snapshotCanonicalLines(unit, versionsA))
hashB := sha256HexFromLines(snapshotCanonicalLines(unit, versionsB))

if hashA != hashB {
t.Fatalf("CreatedAtUnix must be outside the deterministic hash boundary: hashA=%s hashB=%s", hashA, hashB)
}
}

func TestSnapshotHashIgnoresGeneratedLabel(t *testing.T) {
unit := ports.UnitDTO{
ID: "unit-1",
Key: "demo",
Title: "Demo",
Description: "Same deterministic state",
HeadVersionID: "version-1",
}

versionsA := []ports.VersionDTO{
{
ID: "version-1",
Label: "20260507T220000.000Z",
PrevVersionID: "",
ContentHash: "CONTENT_HASH_ABC",
ActorID: "actor-1",
CreatedAtUnix: 1000,
},
}

versionsB := []ports.VersionDTO{
{
ID: "version-1",
Label: "20260507T230000.000Z",
PrevVersionID: "",
ContentHash: "CONTENT_HASH_ABC",
ActorID: "actor-1",
CreatedAtUnix: 1000,
},
}

hashA := sha256HexFromLines(snapshotCanonicalLines(unit, versionsA))
hashB := sha256HexFromLines(snapshotCanonicalLines(unit, versionsB))

if hashA != hashB {
t.Fatalf("generated Label must be outside the deterministic hash boundary: hashA=%s hashB=%s", hashA, hashB)
}
}
6 changes: 3 additions & 3 deletions internal/kernel/usecases/snapshot_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
// Format (one record per line):
//
// UNIT|<unitID>|<key>|<title>|<desc>|<headVersionID>
// VER|<id>|<label>|<prev>|<contentHash>|<actor>|<createdAtUnix>
// VER|<id>|<prev>|<contentHash>
// AUD|<id>|<type>|<atUnix>|<actor>|<unit>|<ver>|<dataCanonical>
func snapshotCanonicalLines(u ports.UnitDTO, vs []ports.VersionDTO) []string {
lines := make([]string, 0, 1+len(vs))
Expand All @@ -27,8 +27,8 @@ func snapshotCanonicalLines(u ports.UnitDTO, vs []ports.VersionDTO) []string {
))
for _, v := range vs {
lines = append(lines, fmt.Sprintf(
"VER|%s|%s|%s|%s|%s|%d",
v.ID, v.Label, v.PrevVersionID, v.ContentHash, v.ActorID, v.CreatedAtUnix,
"VER|%s|%s|%s",
v.ID, v.PrevVersionID, v.ContentHash,
))
}
return lines
Expand Down
Loading