Artifact 0b4272f2b87048a914f3493b3f1471e96571005ec0d664f9b01df62d9e2c5e8c:
- File
repo_ids_test.go
— part of check-in
[61aa4dd022]
at
2026-05-04 18:59:12
on branch trunk
— feat: add Repo.UUIDFromRID for public rid->uuid lookup (#25)
Consumers (bones, EdgeSync) currently bypass the methods-only Repo API to convert RIDs to UUIDs by reaching into r.DB() and running raw SQL. The lookup is well-defined and recurs throughout the libfossil internals; expose it as a first-class method.
Add Repo.UUIDFromRID(rid int64) (string, error) backed by a small internal helper Repo.UUIDByRID on internal/repo so the SELECT lives in one place. Missing rids surface ErrArtifactNotFound (wraps sql.ErrNoRows behind a typed sentinel) with the rid in the message.
closes #23 (user: [email protected], size: 1779) [annotate] [blame] [check-ins using]
package libfossil import ( "errors" "strings" "testing" _ "github.com/danmestas/libfossil/internal/testdriver" ) func TestUUIDFromRID_HappyPath(t *testing.T) { r := newTestRepo(t) rid, uuid, err := r.Commit(CommitOpts{ Files: []FileToCommit{{Name: "hello.txt", Content: []byte("hello\n")}}, Comment: "initial", User: "test", }) if err != nil { t.Fatalf("Commit: %v", err) } got, err := r.UUIDFromRID(rid) if err != nil { t.Fatalf("UUIDFromRID(%d): %v", rid, err) } if got != uuid { t.Errorf("UUIDFromRID(%d) = %q, want %q", rid, got, uuid) } } func TestUUIDFromRID_NotFound_ZeroRID(t *testing.T) { r := newTestRepo(t) _, err := r.UUIDFromRID(0) if err == nil { t.Fatal("expected error for rid=0, got nil") } if !errors.Is(err, ErrArtifactNotFound) { t.Errorf("err = %v, want errors.Is ErrArtifactNotFound", err) } if !strings.Contains(err.Error(), "0") { t.Errorf("err = %q, want message to mention rid 0", err.Error()) } } func TestUUIDFromRID_NotFound_LargeRID(t *testing.T) { r := newTestRepo(t) // Commit something so the repo has at least one blob; the target rid is // still well above any rid the repo could plausibly have allocated. _, _, err := r.Commit(CommitOpts{ Files: []FileToCommit{{Name: "x.txt", Content: []byte("x\n")}}, Comment: "init", User: "test", }) if err != nil { t.Fatalf("Commit: %v", err) } const missing = int64(999999999) _, err = r.UUIDFromRID(missing) if err == nil { t.Fatalf("expected error for rid=%d, got nil", missing) } if !errors.Is(err, ErrArtifactNotFound) { t.Errorf("err = %v, want errors.Is ErrArtifactNotFound", err) } if !strings.Contains(err.Error(), "999999999") { t.Errorf("err = %q, want message to mention rid %d", err.Error(), missing) } }