beads/cmd/bd/markdown_test.go
Julian Knutsen 1f1e82ab08
fix(issueops): one default priority for every create path (#7426)
* fix(issueops): one create priority default; HTTP absent priority is P2

The CLI defaulted `bd create` to P2, but issues:create, issues:batchCreate
and batch:apply create items left Issue.Priority at 0 when `priority` was
absent, so a gc/curl create stored P0 (critical) against the spec's "absent
means the default".

The default now lives in one place, the shared create preparation
(PreparePublicCreateRequest), behind a presence flag because Issue.Priority's
zero value is P0:
- issueops.CreateRequest.DefaultPriority, BatchCreateItem.DefaultPriority and
  CreateItem.DefaultPriority ask for issueops.DefaultCreatePriority (2);
  setting it with a non-zero priority is ErrValidation
  (ValidateCreatePriority, shared with the HTTP client);
- every Lifecycle.Create, BatchCreator and BatchApplier leg (dolt, embedded,
  uow) reaches it; CloneCreateBatchRequest now keeps scalar item members;
- the three handlers set the flag for an absent member and never make up 0;
  the HTTP client omits `priority` for it (wirePriority) only when the
  server advertises the new additive issues.create.defaultPriority token,
  read from the cached handshake before the dial; an older server reads an
  absent priority as P0, so the client sends DefaultCreatePriority (2)
  explicitly to it (pinCreateDefaultPriority). An explicit 0 is still sent;
- `bd create` without --priority, `bd create --graph` nodes without priority
  and `bd create --file` templates without ### Priority rely on the flag; the
  flag's displayed default and the dry-run previews read the library
  constant. CLI output is byte-identical (checked against origin/main).

Spec: the three priority descriptions now say the create default is P2 on
a server advertising issues.create.defaultPriority (apigen regenerated); the
wireshape golden already had priority optional, so no wire-shape change.

Conformance: RunLifecycleCreateAppliesTheDefaultPriority,
RunBatchCreatorAppliesTheDefaultPriority and
RunBatchApplyAppliesTheDefaultPriority on dolt, embedded, uow and http, plus
handler and client encoding tests, and
TestCreateSendsTheDefaultPriorityToAServerThatPredatesIt (new client,
handshake without the token: all three create roles send 2).

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

* fix(issueops): apply review fixups for the default create priority (bd-kb61wg)

Apply the minor and nit findings from the review of #7426:

- CHANGELOG: tell out-of-tree storage backends they must honor
  DefaultPriority (store issueops.DefaultCreatePriority, refuse it with a
  non-zero priority as ErrValidation) and which conformance cases check
  it; scope "CLI output is unchanged" to the in-tree backends.
- bd create --file: pin the default-priority path. The e2e test asserts
  the printed priorities, and a buildMarkdownBatchRequest unit test pins
  the flag and priority for an absent, a 0 and a 1 `### Priority`.
  Deleting `DefaultPriority: !template.PrioritySet` now fails both.
- bd create --file: warn on stderr about a `### Priority` that does not
  parse, as the `### Type` case already does, instead of dropping it
  silently.
- httpclient: document pinCreateDefaultPriority's same-build assumption
  (the handshake is cached per Store, so a stale one is silent here), and
  test its no-context and failed-handshake branches on all three roles.
- Formula steps, `bd q` and `bd todo add` spell
  issueops.DefaultCreatePriority instead of the literal 2.
- Conformance: the batch-creator and batch-apply default-priority cases
  also pin DefaultPriority with a non-zero priority as ErrValidation with
  nothing written.

Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(issueops): mark the out-of-tree DefaultPriority change BREAKING (bd-0tcn1o)

Apply the minor findings from the relaunch review of #7426:

- CHANGELOG: move the obligation on out-of-tree storage backends out of
  the `### Fixed` entry into its own `### Changed` entry, led by
  "BREAKING (out-of-tree storage backends)" as the close-guard change
  and the earlier out-of-tree contract changes are; RELEASING.md asks
  breaking changes to carry their migration. The entry says what such a
  backend used to receive from `bd create`, `--graph` and `--file`
  (priority 2), what it receives now (priority 0 with DefaultPriority
  set), the migration, and the conformance cases that check it. The
  `### Fixed` entry keeps the HTTP P2 fix, points at the new entry, and
  names the stale-handshake residual that only a code comment named: a
  create that lands on an older build behind the same URL still stores
  P0.
- bd cook: the proto root, the gate issue and the cooked formula root
  spell issueops.DefaultCreatePriority instead of the literal 2, as the
  formula steps in the same file already do. No behavior change.

Not taken:
- The literal 2s in create_form.go, gate.go and linear.go, and the
  "default 2" in todo.go's --priority help: explicit P2s outside the
  migrated paths (the help string also feeds the generated CLI
  reference). Left for a follow-up sweep.
- Reusing in.priorityDefault in the direct bd create route: that route
  builds no createInput (gatherCreateInput runs only on the proxied and
  --file routes, and each returns), so there is nothing to reuse.
- A member-level range check in the single-create HTTP decode, as the
  batch shapes have: the asymmetry predates this PR, and the role still
  refuses an out-of-range priority on all three shapes.

Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Julian Knutsen
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 01:37:27 +00:00

345 lines
8.2 KiB
Go

package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"github.com/steveyegge/beads/issueops"
)
func TestParseMarkdownFile(t *testing.T) {
tests := []struct {
name string
content string
expected []*IssueTemplate
wantErr bool
}{
{
name: "simple issue",
content: `## Fix authentication bug
This is a critical bug in the auth system.
### Priority
1
### Type
bug
`,
expected: []*IssueTemplate{
{
Title: "Fix authentication bug",
Description: "This is a critical bug in the auth system.",
Priority: 1,
IssueType: "bug",
},
},
},
{
name: "multiple issues",
content: `## First Issue
Description for first issue.
### Priority
0
### Type
feature
## Second Issue
Description for second issue.
### Priority
2
### Type
task
`,
expected: []*IssueTemplate{
{
Title: "First Issue",
Description: "Description for first issue.",
Priority: 0,
IssueType: "feature",
},
{
Title: "Second Issue",
Description: "Description for second issue.",
Priority: 2,
IssueType: "task",
},
},
},
{
name: "issue with all fields",
content: `## Comprehensive Issue
Initial description text.
### Priority
1
### Type
feature
### Description
Detailed description here.
Multi-line support.
### Design
Design notes go here.
### Acceptance Criteria
- Must do this
- Must do that
### Assignee
alice
### Labels
backend, urgent
### Dependencies
bd-10, bd-20
`,
expected: []*IssueTemplate{
{
Title: "Comprehensive Issue",
Description: "Detailed description here.\nMulti-line support.",
Design: "Design notes go here.",
AcceptanceCriteria: "- Must do this\n- Must do that",
Priority: 1,
IssueType: "feature",
Assignee: "alice",
Labels: []string{"backend", "urgent"},
Dependencies: []string{"bd-10", "bd-20"},
},
},
},
{
name: "dependencies with types",
content: `## Issue with typed dependencies
### Priority
2
### Type
task
### Dependencies
blocks:bd-10, discovered-from:bd-20
`,
expected: []*IssueTemplate{
{
Title: "Issue with typed dependencies",
Priority: 2,
IssueType: "task",
Dependencies: []string{"blocks:bd-10", "discovered-from:bd-20"},
},
},
},
{
name: "default values",
content: `## Minimal Issue
Just a title and description.
`,
expected: []*IssueTemplate{
{
Title: "Minimal Issue",
Description: "Just a title and description.",
Priority: 2, // default
IssueType: "task", // default
},
},
},
{
name: "GH-4643 H2-per-section briefing file parses as three issues",
content: `## Problem
Something is broken.
## Context
Extra background.
## Acceptance Criteria
It should work.
`,
expected: []*IssueTemplate{
{
Title: "Problem",
Description: "Something is broken.",
Priority: 2,
IssueType: "task",
},
{
Title: "Context",
Description: "Extra background.",
Priority: 2,
IssueType: "task",
},
{
Title: "Acceptance Criteria",
Description: "It should work.",
Priority: 2,
IssueType: "task",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create temp file
tmpDir := t.TempDir()
tmpFile := filepath.Join(tmpDir, "test.md")
if err := os.WriteFile(tmpFile, []byte(tt.content), 0600); err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
// Parse file
got, err := parseMarkdownFile(tmpFile)
if (err != nil) != tt.wantErr {
t.Errorf("parseMarkdownFile() error = %v, wantErr %v", err, tt.wantErr)
return
}
if len(got) != len(tt.expected) {
t.Errorf("parseMarkdownFile() got %d issues, want %d", len(got), len(tt.expected))
return
}
// Compare each issue
for i, gotIssue := range got {
wantIssue := tt.expected[i]
if gotIssue.Title != wantIssue.Title {
t.Errorf("Issue %d: Title = %q, want %q", i, gotIssue.Title, wantIssue.Title)
}
if gotIssue.Description != wantIssue.Description {
t.Errorf("Issue %d: Description = %q, want %q", i, gotIssue.Description, wantIssue.Description)
}
if gotIssue.Priority != wantIssue.Priority {
t.Errorf("Issue %d: Priority = %d, want %d", i, gotIssue.Priority, wantIssue.Priority)
}
if gotIssue.IssueType != wantIssue.IssueType {
t.Errorf("Issue %d: IssueType = %q, want %q", i, gotIssue.IssueType, wantIssue.IssueType)
}
if gotIssue.Design != wantIssue.Design {
t.Errorf("Issue %d: Design = %q, want %q", i, gotIssue.Design, wantIssue.Design)
}
if gotIssue.AcceptanceCriteria != wantIssue.AcceptanceCriteria {
t.Errorf("Issue %d: AcceptanceCriteria = %q, want %q", i, gotIssue.AcceptanceCriteria, wantIssue.AcceptanceCriteria)
}
if gotIssue.Assignee != wantIssue.Assignee {
t.Errorf("Issue %d: Assignee = %q, want %q", i, gotIssue.Assignee, wantIssue.Assignee)
}
// Compare slices
if !stringSlicesEqual(gotIssue.Labels, wantIssue.Labels) {
t.Errorf("Issue %d: Labels = %v, want %v", i, gotIssue.Labels, wantIssue.Labels)
}
if !stringSlicesEqual(gotIssue.Dependencies, wantIssue.Dependencies) {
t.Errorf("Issue %d: Dependencies = %v, want %v", i, gotIssue.Dependencies, wantIssue.Dependencies)
}
}
})
}
}
func TestParseMarkdownFile_FileNotFound(t *testing.T) {
_, err := parseMarkdownFile("/nonexistent/file.md")
if err == nil {
t.Error("Expected error for non-existent file, got nil")
}
}
// An unparseable `### Priority` leaves the template to the role's default, and
// says so on stderr the way an unparseable `### Type` does.
func TestParseMarkdownFileWarnsOnAnInvalidPriority(t *testing.T) {
path := filepath.Join(t.TempDir(), "plan.md")
if err := os.WriteFile(path, []byte("## Urgent-ish\n\n### Priority\nurgent\n"), 0o600); err != nil {
t.Fatalf("write plan: %v", err)
}
var templates []*IssueTemplate
stderr := captureStderr(t, func() {
var err error
if templates, err = parseMarkdownFile(path); err != nil {
t.Errorf("parseMarkdownFile: %v", err)
}
})
want := fmt.Sprintf("Warning: invalid priority 'urgent' in 'Urgent-ish', using default P%d", issueops.DefaultCreatePriority)
if !strings.Contains(stderr, want) {
t.Errorf("stderr = %q, want %q", stderr, want)
}
if len(templates) != 1 || templates[0].PrioritySet {
t.Fatalf("templates = %+v, want one template that leaves its priority to the role", templates)
}
}
// A template without `### Priority` asks the role for its default instead of
// spelling 2; one that names a priority, 0 included, sends it without the ask.
func TestBuildMarkdownBatchRequestLeavesAnAbsentPriorityToTheRole(t *testing.T) {
path := filepath.Join(t.TempDir(), "plan.md")
plan := `## Names no priority
## Names priority zero
### Priority
0
## Names priority one
### Priority
1
`
if err := os.WriteFile(path, []byte(plan), 0o600); err != nil {
t.Fatalf("write plan: %v", err)
}
templates, err := parseMarkdownFile(path)
if err != nil {
t.Fatalf("parseMarkdownFile: %v", err)
}
request, err := buildMarkdownBatchRequest(templates, createInput{markdownFile: path})
if err != nil {
t.Fatalf("buildMarkdownBatchRequest: %v", err)
}
want := []struct {
title string
priority int
defaultPriority bool
}{
{"Names no priority", 0, true},
{"Names priority zero", 0, false},
{"Names priority one", 1, false},
}
if len(request.Items) != len(want) {
t.Fatalf("request has %d items, want %d", len(request.Items), len(want))
}
for i, w := range want {
item := request.Items[i]
if item.Issue.Title != w.title || item.Issue.Priority != w.priority || item.DefaultPriority != w.defaultPriority {
t.Errorf("item %d = {%q, priority %d, DefaultPriority %v}, want {%q, priority %d, DefaultPriority %v}",
i, item.Issue.Title, item.Issue.Priority, item.DefaultPriority, w.title, w.priority, w.defaultPriority)
}
}
}
func stringSlicesEqual(a, b []string) bool {
if len(a) != len(b) {
return false
}
if len(a) == 0 && len(b) == 0 {
return true
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}