beads/cmd/bd/update_metadata_test.go
beads/crew/emma 51bb65644b fix(test): comprehensive Dolt skip guards across all test files
Bulk convert t.Fatalf to t.Skipf for dolt.New() connection errors in
16 test files (44 call sites). Tests now gracefully skip when Dolt
server is not available instead of failing CI.

Also improve skipIfNoDolt to check testDoltServerPort (handles macOS
CI where dolt binary exists but no server is running).

Files fixed: cli_coverage_show_test, contributor_routing_e2e_test,
doctor/{database,fix/metadata_dolt,maintenance_cgo,migration_validation,
validation}_test, gitlab_integration_test, init_test, migrate_test,
move_test, rename_prefix{,_repair}_test, sql_test, status_test,
update_metadata_test.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

Executed-By: beads/crew/emma
Rig: beads
Role: crew
2026-02-22 21:53:11 -08:00

208 lines
5.6 KiB
Go

//go:build cgo
package main
import (
"context"
"encoding/json"
"os"
"path/filepath"
"testing"
"github.com/steveyegge/beads/internal/storage/dolt"
"github.com/steveyegge/beads/internal/types"
)
// TestUpdateMetadataInlineJSON tests inline JSON metadata update
func TestUpdateMetadataInlineJSON(t *testing.T) {
tmpDir := t.TempDir()
dbPath := filepath.Join(tmpDir, ".beads", "beads.db")
// Create storage
ctx := context.Background()
store, err := dolt.New(ctx, &dolt.Config{Path: dbPath})
if err != nil {
t.Skipf("skipping: Dolt server not available: %v", err)
}
defer store.Close()
// Initialize database with issue prefix
if err := store.SetConfig(ctx, "issue_prefix", "bd"); err != nil {
t.Fatalf("failed to set issue_prefix: %v", err)
}
// Create an issue
issue := &types.Issue{
ID: "bd-test1",
Title: "Test Issue",
Status: "open",
IssueType: "task",
}
if err := store.CreateIssue(ctx, issue, "test-actor"); err != nil {
t.Fatalf("failed to create issue: %v", err)
}
// Update with inline JSON metadata
metadata := `{"key": "value", "nested": {"foo": "bar"}}`
updates := map[string]interface{}{
"metadata": json.RawMessage(metadata),
}
if err := store.UpdateIssue(ctx, "bd-test1", updates, "test-actor"); err != nil {
t.Fatalf("failed to update issue with metadata: %v", err)
}
// Verify the metadata was stored
updated, err := store.GetIssue(ctx, "bd-test1")
if err != nil {
t.Fatalf("failed to get issue: %v", err)
}
if updated.Metadata == nil {
t.Fatal("metadata should not be nil after update")
}
// Compare as parsed JSON (MySQL/Dolt normalizes JSON whitespace on storage)
var expectedJSON, actualJSON interface{}
if err := json.Unmarshal([]byte(metadata), &expectedJSON); err != nil {
t.Fatalf("failed to parse expected metadata: %v", err)
}
if err := json.Unmarshal(updated.Metadata, &actualJSON); err != nil {
t.Fatalf("failed to parse actual metadata: %v", err)
}
expectedBytes, _ := json.Marshal(expectedJSON)
actualBytes, _ := json.Marshal(actualJSON)
if string(expectedBytes) != string(actualBytes) {
t.Errorf("expected metadata %s, got %s", expectedBytes, actualBytes)
}
}
// TestUpdateMetadataFromFile tests reading metadata from a file
func TestUpdateMetadataFromFile(t *testing.T) {
tmpDir := t.TempDir()
// Create a JSON file
metadataJSON := `{"source": "file", "count": 42}`
metadataFile := filepath.Join(tmpDir, "metadata.json")
if err := os.WriteFile(metadataFile, []byte(metadataJSON), 0644); err != nil {
t.Fatalf("failed to write metadata file: %v", err)
}
// Read and validate
data, err := os.ReadFile(metadataFile)
if err != nil {
t.Fatalf("failed to read metadata file: %v", err)
}
if !json.Valid(data) {
t.Fatal("metadata file should contain valid JSON")
}
if string(data) != metadataJSON {
t.Errorf("expected %q, got %q", metadataJSON, string(data))
}
}
// TestUpdateMetadataInvalidJSON tests that invalid JSON is rejected
func TestUpdateMetadataInvalidJSON(t *testing.T) {
invalidCases := []string{
"{invalid}",
"not json",
`{"unclosed": `,
"",
}
for _, invalid := range invalidCases {
if invalid == "" {
// Empty string is not invalid for json.Valid per se, but we might want it
continue
}
if json.Valid([]byte(invalid)) {
t.Errorf("expected %q to be invalid JSON", invalid)
}
}
}
// TestUpdateMetadataValidJSON tests various valid JSON formats
func TestUpdateMetadataValidJSON(t *testing.T) {
validCases := []string{
`{}`,
`{"key": "value"}`,
`{"nested": {"deep": {"value": 123}}}`,
`{"array": [1, 2, 3]}`,
`{"mixed": [{"a": 1}, {"b": 2}]}`,
`{"unicode": "\u0048\u0065\u006c\u006c\u006f"}`,
`{"null": null}`,
`{"bool": true}`,
`{"number": 3.14159}`,
}
for _, valid := range validCases {
if !json.Valid([]byte(valid)) {
t.Errorf("expected %q to be valid JSON", valid)
}
}
}
// TestUpdateMetadataAtFileSyntax tests the @file.json parsing logic
func TestUpdateMetadataAtFileSyntax(t *testing.T) {
tests := []struct {
input string
isFile bool
filePath string
}{
{`{"inline": true}`, false, ""},
{"@metadata.json", true, "metadata.json"},
{"@/absolute/path.json", true, "/absolute/path.json"},
{"@./relative/path.json", true, "./relative/path.json"},
// Edge case: JSON that starts with @ in a string
{`{"email": "@user.com"}`, false, ""},
}
for _, tc := range tests {
// Check if starts with @ for file reference
isFile := len(tc.input) > 0 && tc.input[0] == '@'
if isFile != tc.isFile {
t.Errorf("input %q: expected isFile=%v, got %v", tc.input, tc.isFile, isFile)
}
if isFile {
filePath := tc.input[1:]
if filePath != tc.filePath {
t.Errorf("input %q: expected filePath=%q, got %q", tc.input, tc.filePath, filePath)
}
}
}
}
// TestUpdateMetadataRPCRoundtrip tests the RPC protocol for metadata
func TestUpdateMetadataRPCRoundtrip(t *testing.T) {
// Test that metadata can be marshaled/unmarshaled through RPC
metadata := `{"key": "value"}`
// Simulate UpdateArgs with metadata
type testUpdateArgs struct {
ID string `json:"id"`
Metadata *string `json:"metadata,omitempty"`
}
args := testUpdateArgs{
ID: "bd-123",
Metadata: &metadata,
}
// Marshal to JSON (as RPC would do)
data, err := json.Marshal(args)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
// Unmarshal (as server would do)
var decoded testUpdateArgs
if err := json.Unmarshal(data, &decoded); err != nil {
t.Fatalf("failed to unmarshal: %v", err)
}
if decoded.Metadata == nil {
t.Fatal("metadata should not be nil after roundtrip")
}
if *decoded.Metadata != metadata {
t.Errorf("expected %q, got %q", metadata, *decoded.Metadata)
}
}