Artifact c0114683aa96b3ed49fd1035f28dfa2f086c7f7b5210d9f7491f8aab9f6355a1:
- File
internal/sync/handler_crosslink_test.go
— part of check-in
[527759acd5]
at
2026-07-19 18:31:23
on branch trunk
— fix: distinguish absent, empty, and valued U-cards in manifest parsing (#73)
* fix: distinguish absent, empty, and valued U-cards in manifest parsing
Manifest parsing collapsed a wholly absent U-card and a present-but-empty U-card into the same stored value (empty string), so every crosslink call site that wrote the check-in user into event/attachment/forumpost rows lost the distinction between "no login recorded" (should be SQL NULL) and "login explicitly blanked" (canonical fossil resolves this to the literal "anonymous" at parse time — see src/manifest.c:1008-1016).
Change deck.Deck.U from string to *string so the three states are representable: nil (absent), non-nil "anonymous" (present-empty, resolved in parseUCard the same way fossil's own parser does), and non-nil with a value (present). Resolving once in the parser means every downstream crosslink/rebuild call site can keep binding d.U directly — Go's database/sql converts a nil *string argument to SQL NULL and dereferences a non-nil one automatically, so no per-call-site special casing is needed. The type change itself makes the old ambiguous string-binding path a compile error everywhere Deck.U is constructed, closing off the drift that caused issue #29.
Added deck.Str, a small pointer-literal helper, since Go can't take the address of a string literal directly; existing tests that built Deck{U: "..."} now go through it.
* fix: review nits — whitespace U-cards, rebuild coverage, tag.AddTag, naming
Four follow-ups from review of the U-card presence fix:
- deck.parseUCard now trims args before the emptiness check, matching canonical fossil's next_token() (skips leading whitespace) and every sibling card parser in this file. Previously "U \n" stored literal whitespace instead of resolving to "anonymous". Added a whitespace-only case to TestParseUCardStates.
- Added TestRebuild_UCardPresenceStates covering the rebuild path (rebuild_manifest.go's rebuildCheckin), mirroring TestCrosslink_UCardPresenceStates. The binding site was already correct (same nil-to-NULL pointer semantics as crosslink.go) but had no dedicated regression test.
- tag.AddTag now panics on an empty opts.User, matching the panic idiom CheckinOpts.User already establishes. Previously a tag created with an empty User silently crosslinked to user=''; that asymmetry with CheckinOpts was a wart worth closing rather than documenting as intentional.
- Renamed deck.Str to deck.User: the name should say what the helper constructs (a Deck.U value), not how it's implemented (a pointer take). (user: [email protected], size: 6169) [annotate] [blame] [check-ins using]
package sync import ( "context" "fmt" "testing" "time" "github.com/danmestas/libfossil/internal/blob" "github.com/danmestas/libfossil/internal/deck" "github.com/danmestas/libfossil/internal/hash" "github.com/danmestas/libfossil/internal/xfer" ) // buildCheckinManifest constructs a checkin manifest referencing the given // file blob and returns the wire bytes plus the manifest UUID. Parents may // be empty for a root-of-tree checkin. func buildCheckinManifest(t *testing.T, comment string, when time.Time, fileUUID string, fileContent []byte, parents []string) ([]byte, string) { t.Helper() d := &deck.Deck{ Type: deck.Checkin, C: comment, D: when, F: []deck.FileCard{{Name: "hello.txt", UUID: fileUUID}}, U: deck.User("tester"), P: parents, T: []deck.TagCard{ {Type: deck.TagPropagating, Name: "branch", UUID: "*", Value: "trunk"}, {Type: deck.TagSingleton, Name: "sym-trunk", UUID: "*"}, }, } rHash, err := d.ComputeR(func(uuid string) ([]byte, error) { if uuid == fileUUID { return fileContent, nil } return nil, fmt.Errorf("unknown uuid: %s", uuid) }) if err != nil { t.Fatalf("ComputeR: %v", err) } d.R = rHash manifestBytes, err := d.Marshal() if err != nil { t.Fatalf("Marshal: %v", err) } return manifestBytes, hash.SHA1(manifestBytes) } // TestHandleSync_CrosslinksReceivedManifest verifies that a manifest pushed // to the server via HandleSync is crosslinked into event/leaf/plink/mlink. // // Pre-fix, HandleSync stored the blob durably but did not call the // crosslink scanner, so the hub's timeline, fork detection, and clone // protocol all observed empty relational state. func TestHandleSync_CrosslinksReceivedManifest(t *testing.T) { r := setupSyncTestRepo(t) fileContent := []byte("hello from crosslink test") fileUUID := hash.SHA1(fileContent) manifestBytes, manifestUUID := buildCheckinManifest(t, "first checkin", time.Date(2026, 4, 25, 12, 0, 0, 0, time.UTC), fileUUID, fileContent, nil) req := &xfer.Message{Cards: []xfer.Card{ &xfer.PushCard{ServerCode: "test", ProjectCode: "test"}, &xfer.FileCard{UUID: fileUUID, Content: fileContent}, &xfer.FileCard{UUID: manifestUUID, Content: manifestBytes}, }} if _, err := HandleSync(context.Background(), r, req); err != nil { t.Fatalf("HandleSync: %v", err) } manifestRID, ok := blob.Exists(r.DB(), manifestUUID) if !ok { t.Fatal("manifest blob not stored") } // event table: one row of type='ci' for the checkin. var eventCount int if err := r.DB().QueryRow( "SELECT count(*) FROM event WHERE type='ci' AND objid=?", manifestRID, ).Scan(&eventCount); err != nil { t.Fatalf("count event: %v", err) } if eventCount != 1 { t.Fatalf("event ci rows for objid=%d: got %d, want 1", manifestRID, eventCount) } // leaf table: the checkin is a tip. var leafCount int if err := r.DB().QueryRow( "SELECT count(*) FROM leaf WHERE rid=?", manifestRID, ).Scan(&leafCount); err != nil { t.Fatalf("count leaf: %v", err) } if leafCount != 1 { t.Fatalf("leaf rows for rid=%d: got %d, want 1", manifestRID, leafCount) } // mlink table: one row mapping the manifest to its file. var mlinkCount int if err := r.DB().QueryRow( "SELECT count(*) FROM mlink WHERE mid=?", manifestRID, ).Scan(&mlinkCount); err != nil { t.Fatalf("count mlink: %v", err) } if mlinkCount != 1 { t.Fatalf("mlink rows for mid=%d: got %d, want 1", manifestRID, mlinkCount) } } // TestHandleSync_LeafTransitionOnChildCheckin verifies that when a child // checkin lands via HandleSync, the parent is removed from leaf and the // child becomes the new tip. This is the operation that produces phantom // "WouldFork" reports in v0.4.0 when crosslink is missing. func TestHandleSync_LeafTransitionOnChildCheckin(t *testing.T) { r := setupSyncTestRepo(t) parentFileContent := []byte("parent file content") parentFileUUID := hash.SHA1(parentFileContent) parentBytes, parentUUID := buildCheckinManifest(t, "parent", time.Date(2026, 4, 25, 12, 0, 0, 0, time.UTC), parentFileUUID, parentFileContent, nil) childFileContent := []byte("child file content") childFileUUID := hash.SHA1(childFileContent) childBytes, childUUID := buildCheckinManifest(t, "child", time.Date(2026, 4, 25, 13, 0, 0, 0, time.UTC), childFileUUID, childFileContent, []string{parentUUID}) // Push the parent first. if _, err := HandleSync(context.Background(), r, &xfer.Message{Cards: []xfer.Card{ &xfer.PushCard{ServerCode: "test", ProjectCode: "test"}, &xfer.FileCard{UUID: parentFileUUID, Content: parentFileContent}, &xfer.FileCard{UUID: parentUUID, Content: parentBytes}, }}); err != nil { t.Fatalf("HandleSync(parent): %v", err) } parentRID, _ := blob.Exists(r.DB(), parentUUID) // Confirm parent is a leaf. var leafBefore int r.DB().QueryRow("SELECT count(*) FROM leaf WHERE rid=?", parentRID).Scan(&leafBefore) if leafBefore != 1 { t.Fatalf("after parent push: leaf rows for parent=%d got %d, want 1", parentRID, leafBefore) } // Push the child. if _, err := HandleSync(context.Background(), r, &xfer.Message{Cards: []xfer.Card{ &xfer.PushCard{ServerCode: "test", ProjectCode: "test"}, &xfer.FileCard{UUID: childFileUUID, Content: childFileContent}, &xfer.FileCard{UUID: childUUID, Content: childBytes}, }}); err != nil { t.Fatalf("HandleSync(child): %v", err) } childRID, _ := blob.Exists(r.DB(), childUUID) // Parent must be gone from leaf, child must replace it. var parentLeaf, childLeaf int r.DB().QueryRow("SELECT count(*) FROM leaf WHERE rid=?", parentRID).Scan(&parentLeaf) r.DB().QueryRow("SELECT count(*) FROM leaf WHERE rid=?", childRID).Scan(&childLeaf) if parentLeaf != 0 { t.Errorf("parent leaf count = %d, want 0 (child should have superseded)", parentLeaf) } if childLeaf != 1 { t.Errorf("child leaf count = %d, want 1", childLeaf) } // plink table: one row connecting parent to child. var plinkCount int r.DB().QueryRow( "SELECT count(*) FROM plink WHERE pid=? AND cid=?", parentRID, childRID, ).Scan(&plinkCount) if plinkCount != 1 { t.Errorf("plink rows pid=%d cid=%d: got %d, want 1", parentRID, childRID, plinkCount) } }