beads/internal/storage/embeddeddolt/issue_operations_validation_test.go
Julian Knutsen b92442d1a3
feat(storage): one guarded issue-lifecycle surface for the CLI and linked consumers (#5191)
Adds issueops.Lifecycle — Create, Update, Close, Reopen — reached from the storage handle via
store.IssueLifecycle(), implemented once and shared by the CLI and linked library consumers. Three backends
implement it and a shared conformance suite asserts them against one spec. The bd CLI's create, update,
close and reopen now route through it.

Fixes several defects that predate this change and became visible once the write paths were forced to
agree: swallowed metadata-edit failures, a claim applied before field validation, missing value validation
on title/priority/estimate, silently dropped create edges to missing targets, infra-typed creates landing in
the wrong table, a dropped SourceRepo, closed_by_session written but never read, and a compound update
writing twice.

Behavior changes needing release notes: create with an occupied explicit id now fails instead of silently
overwriting; a compound update is one atomic operation with one hook and one version commit; reparenting
replaces all parent edges; reopening something not closed reports nothing to do.
2026-07-31 11:26:38 -07:00

61 lines
2.3 KiB
Go

//go:build cgo
package embeddeddolt_test
import (
"encoding/json"
"errors"
"testing"
"github.com/steveyegge/beads/internal/types"
publicops "github.com/steveyegge/beads/issueops"
)
// TestEmbeddedIssueOperationsRejectsInvalidUpdatesWithoutMutation pins the
// shared update validation on this backend's own lifecycle, so a request the
// unit-of-work backend refuses is refused here too — and refused before any
// field in the same patch reaches the row.
func TestEmbeddedIssueOperationsRejectsInvalidUpdatesWithoutMutation(t *testing.T) {
skipUnlessEmbeddedDolt(t)
te := newTestEnv(t, "ops_validation")
ctx := t.Context()
operations, err := te.store.IssueLifecycle()
if err != nil {
t.Fatalf("IssueLifecycle: %v", err)
}
issue := &types.Issue{ID: "ops_validation-1", Title: "original", Status: types.StatusOpen, Priority: 2, IssueType: types.TypeTask, Metadata: json.RawMessage(`{"keep":"yes"}`)}
if err := te.store.CreateIssue(ctx, issue, "seed"); err != nil {
t.Fatalf("seed: %v", err)
}
mutation := publicops.Field[string]{Set: true, Value: "mutated"}
for _, tc := range []struct {
name string
patch publicops.IssuePatch
}{
{"priority above range", publicops.IssuePatch{Title: mutation, Priority: publicops.Field[int]{Set: true, Value: 9}}},
{"metadata set key with unsafe characters", publicops.IssuePatch{Title: mutation, Metadata: publicops.MetadataPatch{Set: map[string]json.RawMessage{"bad-key": json.RawMessage(`"value"`)}}}},
} {
t.Run(tc.name, func(t *testing.T) {
if _, err := operations.Update(ctx, publicops.UpdateRequest{Actor: "writer", IssueID: issue.ID, Patch: tc.patch}); !errors.Is(err, publicops.ErrValidation) {
t.Fatalf("Update error = %v, want ErrValidation", err)
}
stored, err := te.store.GetIssue(ctx, issue.ID)
if err != nil {
t.Fatalf("GetIssue: %v", err)
}
if stored.Title != "original" || stored.Priority != 2 {
t.Fatalf("rejected update mutated the row: title %q, priority %d", stored.Title, stored.Priority)
}
metadata := map[string]json.RawMessage{}
if len(stored.Metadata) > 0 {
if err := json.Unmarshal(stored.Metadata, &metadata); err != nil {
t.Fatalf("unmarshal stored metadata %s: %v", stored.Metadata, err)
}
}
if _, ok := metadata["bad-key"]; ok {
t.Fatalf("rejected update wrote metadata: %s", stored.Metadata)
}
})
}
}