* fix(create): let a remote backend mint the --parent child id
`bd create --parent` always called the raw GetNextChildID first, which the
http backend does not serve, so every child create on a remote workspace
refused before writing anything. The Lifecycle role already mints
<parent>.<n> for a create that carries a ParentID and no id of its own, on
every backend and inside the create's own transaction; conformance
RunIssueOperationsCreateUnderAParentMintsTheNextChildID pins it on every leg,
the served one included (and the bd-enterprise postgres leg the MC server
runs). So the create route needs no new capability token: on a registered
Remote backend the client skips the raw reservation and lets the server mint.
The local route keeps its reservation so its Dolt commit staging is
unchanged, and an explicit --id beside --parent is still refused up front.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(mol): molecule reads, auto-close and close --continue over issueops roles
Over the http backend three molecule paths failed, and two of them
silently:
- `bd mol current` / `bd mol progress` without an id answered "no molecules"
because the in_progress listing (a raw SearchIssues shape) and the parent
walk (raw GetDependencyRecordsForIssues) were refused and the refusal was
swallowed; with an id they refused on raw GetMoleculeProgress.
- The molecule auto-close after `bd close` never ran: findParentMolecules
swallowed the same refused edge read into "not part of a molecule".
- `bd close --continue` claimed the next step in a raw RunInTransaction.
The direct route now reads molecules through roleMolStore, which answers
through roles every backend serves: EdgeReader (batched at the wire's 100
anchors) for the parent walk, BatchGetter for the roots (Get per id where a
server does not publish issues.batchGet), Relations for the step rows (full
rows; the raw dependents read over http is getIssue's shallow projection,
which zeroed step assignees) and the progress counts, and Reader.List for the
in_progress/hooked listings. The auto-close closes the root through
Lifecycle.Close, and the --continue claim is one Lifecycle.Update guarded by
ExpectedStatus=open.
No refusal is swallowed any more: findParentMolecules, the molecule finders
and the hooked scan return their errors; `mol current/progress` report them;
the auto-close stays best effort but warns on stderr; AdvanceToNextStep only
moves on to the next ready step when the claim LOST a race (status mismatch,
not claimable, already claimed) and returns anything else. The one heuristic
still skipped is the id-pattern heal in loadDescendants, and only for an
ErrUnsupported backend that cannot express an id-prefix search; other
failures now surface. `mol current`/`mol progress` leave the
types.IssueFilter forbidigo hole, since they no longer build a filter.
gc does not depend on bd's molecule auto-close: it runs its own
(cmd/gc/molecule_autoclose.go, from the bd on_close hook and the
controller's bead-close events). Its worker prompts do run `bd mol current`.
Served e2e (TestE2E_MoleculesOverHTTP): create --parent mints <root>.1/.2,
mol current <id> is non-empty with the full in-progress step, mol progress
counts, close --continue auto-advances, closing the last step auto-closes
the root. `mol current` without an id now refuses out loud on this stack
(asserted: never an empty list); its non-empty assertion is skipped until
S6c (externaldeps reader passthrough) is underneath.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(mol): list molecule candidates with the four wire knobs, drop the S6c skip
On top of S6c the in_progress listing behind 'bd mol current' (no id)
reaches the v0 wire encoder, which refuses ListRequest.IncludeAllTypes.
Ask for templates, gates, infra and ephemeral rows explicitly, which the
wire publishes, and run the 'mol current without an id' served case.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* feat(issueops): one library entry each for molecule auto-close, advance and reads
The molecule rules lived only in cmd/bd, in copies that disagreed: two
auto-close implementations that closed the root through different
library entries outside the step close's transaction, a `close
--continue` claim that set no assignee and could take a step another
agent held on the direct and http routes, and three progress counts.
Every molecule rule now has one implementation in the public issueops
package, composed over three roles every backend serves (BatchGetter,
Relations, EdgeReader): MoleculeRoots, LoadMolecule, AnalyzeMolecule,
ViewMolecule, ReadMoleculeProgress, ActiveMoleculeIDs,
CompletedMolecule. Membership is the parent-child edge.
Auto-close: CloseRequest.AutoCloseMolecule and
CloseBatchRequest.AutoCloseMolecule (opt-in, so gc's own auto-close is
not doubled) close the molecule root a close completed IN THE SAME
TRANSACTION, unforced and guarded on the root revision read there:
storageissueops.CloseCompletedMoleculeInTx on the store backends (the
role bodies bound to the caller's tx, TxMoleculeReader) and
uow.CloseCompletedMoleculeInUOW on the unit of work. It replays on an
idempotent re-close, reports the root on CloseResult/CloseOutcome
.AutoClosedMolecule, and a root its own close policy refuses stays open
and is reported (MoleculeAutoCloseRefusal) without failing the step.
Advance: a new MoleculeStepper role (storage.DoltStorage accessor,
uow.MoleculeStepperSource) whose one implementation,
issueops.NewMoleculeStepper, claims the next ready step through the
Claimer (Lifecycle.Update's Claim for a wisp step): assignee set, a step
another actor holds skipped, never taken.
HTTP: closeIssue/batchCloseIssues carry auto_close_molecule behind the
issues.close.autoCloseMolecule behavior token, and POST
/v0/beads/issues/{id}:advanceMolecule (issues.advanceMolecule) serves
the stepper. Handlers only decode, call the role and encode. The client
sends the member and dials the operation only where advertised; against
an older server it DEGRADES rather than refuses — it runs the same
library rule over its own roles (issueops.CloseCompletedMolecule after
the close, NewMoleculeStepper for the advance), so the outcome is the
same in more round trips. The capability fallbacks that lived in
cmd/bd move into the client: BatchGetter answers one getIssue per id
without issues.batchGet, and EdgeReader splits past the operation's 100
anchors.
Conformance: RunMolecule* on dolt, embedded, uow and http (the http leg
twice: served, and against a server with the tokens struck out).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* refactor(cmd/bd): molecule commands and create --parent through the library entries
`bd close`, `bd close --continue`, `bd mol current`, `bd mol progress`,
`bd ready --mol` and `bd create --parent` now call the one library entry
for each rule on every route (direct, proxied server, remote):
- close (batch, --if-revision, proxied) sets CloseRequest/
CloseBatchRequest.AutoCloseMolecule and only reports what the library
closed in the step close's transaction. autoCloseCompletedMolecule,
autoCloseProxiedCompletedMolecule and closeCompletedMoleculeRoot are
gone, and so is the separate post-close write.
- --continue is the route's MoleculeStepper role: a real claim (the
assignee is set) that skips a step another actor holds. Before, the
direct and http routes flipped the status only and could take it.
AdvanceToNextStep, ClaimStepIfOpen and its two implementations are
gone.
- mol current / mol progress / ready --mol read through
issueops.ViewMolecule, ReadMoleculeProgress, ActiveMoleculeIDs and
LoadMolecule/AnalyzeMolecule over the route's roles. Over http this
fixes the missing step assignee on `ready --mol` (corpus mark
mc-ready-mol). The proxied route's SearchIssues fallback, the role
store, the third progress count and the cmd/bd copy of the readiness
analysis are deleted; only presentation stays here.
- create --parent asks the create role to mint the child id on every
route (the local CLI reservation is gone) and to inherit the parent's
labels (InheritLabelsFromParent) inside the create. The dry-run
preview still shows the labels, and a failed label read there is
reported, not swallowed.
TestProxiedServerMoleculeLifecycleAgreesOnEveryRoute runs one molecule
lifecycle through the real binary on the direct, proxied and remote
(`bd connect` to `bd serve`) routes and requires the same rows and the
same JSON on all three. The served e2e now also pins the held-step skip,
the claimed step's assignee and the full rows of `ready --mol`.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(httpclient): refuse molecule auto-close and advance before dialing an older server
The client used to DEGRADE against a server without
issues.close.autoCloseMolecule / issues.advanceMolecule: it closed the step
without the member and ran a second, client-side copy of the auto-close
(issueops.CloseCompletedMolecule) in its own transactions, and composed the
stepper over its own roles. That kept two executions of each rule alive.
Now the rule runs in ONE place, the server's library: a close or batch close
that sets AutoCloseMolecule, and an Advance, refuse before anything is dialed
with the typed capability error (storage.ErrUnsupported, Capability naming the
token). The composed single-item batch leg now carries the member too.
- issueops.CloseCompletedMolecule (the role-composed form) is gone.
MoleculeRootClose is the one root close request (unforced, the auto-close
reason, the session, guarded on the root revision read), and both
in-transaction bodies (storage issueops CloseCompletedMoleculeInTx, uow
CloseCompletedMoleculeInUOW) build from it.
- MoleculeView.Ready is `bd ready --mol`'s list (every analysis-ready node,
hydrated), so ready --mol and mol current derive from ViewMolecule alone.
Tests: unit pre-dial refusal and member pins (httpclient molecule_test.go),
TestServedMoleculeRefusesAgainstAnOlderServer (step stays open, nothing
claimed) replacing the degraded contract arm.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(cmd/bd): ask for the molecule auto-close only where the route serves it
The refuse-before-dialing client made every `bd close` over http fail
against a server that does not advertise issues.close.autoCloseMolecule
(an older bd serve, or the MC server shape, whose orchestrator owns the
molecule lifecycle): the CLI always set AutoCloseMolecule.
storage.ServesMoleculeAutoClose answers, through every decorator, whether a
store's closes may carry the flag (MoleculeAutoCloseProber, implemented by
httpclient.Store from its handshake; every local backend serves it). The
direct batch close and the --if-revision close set the flag from it; the
proxied route always serves it. Against a silent server the close goes out
without the member and the server's side owns the auto-close.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* test(backend/http): scope the advertised-policy check to reads before the close
The close policy check is the client-side policy's last chance: it runs
before the close request, so only the reads up to that request count. A
read after the close (the old-server molecule notice reads the closed
step's edges) is not the external-dependency policy.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* test(backend/http/corpus): drop the six marks S16 and S19 retire
With the molecule library entries underneath, mol current (with and
without an id, json and text), mol progress and ready --mol read through
issueops.LoadMolecule over the roles, and create --parent lets the server
mint the child id. Those six shapes pass on the reference profile.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(cmd/bd): never silent against a server without the molecule tokens
Against a server that advertises neither issues.close.autoCloseMolecule
nor issues.advanceMolecule (an older bd serve, or one whose orchestrator
owns the molecule lifecycle):
- `bd close --continue` closed the step and only then found the advance
refused, leaving the step closed and the next step unclaimed. It now
asks storage.ServesMoleculeAdvance (a MoleculeAdvanceProber the http
store answers from its handshake; local backends always serve it)
BEFORE the close and refuses up front, saying nothing was closed.
- `bd close` went out without auto_close_molecule and said nothing. When
the closed step belongs to a molecule (issueops.MoleculeRoot), a notice
on stderr now names the molecule and the missing token: its auto-close
is the server's to run. The batch and --if-revision closes both say it.
The served e2e masks both tokens out of the handshake and checks the
up-front refusal (the step stays open) and the notice; the httpclient
unit test pins ServesMoleculeAdvance to the handshake.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* refactor(cmd/bd): mol show through issueops.LoadMolecule on every route
`bd mol show` still loaded the molecule through cmd/bd's
loadTemplateSubgraph (raw GetIssue, GetDependentsWithMetadata and
GetDependencyRecords, plus an id-pattern child probe), while mol current,
mol progress and ready --mol read through the library. It now loads
through issueops.LoadMolecule over the route's molecule roles (the direct
store's, or the proxied provider's), so membership is the parent-child
edge everywhere and the http route reads through roles the server serves.
The served e2e checks mol show's root and step count.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* docs: the molecule auto-close fails the step's close on a non-policy error; stale comments
CloseRequest.AutoCloseMolecule and CloseCompletedMoleculeInTx now say what
changed when the auto-close moved into the close's transaction: a root
close the close policy refuses still leaves the root open and is reported,
but any other failure (a storage error, a lost race on the root's revision)
fails the step's close with it, where the CLI used to close the root
afterwards and only warn.
Stale: the advanceMolecule vocabulary entry said the client composes the
stepper over its own roles when unserved (it refuses before dialing), and
the graph-create collision probe called itself best-effort by transport
(the http GetIssue miss is storage.ErrNotFound like every other store's).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* docs(changelog): the molecule tokens and the S16/S19 behaviours
Entries for issues.close.autoCloseMolecule and issues.advanceMolecule, the
issueops molecule entries, the auto-close inside the step's close (and a
non-policy failure of it failing the close), the real claim behind
close --continue, the molecule reads on every route, the notice and the
up-front --continue refusal against a server without the tokens, and
create --parent / mol reads over http.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(httpclient): regenerate unsupported_gen.go for MoleculeStepper
The PR adds MoleculeStepper() to storage.DoltStorage (200 -> 201
methods). httpclient implements it by hand, but the generated shell's
trailer still counted 200, so
//internal/storage/unsupportedgen:unsupportedgen_test
(TestGeneratedShellIsFresh) failed in Bazel / test and
Bazel / integration, turning CI Gate / Required red.
Regenerated with `go generate ./internal/httpclient`; the only change
is the trailer count (143 of 201 methods generated; 58 left).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* fix(molecule): auto-close refusals, write skew, close --json, docs
Review fixes for #7513, numbered as in each iteration's review
synthesis.
Iteration 1:
F1: a close guard's refusal of the root (TemplateReadOnlyError,
PinnedError, CloseNotAssigneeError) is an auto-close refusal, like the
close policy's: the root stays open and is reported, and the step's
close stands. Pinned by the new conformance case
RunMoleculeGuardedRootKeepsTheStepClose on dolt, embedded, uow and the
served http client.
F2: two closes that finish a molecule's last two steps in concurrent
transactions each read the other's step as open, so neither closed the
root and both committed. Every auto-close now rewrites the root's
coordination cell before it decides the root (local_metadata
molecule-coordination/v1/<shard>, the dependency coordination idiom and
sharding with a FreshRowLock value), in both twins
(CloseCompletedMoleculesInTx and CloseCompletedMoleculesInUOW). Two
deciders of one root now conflict at commit (1213), and the retry reads
the winner's close and closes the root. This is the synthesis's option
(c), applied through a coordination cell instead of the root row; it
needs no post-commit second transaction and no engine lock. The
deterministic two-transaction regression fails without the touch and
passes with it on dolt and uow; the concurrent-close tests end with the
root closed in every round, reported by exactly one close.
F3: the auto-close checks the root's eligibility before walking any
step, MoleculeStepsClosed stops at the first open step, and a batch
decides each root once and reports it on the first item under it.
F4: ErrVersionMismatch on the root is not a refusal; it fails the
step's close with it. The classifier unit test pins this. A conformance
case cannot stage that race deterministically through the public roles.
F5: storage.ServesMoleculeAdvance answers (bool, error), and bd close
--continue refuses before closing anything when the handshake cannot be
read.
F6: the CHANGELOG and the stepper and batch-closer doc comments say
what the code does.
F7: the unserved-auto-close notice fires only when the close left an
auto-closing molecule with every step closed and its root open
(issueops.CompletedMolecule): again on a re-close, once per root in a
batch, never for a step under an ordinary epic or an unfinished
molecule. Proxied mol show, mol current and mol progress again report a
read unit of work that cannot be opened as itself, not as a molecule
that was not found.
F8: the write-ledger comment says the close is refused before dialing.
The dead FirstClosed/LastClosed guards in mol progress pre-exist on main
and are left as they are.
Iteration 2:
F1: on the proxied route, bd ready --mol reported a read unit
of work it could not open twice: once as itself, then as "molecule
'<id>' not found", which under --json is a second error document on
stdout. It now returns moleculeNotResolved, as mol show, mol current and
mol progress do. TestProxiedReadyMolReportsAnUnopenableReadOnce drives
runMoleculeReady with a provider whose NewUOW fails and wants one Error:
line, or one JSON document; both subtests fail with the old return.
F2: the coordination cell serializes every concurrent close of a step
under an open auto-closing root, not only the closes that finish the
molecule. That is the price of deciding the root in the step's
transaction, so it is documented (TouchMoleculeCoordinationInTx, the
UOW twin, CloseRequest.AutoCloseMolecule, the CHANGELOG) and pinned on
dolt and uow: two sibling closes with a third step still open conflict
(both commit when the touch is removed, on either twin), and a six-step
fan-out closes every step through the store's retry, with the root
closed and reported by exactly one close. The synthesis's alternative,
a post-commit recheck by any close that read a step open, is not taken:
it is a second, best-effort transaction after the step's commit, the
shape this PR exists to remove.
F3: MoleculeRootClose's doc no longer promises that a step reopened
between the completion read and the root close fails the step's close.
The guard is the root's RowVersion, which a step reopen never writes;
the doc now says what the guard covers and that this race goes
undetected (it is inherited from the CLI's old post-commit auto-close).
F4: the user docs describe the auto-close. docs/workflows/molecules.md
replaces "Closing the last child does not close the molecule root"
with which roots close and which stay open, the refusal warning, the
unserved-server note and the --json members, and documents the claiming
--continue and parent-child step membership;
docs/reference/json-schema.md gains a bd close --json section. These
are main-only behaviors on pages pinned to v1.2.2 (docs/cli-docs.pin):
a maintainer call against §1 of
engdocs/decisions/2026-07-17-docs-release-pin.md, as in #6508, #5855,
#5997 and #5092. The auto-close itself shipped in v1.2.2
(cmd/bd/close.go autoCloseCompletedMolecule), so the old sentence was
already wrong at the pin. docs/cli-reference/ is generated from the
pinned release and is not hand-edited; the bd close and bd mol show
help texts carry the same facts and reach it at the next pin bump.
F5: bd close --json reports the auto-close. The entry of a close that
completed a molecule gains auto_closed_molecule (the root it closed) or
molecule_auto_close_refusal (why the root stayed open), the HTTP API's
names, on the direct, proxied and --if-revision routes. The members sit
beside the issue's own rather than wrapping the output in an object the
way --suggest-next does: an object would change the output's shape
exactly when a close completes a molecule and break every consumer that
reads an array, while members leave each entry decoding as the closed
issue, compose with `closed` under the flag objects, and leave a plain
close's bytes unchanged. Pinned on the embedded and proxied tiers
(TestEmbeddedCloseJSONReportsMoleculeAutoClose, TestProxiedServerClose2B
json_reports_*, and both --if-revision replay tests), all red without
the change.
F6: cmd/bd's BeadsTemplateLabel is issueops.MoleculeTemplateLabel, so
the template commands and molecule membership cannot drift apart.
F7: the UOW twin's revision probe says that it writes nothing,
and the BatchCloseRequest.auto_close_molecule spec description has its
verb back (apigen regenerated, no drift).
Iteration 3:
F1: on the unit-of-work route (bd serve, the proxied CLI), a refused
root's molecule_auto_close_refusal and warning carried the use case's
and repository's wrapping ("close <id>: db:
IssueSQLRepository.CloseChecked <id>: ...") around the refusal, where
the tx twin and the docs give the refusal alone. Both twins now report
it through RefusedMoleculeAutoClose, which answers the refusing guard's
or policy's own sentence. RunMoleculeRefusedRootKeepsTheStepClose,
RunMoleculeGuardedRootKeepsTheStepClose and the embedded and proxied
close --json tests want that exact sentence; the uow conformance leg
and the proxied test fail without the unwrap.
F2: the docs no longer promise ErrVersionMismatch for a write to the
root that races its auto-close. The root's read and its revision check
share one transaction snapshot, so that write conflicts with the root
close at commit and the retry decides the root again; the close fails
only if it still conflicts when its retry budget runs out
(MoleculeRootClose, IsMoleculeAutoCloseRefusal,
CloseRequest.AutoCloseMolecule, the tx twin's comment, the CHANGELOG
and the OpenAPI description, apigen regenerated).
F3: TestEmbeddedCloseJSONReportsMoleculeAutoClose moves above
TestEmbeddedCloseConcurrent's doc comment, which documents its own test
again.
F4: bd mol progress counting direct steps while the auto-close counts
every descendant predates this PR; it is left for a follow-up.
F5: cmd/bd's molReader drops GetMoleculeProgress, which nothing called
once mol progress read through issueops.ReadMoleculeProgress.
Agent-Signature: claude-code-claude-opus-5-5-max on behalf of Gas City Adopt-PR
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
3410 lines
98 KiB
Go
3410 lines
98 KiB
Go
//go:build cgo
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/steveyegge/beads/internal/formula"
|
|
"github.com/steveyegge/beads/internal/storage"
|
|
"github.com/steveyegge/beads/internal/types"
|
|
"github.com/steveyegge/beads/issueops"
|
|
)
|
|
|
|
func TestParseDistillVar(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
varFlag string
|
|
searchableText string
|
|
wantFind string
|
|
wantVar string
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "spawn-style: variable=value",
|
|
varFlag: "branch=feature-auth",
|
|
searchableText: "Implement feature-auth login flow",
|
|
wantFind: "feature-auth",
|
|
wantVar: "branch",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "substitution-style: value=variable",
|
|
varFlag: "feature-auth=branch",
|
|
searchableText: "Implement feature-auth login flow",
|
|
wantFind: "feature-auth",
|
|
wantVar: "branch",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "spawn-style with version number",
|
|
varFlag: "version=1.2.3",
|
|
searchableText: "Release version 1.2.3 to production",
|
|
wantFind: "1.2.3",
|
|
wantVar: "version",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "both found - prefers spawn-style",
|
|
varFlag: "api=api",
|
|
searchableText: "The api endpoint uses api keys",
|
|
wantFind: "api",
|
|
wantVar: "api",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "neither found - error",
|
|
varFlag: "foo=bar",
|
|
searchableText: "Nothing matches here",
|
|
wantFind: "",
|
|
wantVar: "",
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "empty left side - error",
|
|
varFlag: "=value",
|
|
searchableText: "Some text with value",
|
|
wantFind: "",
|
|
wantVar: "",
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "empty right side - error",
|
|
varFlag: "value=",
|
|
searchableText: "Some text with value",
|
|
wantFind: "",
|
|
wantVar: "",
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "no equals sign - error",
|
|
varFlag: "noequals",
|
|
searchableText: "Some text",
|
|
wantFind: "",
|
|
wantVar: "",
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "value with equals sign",
|
|
varFlag: "env=KEY=VALUE",
|
|
searchableText: "Set KEY=VALUE in config",
|
|
wantFind: "KEY=VALUE",
|
|
wantVar: "env",
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "partial match in longer word - finds it",
|
|
varFlag: "name=auth",
|
|
searchableText: "authentication module",
|
|
wantFind: "auth",
|
|
wantVar: "name",
|
|
wantErr: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
gotFind, gotVar, err := parseDistillVar(tt.varFlag, tt.searchableText)
|
|
|
|
if tt.wantErr {
|
|
if err == nil {
|
|
t.Errorf("parseDistillVar() expected error, got none")
|
|
}
|
|
return
|
|
}
|
|
|
|
if err != nil {
|
|
t.Errorf("parseDistillVar() unexpected error: %v", err)
|
|
return
|
|
}
|
|
|
|
if gotFind != tt.wantFind {
|
|
t.Errorf("parseDistillVar() find = %q, want %q", gotFind, tt.wantFind)
|
|
}
|
|
if gotVar != tt.wantVar {
|
|
t.Errorf("parseDistillVar() var = %q, want %q", gotVar, tt.wantVar)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCollectSubgraphText(t *testing.T) {
|
|
// Create a simple subgraph for testing
|
|
subgraph := &MoleculeSubgraph{
|
|
Issues: []*types.Issue{
|
|
{
|
|
Title: "Epic: Feature Auth",
|
|
Description: "Implement authentication",
|
|
Design: "Use OAuth2",
|
|
},
|
|
{
|
|
Title: "Add login endpoint",
|
|
Notes: "See RFC 6749",
|
|
},
|
|
},
|
|
}
|
|
|
|
text := collectSubgraphText(subgraph)
|
|
|
|
// Verify all fields are included
|
|
expected := []string{
|
|
"Epic: Feature Auth",
|
|
"Implement authentication",
|
|
"Use OAuth2",
|
|
"Add login endpoint",
|
|
"See RFC 6749",
|
|
}
|
|
|
|
for _, exp := range expected {
|
|
if !strings.Contains(text, exp) {
|
|
t.Errorf("collectSubgraphText() missing %q", exp)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestIsProto(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
labels []string
|
|
want bool
|
|
}{
|
|
{"with template label", []string{"template", "other"}, true},
|
|
{"template only", []string{"template"}, true},
|
|
{"no template label", []string{"bug", "feature"}, false},
|
|
{"empty labels", []string{}, false},
|
|
{"nil labels", nil, false},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
issue := &types.Issue{Labels: tt.labels}
|
|
got := isProto(issue)
|
|
if got != tt.want {
|
|
t.Errorf("isProto() = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestOperandType(t *testing.T) {
|
|
if got := operandType(true); got != "proto" {
|
|
t.Errorf("operandType(true) = %q, want %q", got, "proto")
|
|
}
|
|
if got := operandType(false); got != "molecule" {
|
|
t.Errorf("operandType(false) = %q, want %q", got, "molecule")
|
|
}
|
|
}
|
|
|
|
func TestMinPriority(t *testing.T) {
|
|
tests := []struct {
|
|
a, b, want int
|
|
}{
|
|
{1, 2, 1},
|
|
{2, 1, 1},
|
|
{0, 3, 0},
|
|
{3, 3, 3},
|
|
}
|
|
for _, tt := range tests {
|
|
got := minPriority(tt.a, tt.b)
|
|
if got != tt.want {
|
|
t.Errorf("minPriority(%d, %d) = %d, want %d", tt.a, tt.b, got, tt.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestBondProtoProto(t *testing.T) {
|
|
ctx := context.Background()
|
|
dbPath := t.TempDir() + "/test.db"
|
|
store := newTestStore(t, dbPath)
|
|
|
|
// Create two protos
|
|
protoA := &types.Issue{
|
|
Title: "Proto A",
|
|
Status: types.StatusOpen,
|
|
Priority: 1,
|
|
IssueType: types.TypeEpic,
|
|
Labels: []string{MoleculeLabel},
|
|
}
|
|
protoB := &types.Issue{
|
|
Title: "Proto B",
|
|
Status: types.StatusOpen,
|
|
Priority: 2,
|
|
IssueType: types.TypeEpic,
|
|
Labels: []string{MoleculeLabel},
|
|
}
|
|
|
|
if err := store.CreateIssue(ctx, protoA, "test"); err != nil {
|
|
t.Fatalf("Failed to create protoA: %v", err)
|
|
}
|
|
if err := store.CreateIssue(ctx, protoB, "test"); err != nil {
|
|
t.Fatalf("Failed to create protoB: %v", err)
|
|
}
|
|
|
|
// Test sequential bond
|
|
result, err := bondProtoProto(ctx, store, protoA, protoB, types.BondTypeSequential, "", "test")
|
|
if err != nil {
|
|
t.Fatalf("bondProtoProto failed: %v", err)
|
|
}
|
|
|
|
if result.ResultType != "compound_proto" {
|
|
t.Errorf("ResultType = %q, want %q", result.ResultType, "compound_proto")
|
|
}
|
|
if result.BondType != types.BondTypeSequential {
|
|
t.Errorf("BondType = %q, want %q", result.BondType, types.BondTypeSequential)
|
|
}
|
|
|
|
// Verify compound was created
|
|
compound, err := store.GetIssue(ctx, result.ResultID)
|
|
if err != nil {
|
|
t.Fatalf("Failed to get compound: %v", err)
|
|
}
|
|
if !isProto(compound) {
|
|
t.Errorf("Compound should be a proto (have template label), got labels: %v", compound.Labels)
|
|
}
|
|
if compound.Priority != 1 {
|
|
t.Errorf("Compound priority = %d, want %d (min of 1,2)", compound.Priority, 1)
|
|
}
|
|
|
|
// Verify dependencies exist (protoA depends on compound via parent-child)
|
|
deps, err := store.GetDependenciesWithMetadata(ctx, protoA.ID)
|
|
if err != nil {
|
|
t.Fatalf("Failed to get deps for protoA: %v", err)
|
|
}
|
|
foundParentChild := false
|
|
for _, dep := range deps {
|
|
if dep.ID == compound.ID && dep.DependencyType == types.DepParentChild {
|
|
foundParentChild = true
|
|
}
|
|
}
|
|
if !foundParentChild {
|
|
t.Error("Expected parent-child dependency from protoA to compound")
|
|
}
|
|
}
|
|
|
|
func TestBondProtoMol(t *testing.T) {
|
|
ctx := context.Background()
|
|
dbPath := t.TempDir() + "/test.db"
|
|
store := newTestStore(t, dbPath)
|
|
|
|
// Create a proto with a child issue
|
|
proto := &types.Issue{
|
|
Title: "Proto: {{name}}",
|
|
Status: types.StatusOpen,
|
|
Priority: 1,
|
|
IssueType: types.TypeEpic,
|
|
Labels: []string{MoleculeLabel},
|
|
}
|
|
if err := store.CreateIssue(ctx, proto, "test"); err != nil {
|
|
t.Fatalf("Failed to create proto: %v", err)
|
|
}
|
|
|
|
protoChild := &types.Issue{
|
|
Title: "Step 1 for {{name}}",
|
|
Status: types.StatusOpen,
|
|
Priority: 2,
|
|
IssueType: types.TypeTask,
|
|
Labels: []string{MoleculeLabel},
|
|
}
|
|
if err := store.CreateIssue(ctx, protoChild, "test"); err != nil {
|
|
t.Fatalf("Failed to create proto child: %v", err)
|
|
}
|
|
|
|
// Add parent-child dependency
|
|
if err := store.AddDependency(ctx, &types.Dependency{
|
|
IssueID: protoChild.ID,
|
|
DependsOnID: proto.ID,
|
|
Type: types.DepParentChild,
|
|
}, "test"); err != nil {
|
|
t.Fatalf("Failed to add dependency: %v", err)
|
|
}
|
|
|
|
// Create a molecule (existing epic)
|
|
mol := &types.Issue{
|
|
Title: "Existing Work",
|
|
Status: types.StatusInProgress,
|
|
Priority: 1,
|
|
IssueType: types.TypeEpic,
|
|
}
|
|
if err := store.CreateIssue(ctx, mol, "test"); err != nil {
|
|
t.Fatalf("Failed to create molecule: %v", err)
|
|
}
|
|
|
|
// Bond proto to molecule
|
|
vars := map[string]string{"name": "auth-feature"}
|
|
result, err := bondProtoMol(ctx, store, proto, mol, types.BondTypeSequential, vars, "", "test", false, false)
|
|
if err != nil {
|
|
t.Fatalf("bondProtoMol failed: %v", err)
|
|
}
|
|
|
|
if result.ResultType != "compound_molecule" {
|
|
t.Errorf("ResultType = %q, want %q", result.ResultType, "compound_molecule")
|
|
}
|
|
if result.Spawned != 2 {
|
|
t.Errorf("Spawned = %d, want 2", result.Spawned)
|
|
}
|
|
if result.ResultID != mol.ID {
|
|
t.Errorf("ResultID = %q, want %q (original molecule)", result.ResultID, mol.ID)
|
|
}
|
|
}
|
|
|
|
func TestBondMolMol(t *testing.T) {
|
|
ctx := context.Background()
|
|
dbPath := t.TempDir() + "/test.db"
|
|
store := newTestStore(t, dbPath)
|
|
|
|
// Create two molecules
|
|
molA := &types.Issue{
|
|
Title: "Molecule A",
|
|
Status: types.StatusOpen,
|
|
Priority: 1,
|
|
IssueType: types.TypeEpic,
|
|
}
|
|
molB := &types.Issue{
|
|
Title: "Molecule B",
|
|
Status: types.StatusOpen,
|
|
Priority: 2,
|
|
IssueType: types.TypeEpic,
|
|
}
|
|
|
|
if err := store.CreateIssue(ctx, molA, "test"); err != nil {
|
|
t.Fatalf("Failed to create molA: %v", err)
|
|
}
|
|
if err := store.CreateIssue(ctx, molB, "test"); err != nil {
|
|
t.Fatalf("Failed to create molB: %v", err)
|
|
}
|
|
|
|
// Test sequential bond
|
|
result, err := bondMolMol(ctx, store, molA, molB, types.BondTypeSequential, "test")
|
|
if err != nil {
|
|
t.Fatalf("bondMolMol failed: %v", err)
|
|
}
|
|
|
|
if result.ResultType != "compound_molecule" {
|
|
t.Errorf("ResultType = %q, want %q", result.ResultType, "compound_molecule")
|
|
}
|
|
if result.ResultID != molA.ID {
|
|
t.Errorf("ResultID = %q, want %q", result.ResultID, molA.ID)
|
|
}
|
|
|
|
// Verify dependency: B blocks on A
|
|
deps, err := store.GetDependenciesWithMetadata(ctx, molB.ID)
|
|
if err != nil {
|
|
t.Fatalf("Failed to get deps for molB: %v", err)
|
|
}
|
|
foundBlocks := false
|
|
for _, dep := range deps {
|
|
if dep.ID == molA.ID && dep.DependencyType == types.DepBlocks {
|
|
foundBlocks = true
|
|
}
|
|
}
|
|
if !foundBlocks {
|
|
t.Error("Expected blocks dependency from molB to molA for sequential bond")
|
|
}
|
|
|
|
// Test parallel bond (create new molecules)
|
|
molC := &types.Issue{
|
|
Title: "Molecule C",
|
|
Status: types.StatusOpen,
|
|
IssueType: types.TypeEpic,
|
|
}
|
|
molD := &types.Issue{
|
|
Title: "Molecule D",
|
|
Status: types.StatusOpen,
|
|
IssueType: types.TypeEpic,
|
|
}
|
|
if err := store.CreateIssue(ctx, molC, "test"); err != nil {
|
|
t.Fatalf("Failed to create molC: %v", err)
|
|
}
|
|
if err := store.CreateIssue(ctx, molD, "test"); err != nil {
|
|
t.Fatalf("Failed to create molD: %v", err)
|
|
}
|
|
|
|
result2, err := bondMolMol(ctx, store, molC, molD, types.BondTypeParallel, "test")
|
|
if err != nil {
|
|
t.Fatalf("bondMolMol parallel failed: %v", err)
|
|
}
|
|
|
|
// Verify parent-child dependency for parallel
|
|
deps2, err := store.GetDependenciesWithMetadata(ctx, molD.ID)
|
|
if err != nil {
|
|
t.Fatalf("Failed to get deps for molD: %v", err)
|
|
}
|
|
foundParentChild := false
|
|
for _, dep := range deps2 {
|
|
if dep.ID == molC.ID && dep.DependencyType == types.DepParentChild {
|
|
foundParentChild = true
|
|
}
|
|
}
|
|
if !foundParentChild {
|
|
t.Errorf("Expected parent-child dependency for parallel bond, result: %+v", result2)
|
|
}
|
|
}
|
|
|
|
func TestSquashMolecule(t *testing.T) {
|
|
ctx := context.Background()
|
|
dbPath := t.TempDir() + "/test.db"
|
|
s := newTestStore(t, dbPath)
|
|
|
|
// Create a molecule (root issue)
|
|
root := &types.Issue{
|
|
Title: "Test Molecule",
|
|
Status: types.StatusOpen,
|
|
Priority: 1,
|
|
IssueType: types.TypeEpic,
|
|
}
|
|
if err := s.CreateIssue(ctx, root, "test"); err != nil {
|
|
t.Fatalf("Failed to create root: %v", err)
|
|
}
|
|
|
|
// Create ephemeral children
|
|
child1 := &types.Issue{
|
|
Title: "Step 1: Design",
|
|
Description: "Design the architecture",
|
|
Status: types.StatusClosed,
|
|
Priority: 2,
|
|
IssueType: types.TypeTask,
|
|
Ephemeral: true,
|
|
CloseReason: "Completed design",
|
|
}
|
|
child2 := &types.Issue{
|
|
Title: "Step 2: Implement",
|
|
Description: "Build the feature",
|
|
Status: types.StatusClosed,
|
|
Priority: 2,
|
|
IssueType: types.TypeTask,
|
|
Ephemeral: true,
|
|
CloseReason: "Code merged",
|
|
}
|
|
|
|
if err := s.CreateIssue(ctx, child1, "test"); err != nil {
|
|
t.Fatalf("Failed to create child1: %v", err)
|
|
}
|
|
if err := s.CreateIssue(ctx, child2, "test"); err != nil {
|
|
t.Fatalf("Failed to create child2: %v", err)
|
|
}
|
|
|
|
// Add parent-child dependencies
|
|
if err := s.AddDependency(ctx, &types.Dependency{
|
|
IssueID: child1.ID,
|
|
DependsOnID: root.ID,
|
|
Type: types.DepParentChild,
|
|
}, "test"); err != nil {
|
|
t.Fatalf("Failed to add child1 dependency: %v", err)
|
|
}
|
|
if err := s.AddDependency(ctx, &types.Dependency{
|
|
IssueID: child2.ID,
|
|
DependsOnID: root.ID,
|
|
Type: types.DepParentChild,
|
|
}, "test"); err != nil {
|
|
t.Fatalf("Failed to add child2 dependency: %v", err)
|
|
}
|
|
|
|
// Test squash with keep-children
|
|
children := []*types.Issue{child1, child2}
|
|
result, err := squashMolecule(ctx, s, root, children, true, "", "test")
|
|
if err != nil {
|
|
t.Fatalf("squashMolecule failed: %v", err)
|
|
}
|
|
|
|
if result.SquashedCount != 2 {
|
|
t.Errorf("SquashedCount = %d, want 2", result.SquashedCount)
|
|
}
|
|
if result.DeletedCount != 0 {
|
|
t.Errorf("DeletedCount = %d, want 0 (keep-children)", result.DeletedCount)
|
|
}
|
|
if !result.KeptChildren {
|
|
t.Error("KeptChildren should be true")
|
|
}
|
|
|
|
// Verify digest was created
|
|
digest, err := s.GetIssue(ctx, result.DigestID)
|
|
if err != nil {
|
|
t.Fatalf("Failed to get digest: %v", err)
|
|
}
|
|
if digest.Ephemeral {
|
|
t.Error("Digest should NOT be ephemeral")
|
|
}
|
|
if digest.Status != types.StatusClosed {
|
|
t.Errorf("Digest status = %v, want closed", digest.Status)
|
|
}
|
|
if !strings.Contains(digest.Description, "Step 1: Design") {
|
|
t.Error("Digest should contain child titles")
|
|
}
|
|
if !strings.Contains(digest.Description, "Completed design") {
|
|
t.Error("Digest should contain close reasons")
|
|
}
|
|
|
|
// Children should still exist
|
|
c1, err := s.GetIssue(ctx, child1.ID)
|
|
if err != nil || c1 == nil {
|
|
t.Error("Child1 should still exist with keep-children")
|
|
}
|
|
}
|
|
|
|
func TestSquashMoleculeWithDelete(t *testing.T) {
|
|
ctx := context.Background()
|
|
dbPath := t.TempDir() + "/test.db"
|
|
s := newTestStore(t, dbPath)
|
|
|
|
// Create a molecule with ephemeral children
|
|
root := &types.Issue{
|
|
Title: "Delete Test Molecule",
|
|
Status: types.StatusOpen,
|
|
Priority: 1,
|
|
IssueType: types.TypeEpic,
|
|
}
|
|
if err := s.CreateIssue(ctx, root, "test"); err != nil {
|
|
t.Fatalf("Failed to create root: %v", err)
|
|
}
|
|
|
|
child := &types.Issue{
|
|
Title: "Wisp Step",
|
|
Status: types.StatusClosed,
|
|
Priority: 2,
|
|
IssueType: types.TypeTask,
|
|
Ephemeral: true,
|
|
}
|
|
if err := s.CreateIssue(ctx, child, "test"); err != nil {
|
|
t.Fatalf("Failed to create child: %v", err)
|
|
}
|
|
if err := s.AddDependency(ctx, &types.Dependency{
|
|
IssueID: child.ID,
|
|
DependsOnID: root.ID,
|
|
Type: types.DepParentChild,
|
|
}, "test"); err != nil {
|
|
t.Fatalf("Failed to add dependency: %v", err)
|
|
}
|
|
|
|
// Squash with delete (keepChildren=false)
|
|
result, err := squashMolecule(ctx, s, root, []*types.Issue{child}, false, "", "test")
|
|
if err != nil {
|
|
t.Fatalf("squashMolecule failed: %v", err)
|
|
}
|
|
|
|
if result.DeletedCount != 1 {
|
|
t.Errorf("DeletedCount = %d, want 1", result.DeletedCount)
|
|
}
|
|
|
|
// Child should be deleted
|
|
c, err := s.GetIssue(ctx, child.ID)
|
|
if err == nil && c != nil {
|
|
t.Error("Child should have been deleted")
|
|
}
|
|
|
|
// Digest should exist
|
|
digest, err := s.GetIssue(ctx, result.DigestID)
|
|
if err != nil || digest == nil {
|
|
t.Error("Digest should exist after squash")
|
|
}
|
|
}
|
|
|
|
func TestGenerateDigest(t *testing.T) {
|
|
root := &types.Issue{
|
|
Title: "Test Molecule",
|
|
}
|
|
children := []*types.Issue{
|
|
{
|
|
Title: "Step 1",
|
|
Description: "First step description",
|
|
Status: types.StatusClosed,
|
|
CloseReason: "Done",
|
|
},
|
|
{
|
|
Title: "Step 2",
|
|
Description: "Second step description that is longer",
|
|
Status: types.StatusInProgress,
|
|
},
|
|
}
|
|
|
|
digest := generateDigest(root, children)
|
|
|
|
// Verify structure
|
|
if !strings.Contains(digest, "## Molecule Execution Summary") {
|
|
t.Error("Digest should have summary header")
|
|
}
|
|
if !strings.Contains(digest, "Test Molecule") {
|
|
t.Error("Digest should contain molecule title")
|
|
}
|
|
if !strings.Contains(digest, "**Steps**: 2") {
|
|
t.Error("Digest should show step count")
|
|
}
|
|
if !strings.Contains(digest, "**Completed**: 1/2") {
|
|
t.Error("Digest should show completion stats")
|
|
}
|
|
if !strings.Contains(digest, "**In Progress**: 1") {
|
|
t.Error("Digest should show in-progress count")
|
|
}
|
|
if !strings.Contains(digest, "Step 1") {
|
|
t.Error("Digest should list step titles")
|
|
}
|
|
if !strings.Contains(digest, "*Outcome: Done*") {
|
|
t.Error("Digest should include close reasons")
|
|
}
|
|
}
|
|
|
|
// TestSquashMoleculeWithAgentSummary verifies that agent-provided summaries are used
|
|
func TestSquashMoleculeWithAgentSummary(t *testing.T) {
|
|
ctx := context.Background()
|
|
dbPath := t.TempDir() + "/test.db"
|
|
s := newTestStore(t, dbPath)
|
|
|
|
// Create a molecule with ephemeral child
|
|
root := &types.Issue{
|
|
Title: "Agent Summary Test",
|
|
Status: types.StatusOpen,
|
|
Priority: 1,
|
|
IssueType: types.TypeEpic,
|
|
}
|
|
if err := s.CreateIssue(ctx, root, "test"); err != nil {
|
|
t.Fatalf("Failed to create root: %v", err)
|
|
}
|
|
|
|
child := &types.Issue{
|
|
Title: "Wisp Step",
|
|
Description: "This should NOT appear in digest",
|
|
Status: types.StatusClosed,
|
|
Priority: 2,
|
|
IssueType: types.TypeTask,
|
|
Ephemeral: true,
|
|
CloseReason: "Done",
|
|
}
|
|
if err := s.CreateIssue(ctx, child, "test"); err != nil {
|
|
t.Fatalf("Failed to create child: %v", err)
|
|
}
|
|
if err := s.AddDependency(ctx, &types.Dependency{
|
|
IssueID: child.ID,
|
|
DependsOnID: root.ID,
|
|
Type: types.DepParentChild,
|
|
}, "test"); err != nil {
|
|
t.Fatalf("Failed to add dependency: %v", err)
|
|
}
|
|
|
|
// Squash with agent-provided summary
|
|
agentSummary := "## AI-Generated Summary\n\nThe agent completed the task successfully."
|
|
result, err := squashMolecule(ctx, s, root, []*types.Issue{child}, true, agentSummary, "test")
|
|
if err != nil {
|
|
t.Fatalf("squashMolecule failed: %v", err)
|
|
}
|
|
|
|
// Verify digest uses agent summary, not auto-generated content
|
|
digest, err := s.GetIssue(ctx, result.DigestID)
|
|
if err != nil {
|
|
t.Fatalf("Failed to get digest: %v", err)
|
|
}
|
|
|
|
if digest.Description != agentSummary {
|
|
t.Errorf("Digest should use agent summary.\nGot: %s\nWant: %s", digest.Description, agentSummary)
|
|
}
|
|
|
|
// Verify auto-generated content is NOT present
|
|
if strings.Contains(digest.Description, "Wisp Step") {
|
|
t.Error("Digest should NOT contain auto-generated content when agent summary provided")
|
|
}
|
|
}
|
|
|
|
// TestSquashWispMoleculeClearsRootEphemeral verifies that squashing a wisp
|
|
// molecule (root with Ephemeral=true) clears the root's Ephemeral flag in
|
|
// addition to auto-closing it. Without this, auto-export keeps re-emitting
|
|
// the closed root as ephemeral=true on every export cycle, accumulating
|
|
// duplicate JSONL rows.
|
|
func TestSquashWispMoleculeClearsRootEphemeral(t *testing.T) {
|
|
ctx := context.Background()
|
|
dbPath := t.TempDir() + "/test.db"
|
|
s := newTestStore(t, dbPath)
|
|
|
|
// Wisp molecule: root is ephemeral too (created via `bd mol wisp`).
|
|
root := &types.Issue{
|
|
Title: "Wisp Molecule Root",
|
|
Status: types.StatusOpen,
|
|
Priority: 1,
|
|
IssueType: types.TypeEpic,
|
|
Ephemeral: true,
|
|
}
|
|
if err := s.CreateIssue(ctx, root, "test"); err != nil {
|
|
t.Fatalf("Failed to create ephemeral root: %v", err)
|
|
}
|
|
|
|
child := &types.Issue{
|
|
Title: "Wisp Step",
|
|
Status: types.StatusClosed,
|
|
Priority: 2,
|
|
IssueType: types.TypeTask,
|
|
Ephemeral: true,
|
|
}
|
|
if err := s.CreateIssue(ctx, child, "test"); err != nil {
|
|
t.Fatalf("Failed to create child: %v", err)
|
|
}
|
|
if err := s.AddDependency(ctx, &types.Dependency{
|
|
IssueID: child.ID,
|
|
DependsOnID: root.ID,
|
|
Type: types.DepParentChild,
|
|
}, "test"); err != nil {
|
|
t.Fatalf("Failed to add dependency: %v", err)
|
|
}
|
|
|
|
result, err := squashMolecule(ctx, s, root, []*types.Issue{child}, false, "", "test")
|
|
if err != nil {
|
|
t.Fatalf("squashMolecule failed: %v", err)
|
|
}
|
|
if !result.WispSquash {
|
|
t.Error("expected WispSquash=true for ephemeral root")
|
|
}
|
|
|
|
closed, err := s.GetIssue(ctx, root.ID)
|
|
if err != nil {
|
|
t.Fatalf("Failed to get root after squash: %v", err)
|
|
}
|
|
if closed.Status != types.StatusClosed {
|
|
t.Errorf("root status = %v, want closed", closed.Status)
|
|
}
|
|
if closed.Ephemeral {
|
|
t.Errorf("root Ephemeral should be false after squash, got true (issue: closed wisp roots leak as duplicate JSONL rows on every export)")
|
|
}
|
|
}
|
|
|
|
// =============================================================================
|
|
// Spawn --attach Tests (bd-f7p1)
|
|
// =============================================================================
|
|
|
|
// TestSpawnWithBasicAttach tests spawning a proto with one --attach flag
|
|
func TestSpawnWithBasicAttach(t *testing.T) {
|
|
ctx := context.Background()
|
|
dbPath := t.TempDir() + "/test.db"
|
|
s := newTestStore(t, dbPath)
|
|
|
|
// Create primary proto with a child
|
|
primaryProto := &types.Issue{
|
|
Title: "Primary: {{feature}}",
|
|
Status: types.StatusOpen,
|
|
Priority: 1,
|
|
IssueType: types.TypeEpic,
|
|
Labels: []string{MoleculeLabel},
|
|
}
|
|
if err := s.CreateIssue(ctx, primaryProto, "test"); err != nil {
|
|
t.Fatalf("Failed to create primary proto: %v", err)
|
|
}
|
|
|
|
primaryChild := &types.Issue{
|
|
Title: "Step 1 for {{feature}}",
|
|
Status: types.StatusOpen,
|
|
Priority: 2,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
if err := s.CreateIssue(ctx, primaryChild, "test"); err != nil {
|
|
t.Fatalf("Failed to create primary child: %v", err)
|
|
}
|
|
if err := s.AddDependency(ctx, &types.Dependency{
|
|
IssueID: primaryChild.ID,
|
|
DependsOnID: primaryProto.ID,
|
|
Type: types.DepParentChild,
|
|
}, "test"); err != nil {
|
|
t.Fatalf("Failed to add primary child dependency: %v", err)
|
|
}
|
|
|
|
// Create attachment proto with a child
|
|
attachProto := &types.Issue{
|
|
Title: "Attachment: {{feature}} docs",
|
|
Status: types.StatusOpen,
|
|
Priority: 2,
|
|
IssueType: types.TypeEpic,
|
|
Labels: []string{MoleculeLabel},
|
|
}
|
|
if err := s.CreateIssue(ctx, attachProto, "test"); err != nil {
|
|
t.Fatalf("Failed to create attach proto: %v", err)
|
|
}
|
|
|
|
attachChild := &types.Issue{
|
|
Title: "Write docs for {{feature}}",
|
|
Status: types.StatusOpen,
|
|
Priority: 3,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
if err := s.CreateIssue(ctx, attachChild, "test"); err != nil {
|
|
t.Fatalf("Failed to create attach child: %v", err)
|
|
}
|
|
if err := s.AddDependency(ctx, &types.Dependency{
|
|
IssueID: attachChild.ID,
|
|
DependsOnID: attachProto.ID,
|
|
Type: types.DepParentChild,
|
|
}, "test"); err != nil {
|
|
t.Fatalf("Failed to add attach child dependency: %v", err)
|
|
}
|
|
|
|
// Spawn primary proto
|
|
primarySubgraph, err := loadTemplateSubgraph(ctx, s, primaryProto.ID)
|
|
if err != nil {
|
|
t.Fatalf("Failed to load primary subgraph: %v", err)
|
|
}
|
|
|
|
vars := map[string]string{"feature": "auth"}
|
|
spawnResult, err := spawnMolecule(ctx, s, primarySubgraph, vars, "", "test", true, "")
|
|
if err != nil {
|
|
t.Fatalf("Failed to spawn primary: %v", err)
|
|
}
|
|
|
|
if spawnResult.Created != 2 {
|
|
t.Errorf("Spawn created = %d, want 2", spawnResult.Created)
|
|
}
|
|
|
|
// Get the spawned molecule
|
|
spawnedMol, err := s.GetIssue(ctx, spawnResult.NewEpicID)
|
|
if err != nil {
|
|
t.Fatalf("Failed to get spawned molecule: %v", err)
|
|
}
|
|
|
|
// Attach the second proto (simulating --attach flag behavior)
|
|
bondResult, err := bondProtoMol(ctx, s, attachProto, spawnedMol, types.BondTypeSequential, vars, "", "test", false, false)
|
|
if err != nil {
|
|
t.Fatalf("Failed to bond attachment: %v", err)
|
|
}
|
|
|
|
if bondResult.Spawned != 2 {
|
|
t.Errorf("Bond spawned = %d, want 2", bondResult.Spawned)
|
|
}
|
|
if bondResult.ResultType != "compound_molecule" {
|
|
t.Errorf("ResultType = %q, want %q", bondResult.ResultType, "compound_molecule")
|
|
}
|
|
|
|
// Verify the spawned attachment root has dependency on the primary molecule
|
|
attachedRootID := bondResult.IDMapping[attachProto.ID]
|
|
deps, err := s.GetDependenciesWithMetadata(ctx, attachedRootID)
|
|
if err != nil {
|
|
t.Fatalf("Failed to get deps: %v", err)
|
|
}
|
|
|
|
foundBlocks := false
|
|
for _, dep := range deps {
|
|
if dep.ID == spawnedMol.ID && dep.DependencyType == types.DepBlocks {
|
|
foundBlocks = true
|
|
}
|
|
}
|
|
if !foundBlocks {
|
|
t.Error("Expected blocks dependency from attached proto to spawned molecule for sequential bond")
|
|
}
|
|
|
|
// Verify variable substitution worked in attached issues
|
|
attachedRoot, err := s.GetIssue(ctx, attachedRootID)
|
|
if err != nil {
|
|
t.Fatalf("Failed to get attached root: %v", err)
|
|
}
|
|
if !strings.Contains(attachedRoot.Title, "auth") {
|
|
t.Errorf("Attached root title %q should contain 'auth' from variable substitution", attachedRoot.Title)
|
|
}
|
|
}
|
|
|
|
// TestSpawnWithMultipleAttachments tests spawning with --attach A --attach B
|
|
func TestSpawnWithMultipleAttachments(t *testing.T) {
|
|
ctx := context.Background()
|
|
dbPath := t.TempDir() + "/test.db"
|
|
s := newTestStore(t, dbPath)
|
|
|
|
// Create primary proto
|
|
primaryProto := &types.Issue{
|
|
Title: "Primary Feature",
|
|
Status: types.StatusOpen,
|
|
Priority: 1,
|
|
IssueType: types.TypeEpic,
|
|
Labels: []string{MoleculeLabel},
|
|
}
|
|
if err := s.CreateIssue(ctx, primaryProto, "test"); err != nil {
|
|
t.Fatalf("Failed to create primary proto: %v", err)
|
|
}
|
|
|
|
// Create first attachment proto
|
|
attachA := &types.Issue{
|
|
Title: "Attachment A: Testing",
|
|
Status: types.StatusOpen,
|
|
Priority: 2,
|
|
IssueType: types.TypeEpic,
|
|
Labels: []string{MoleculeLabel},
|
|
}
|
|
if err := s.CreateIssue(ctx, attachA, "test"); err != nil {
|
|
t.Fatalf("Failed to create attachA: %v", err)
|
|
}
|
|
|
|
// Create second attachment proto
|
|
attachB := &types.Issue{
|
|
Title: "Attachment B: Documentation",
|
|
Status: types.StatusOpen,
|
|
Priority: 3,
|
|
IssueType: types.TypeEpic,
|
|
Labels: []string{MoleculeLabel},
|
|
}
|
|
if err := s.CreateIssue(ctx, attachB, "test"); err != nil {
|
|
t.Fatalf("Failed to create attachB: %v", err)
|
|
}
|
|
|
|
// Spawn primary
|
|
primarySubgraph, err := loadTemplateSubgraph(ctx, s, primaryProto.ID)
|
|
if err != nil {
|
|
t.Fatalf("Failed to load primary subgraph: %v", err)
|
|
}
|
|
|
|
spawnResult, err := spawnMolecule(ctx, s, primarySubgraph, nil, "", "test", true, "")
|
|
if err != nil {
|
|
t.Fatalf("Failed to spawn primary: %v", err)
|
|
}
|
|
|
|
spawnedMol, err := s.GetIssue(ctx, spawnResult.NewEpicID)
|
|
if err != nil {
|
|
t.Fatalf("Failed to get spawned molecule: %v", err)
|
|
}
|
|
|
|
// Attach both protos (simulating --attach A --attach B)
|
|
bondResultA, err := bondProtoMol(ctx, s, attachA, spawnedMol, types.BondTypeSequential, nil, "", "test", false, false)
|
|
if err != nil {
|
|
t.Fatalf("Failed to bond attachA: %v", err)
|
|
}
|
|
|
|
bondResultB, err := bondProtoMol(ctx, s, attachB, spawnedMol, types.BondTypeSequential, nil, "", "test", false, false)
|
|
if err != nil {
|
|
t.Fatalf("Failed to bond attachB: %v", err)
|
|
}
|
|
|
|
// Both should have spawned their protos
|
|
if bondResultA.Spawned != 1 {
|
|
t.Errorf("bondResultA.Spawned = %d, want 1", bondResultA.Spawned)
|
|
}
|
|
if bondResultB.Spawned != 1 {
|
|
t.Errorf("bondResultB.Spawned = %d, want 1", bondResultB.Spawned)
|
|
}
|
|
|
|
// Both should depend on the primary molecule
|
|
attachedAID := bondResultA.IDMapping[attachA.ID]
|
|
attachedBID := bondResultB.IDMapping[attachB.ID]
|
|
|
|
depsA, err := s.GetDependenciesWithMetadata(ctx, attachedAID)
|
|
if err != nil {
|
|
t.Fatalf("Failed to get deps for A: %v", err)
|
|
}
|
|
depsB, err := s.GetDependenciesWithMetadata(ctx, attachedBID)
|
|
if err != nil {
|
|
t.Fatalf("Failed to get deps for B: %v", err)
|
|
}
|
|
|
|
foundABlocks := false
|
|
for _, dep := range depsA {
|
|
if dep.ID == spawnedMol.ID && dep.DependencyType == types.DepBlocks {
|
|
foundABlocks = true
|
|
}
|
|
}
|
|
foundBBlocks := false
|
|
for _, dep := range depsB {
|
|
if dep.ID == spawnedMol.ID && dep.DependencyType == types.DepBlocks {
|
|
foundBBlocks = true
|
|
}
|
|
}
|
|
|
|
if !foundABlocks {
|
|
t.Error("Expected A to block on spawned molecule")
|
|
}
|
|
if !foundBBlocks {
|
|
t.Error("Expected B to block on spawned molecule")
|
|
}
|
|
}
|
|
|
|
// TestSpawnAttachTypes verifies sequential vs parallel bonding behavior
|
|
func TestSpawnAttachTypes(t *testing.T) {
|
|
ctx := context.Background()
|
|
dbPath := t.TempDir() + "/test.db"
|
|
s := newTestStore(t, dbPath)
|
|
|
|
// Create primary proto
|
|
primaryProto := &types.Issue{
|
|
Title: "Primary",
|
|
Status: types.StatusOpen,
|
|
Priority: 1,
|
|
IssueType: types.TypeEpic,
|
|
Labels: []string{MoleculeLabel},
|
|
}
|
|
if err := s.CreateIssue(ctx, primaryProto, "test"); err != nil {
|
|
t.Fatalf("Failed to create primary: %v", err)
|
|
}
|
|
|
|
// Create attachment proto
|
|
attachProto := &types.Issue{
|
|
Title: "Attachment",
|
|
Status: types.StatusOpen,
|
|
Priority: 2,
|
|
IssueType: types.TypeEpic,
|
|
Labels: []string{MoleculeLabel},
|
|
}
|
|
if err := s.CreateIssue(ctx, attachProto, "test"); err != nil {
|
|
t.Fatalf("Failed to create attachment: %v", err)
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
bondType string
|
|
expectType types.DependencyType
|
|
}{
|
|
{"sequential uses blocks", types.BondTypeSequential, types.DepBlocks},
|
|
{"parallel uses parent-child", types.BondTypeParallel, types.DepParentChild},
|
|
{"conditional uses conditional-blocks", types.BondTypeConditional, types.DepConditionalBlocks},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
// Spawn fresh primary for each test
|
|
primarySubgraph, err := loadTemplateSubgraph(ctx, s, primaryProto.ID)
|
|
if err != nil {
|
|
t.Fatalf("Failed to load primary subgraph: %v", err)
|
|
}
|
|
|
|
spawnResult, err := spawnMolecule(ctx, s, primarySubgraph, nil, "", "test", true, "")
|
|
if err != nil {
|
|
t.Fatalf("Failed to spawn primary: %v", err)
|
|
}
|
|
|
|
spawnedMol, err := s.GetIssue(ctx, spawnResult.NewEpicID)
|
|
if err != nil {
|
|
t.Fatalf("Failed to get spawned molecule: %v", err)
|
|
}
|
|
|
|
// Bond with specified type
|
|
bondResult, err := bondProtoMol(ctx, s, attachProto, spawnedMol, tt.bondType, nil, "", "test", false, false)
|
|
if err != nil {
|
|
t.Fatalf("Failed to bond: %v", err)
|
|
}
|
|
|
|
// Check dependency type
|
|
attachedID := bondResult.IDMapping[attachProto.ID]
|
|
deps, err := s.GetDependenciesWithMetadata(ctx, attachedID)
|
|
if err != nil {
|
|
t.Fatalf("Failed to get deps: %v", err)
|
|
}
|
|
|
|
foundExpected := false
|
|
for _, dep := range deps {
|
|
if dep.ID == spawnedMol.ID && dep.DependencyType == tt.expectType {
|
|
foundExpected = true
|
|
}
|
|
}
|
|
|
|
if !foundExpected {
|
|
t.Errorf("Expected %s dependency from attached to spawned molecule", tt.expectType)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestSpawnAttachNonProtoError tests that attaching a non-proto fails validation
|
|
func TestSpawnAttachNonProtoError(t *testing.T) {
|
|
// The isProto function is tested separately in TestIsProto
|
|
// This test verifies the validation logic that would be used in runMolSpawn
|
|
|
|
// Create a non-proto issue (no template label)
|
|
issue := &types.Issue{
|
|
Title: "Not a proto",
|
|
Status: types.StatusOpen,
|
|
Labels: []string{"bug"}, // Not MoleculeLabel
|
|
}
|
|
|
|
if isProto(issue) {
|
|
t.Error("isProto should return false for issue without template label")
|
|
}
|
|
|
|
// Issue with template label should pass
|
|
protoIssue := &types.Issue{
|
|
Title: "A proto",
|
|
Status: types.StatusOpen,
|
|
Labels: []string{MoleculeLabel},
|
|
}
|
|
|
|
if !isProto(protoIssue) {
|
|
t.Error("isProto should return true for issue with template label")
|
|
}
|
|
}
|
|
|
|
// TestSpawnVariableAggregation tests that variables from primary + attachments are combined
|
|
func TestSpawnVariableAggregation(t *testing.T) {
|
|
ctx := context.Background()
|
|
dbPath := t.TempDir() + "/test.db"
|
|
s := newTestStore(t, dbPath)
|
|
|
|
// Create primary proto with one variable
|
|
primaryProto := &types.Issue{
|
|
Title: "Feature: {{feature_name}}",
|
|
Description: "Implement the {{feature_name}} feature",
|
|
Status: types.StatusOpen,
|
|
Priority: 1,
|
|
IssueType: types.TypeEpic,
|
|
Labels: []string{MoleculeLabel},
|
|
}
|
|
if err := s.CreateIssue(ctx, primaryProto, "test"); err != nil {
|
|
t.Fatalf("Failed to create primary: %v", err)
|
|
}
|
|
|
|
// Create attachment proto with a different variable
|
|
attachProto := &types.Issue{
|
|
Title: "Docs for {{doc_version}}",
|
|
Description: "Document version {{doc_version}}",
|
|
Status: types.StatusOpen,
|
|
Priority: 2,
|
|
IssueType: types.TypeEpic,
|
|
Labels: []string{MoleculeLabel},
|
|
}
|
|
if err := s.CreateIssue(ctx, attachProto, "test"); err != nil {
|
|
t.Fatalf("Failed to create attachment: %v", err)
|
|
}
|
|
|
|
// Load subgraphs and extract variables
|
|
primarySubgraph, err := loadTemplateSubgraph(ctx, s, primaryProto.ID)
|
|
if err != nil {
|
|
t.Fatalf("Failed to load primary subgraph: %v", err)
|
|
}
|
|
attachSubgraph, err := loadTemplateSubgraph(ctx, s, attachProto.ID)
|
|
if err != nil {
|
|
t.Fatalf("Failed to load attach subgraph: %v", err)
|
|
}
|
|
|
|
// Aggregate variables (simulating runMolSpawn logic)
|
|
requiredVars := extractAllVariables(primarySubgraph)
|
|
attachVars := extractAllVariables(attachSubgraph)
|
|
for _, v := range attachVars {
|
|
found := false
|
|
for _, rv := range requiredVars {
|
|
if rv == v {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
requiredVars = append(requiredVars, v)
|
|
}
|
|
}
|
|
|
|
// Should have both variables
|
|
if len(requiredVars) != 2 {
|
|
t.Errorf("Expected 2 required vars, got %d: %v", len(requiredVars), requiredVars)
|
|
}
|
|
|
|
hasFeatureName := false
|
|
hasDocVersion := false
|
|
for _, v := range requiredVars {
|
|
if v == "feature_name" {
|
|
hasFeatureName = true
|
|
}
|
|
if v == "doc_version" {
|
|
hasDocVersion = true
|
|
}
|
|
}
|
|
|
|
if !hasFeatureName {
|
|
t.Error("Missing feature_name variable from primary proto")
|
|
}
|
|
if !hasDocVersion {
|
|
t.Error("Missing doc_version variable from attachment proto")
|
|
}
|
|
|
|
// Provide both variables and verify substitution
|
|
vars := map[string]string{
|
|
"feature_name": "authentication",
|
|
"doc_version": "2.0",
|
|
}
|
|
|
|
// Spawn primary with variables
|
|
spawnResult, err := spawnMolecule(ctx, s, primarySubgraph, vars, "", "test", true, "")
|
|
if err != nil {
|
|
t.Fatalf("Failed to spawn primary: %v", err)
|
|
}
|
|
|
|
// Verify primary variable was substituted
|
|
spawnedPrimary, err := s.GetIssue(ctx, spawnResult.NewEpicID)
|
|
if err != nil {
|
|
t.Fatalf("Failed to get spawned primary: %v", err)
|
|
}
|
|
if !strings.Contains(spawnedPrimary.Title, "authentication") {
|
|
t.Errorf("Primary title %q should contain 'authentication'", spawnedPrimary.Title)
|
|
}
|
|
|
|
// Bond attachment with same variables
|
|
spawnedMol, _ := s.GetIssue(ctx, spawnResult.NewEpicID)
|
|
bondResult, err := bondProtoMol(ctx, s, attachProto, spawnedMol, types.BondTypeSequential, vars, "", "test", false, false)
|
|
if err != nil {
|
|
t.Fatalf("Failed to bond: %v", err)
|
|
}
|
|
|
|
// Verify attachment variable was substituted
|
|
attachedID := bondResult.IDMapping[attachProto.ID]
|
|
attachedIssue, err := s.GetIssue(ctx, attachedID)
|
|
if err != nil {
|
|
t.Fatalf("Failed to get attached issue: %v", err)
|
|
}
|
|
if !strings.Contains(attachedIssue.Title, "2.0") {
|
|
t.Errorf("Attached title %q should contain '2.0'", attachedIssue.Title)
|
|
}
|
|
}
|
|
|
|
// TestSpawnAttachDryRunOutput tests that dry-run includes attachment info
|
|
// This is a lighter test since dry-run is mainly a CLI output concern
|
|
func TestSpawnAttachDryRunOutput(t *testing.T) {
|
|
// The dry-run logic in runMolSpawn outputs attachment info when len(attachments) > 0
|
|
// We verify the data structures that would be used in dry-run
|
|
|
|
type attachmentInfo struct {
|
|
id string
|
|
title string
|
|
subgraph *MoleculeSubgraph
|
|
}
|
|
|
|
// Simulate the attachment info collection
|
|
attachments := []attachmentInfo{
|
|
{id: "test-1", title: "Attachment 1", subgraph: &MoleculeSubgraph{
|
|
Issues: []*types.Issue{{Title: "Issue A"}, {Title: "Issue B"}},
|
|
}},
|
|
{id: "test-2", title: "Attachment 2", subgraph: &MoleculeSubgraph{
|
|
Issues: []*types.Issue{{Title: "Issue C"}},
|
|
}},
|
|
}
|
|
|
|
// Verify attachment count calculation (used in dry-run output)
|
|
totalAttachmentIssues := 0
|
|
for _, attach := range attachments {
|
|
totalAttachmentIssues += len(attach.subgraph.Issues)
|
|
}
|
|
|
|
if totalAttachmentIssues != 3 {
|
|
t.Errorf("Expected 3 total attachment issues, got %d", totalAttachmentIssues)
|
|
}
|
|
|
|
// Verify bond type would be included (sequential is default)
|
|
attachType := types.BondTypeSequential
|
|
if attachType != "sequential" {
|
|
t.Errorf("Expected default attach type 'sequential', got %q", attachType)
|
|
}
|
|
}
|
|
|
|
// TestWispFilteringFromExport verifies that wisp issues are filtered
|
|
// from sync (bd-687g). Wisp issues are stored in the dolt_ignore table,
|
|
// excluded from push/pull, to prevent "zombie" resurrection after mol squash.
|
|
func TestWispFilteringFromExport(t *testing.T) {
|
|
ctx := context.Background()
|
|
dbPath := t.TempDir() + "/test.db"
|
|
s := newTestStore(t, dbPath)
|
|
|
|
// Create a mix of wisp and non-wisp issues
|
|
normalIssue := &types.Issue{
|
|
Title: "Normal Issue",
|
|
Status: types.StatusOpen,
|
|
Priority: 1,
|
|
IssueType: types.TypeTask,
|
|
Ephemeral: false,
|
|
}
|
|
wispIssue := &types.Issue{
|
|
Title: "Wisp Issue",
|
|
Status: types.StatusOpen,
|
|
Priority: 2,
|
|
IssueType: types.TypeTask,
|
|
Ephemeral: true,
|
|
}
|
|
|
|
if err := s.CreateIssue(ctx, normalIssue, "test"); err != nil {
|
|
t.Fatalf("Failed to create normal issue: %v", err)
|
|
}
|
|
if err := s.CreateIssue(ctx, wispIssue, "test"); err != nil {
|
|
t.Fatalf("Failed to create wisp issue: %v", err)
|
|
}
|
|
|
|
// Get all issues from DB - should include both
|
|
allIssues, err := s.SearchIssues(ctx, "", types.IssueFilter{})
|
|
if err != nil {
|
|
t.Fatalf("Failed to search issues: %v", err)
|
|
}
|
|
if len(allIssues) != 2 {
|
|
t.Fatalf("Expected 2 issues in DB, got %d", len(allIssues))
|
|
}
|
|
|
|
// Filter wisp issues (simulating export behavior)
|
|
exportableIssues := make([]*types.Issue, 0)
|
|
for _, issue := range allIssues {
|
|
if !issue.Ephemeral {
|
|
exportableIssues = append(exportableIssues, issue)
|
|
}
|
|
}
|
|
|
|
// Should only have the non-wisp issue
|
|
if len(exportableIssues) != 1 {
|
|
t.Errorf("Expected 1 exportable issue, got %d", len(exportableIssues))
|
|
}
|
|
if exportableIssues[0].ID != normalIssue.ID {
|
|
t.Errorf("Expected normal issue %s, got %s", normalIssue.ID, exportableIssues[0].ID)
|
|
}
|
|
}
|
|
|
|
// =============================================================================
|
|
// Mol Current Tests (bd-nurq)
|
|
// =============================================================================
|
|
|
|
// TestGetMoleculeProgress tests loading a molecule and computing progress
|
|
func TestGetMoleculeProgress(t *testing.T) {
|
|
ctx := context.Background()
|
|
dbPath := t.TempDir() + "/test.db"
|
|
s := newTestStore(t, dbPath)
|
|
|
|
// Create a molecule (epic with template label)
|
|
root := &types.Issue{
|
|
Title: "Test Molecule",
|
|
Status: types.StatusOpen,
|
|
Priority: 1,
|
|
IssueType: types.TypeEpic,
|
|
Labels: []string{BeadsTemplateLabel},
|
|
Assignee: "test-agent",
|
|
}
|
|
if err := s.CreateIssue(ctx, root, "test"); err != nil {
|
|
t.Fatalf("Failed to create root: %v", err)
|
|
}
|
|
|
|
// Create steps with different statuses
|
|
step1 := &types.Issue{
|
|
Title: "Step 1: Done",
|
|
Status: types.StatusClosed,
|
|
Priority: 2,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
step2 := &types.Issue{
|
|
Title: "Step 2: Current",
|
|
Status: types.StatusInProgress,
|
|
Priority: 2,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
step3 := &types.Issue{
|
|
Title: "Step 3: Pending",
|
|
Status: types.StatusOpen,
|
|
Priority: 2,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
|
|
for _, step := range []*types.Issue{step1, step2, step3} {
|
|
if err := s.CreateIssue(ctx, step, "test"); err != nil {
|
|
t.Fatalf("Failed to create step: %v", err)
|
|
}
|
|
// Add parent-child dependency
|
|
if err := s.AddDependency(ctx, &types.Dependency{
|
|
IssueID: step.ID,
|
|
DependsOnID: root.ID,
|
|
Type: types.DepParentChild,
|
|
}, "test"); err != nil {
|
|
t.Fatalf("Failed to add dependency: %v", err)
|
|
}
|
|
}
|
|
|
|
// Add blocking dependency: step3 blocks on step2
|
|
if err := s.AddDependency(ctx, &types.Dependency{
|
|
IssueID: step3.ID,
|
|
DependsOnID: step2.ID,
|
|
Type: types.DepBlocks,
|
|
}, "test"); err != nil {
|
|
t.Fatalf("Failed to add blocking dependency: %v", err)
|
|
}
|
|
|
|
// Get progress
|
|
progress, err := viewMoleculeProgress(ctx, s, root.ID)
|
|
if err != nil {
|
|
t.Fatalf("getMoleculeProgress failed: %v", err)
|
|
}
|
|
|
|
// Verify progress
|
|
if progress.MoleculeID != root.ID {
|
|
t.Errorf("MoleculeID = %q, want %q", progress.MoleculeID, root.ID)
|
|
}
|
|
if progress.MoleculeTitle != root.Title {
|
|
t.Errorf("MoleculeTitle = %q, want %q", progress.MoleculeTitle, root.Title)
|
|
}
|
|
if progress.Assignee != "test-agent" {
|
|
t.Errorf("Assignee = %q, want %q", progress.Assignee, "test-agent")
|
|
}
|
|
if progress.Total != 3 {
|
|
t.Errorf("Total = %d, want 3", progress.Total)
|
|
}
|
|
if progress.Completed != 1 {
|
|
t.Errorf("Completed = %d, want 1", progress.Completed)
|
|
}
|
|
if progress.CurrentStep == nil {
|
|
t.Error("CurrentStep should not be nil")
|
|
} else if progress.CurrentStep.ID != step2.ID {
|
|
t.Errorf("CurrentStep.ID = %q, want %q", progress.CurrentStep.ID, step2.ID)
|
|
}
|
|
if len(progress.Steps) != 3 {
|
|
t.Errorf("Steps count = %d, want 3", len(progress.Steps))
|
|
}
|
|
}
|
|
|
|
// TestFindParentMolecule tests walking up parent-child chain
|
|
func TestFindParentMolecule(t *testing.T) {
|
|
ctx := context.Background()
|
|
dbPath := t.TempDir() + "/test.db"
|
|
s := newTestStore(t, dbPath)
|
|
|
|
// Create molecule root (epic with template label)
|
|
root := &types.Issue{
|
|
Title: "Molecule Root",
|
|
Status: types.StatusOpen,
|
|
Priority: 1,
|
|
IssueType: types.TypeEpic,
|
|
Labels: []string{BeadsTemplateLabel},
|
|
}
|
|
if err := s.CreateIssue(ctx, root, "test"); err != nil {
|
|
t.Fatalf("Failed to create root: %v", err)
|
|
}
|
|
|
|
// Create child step
|
|
child := &types.Issue{
|
|
Title: "Child Step",
|
|
Status: types.StatusOpen,
|
|
Priority: 2,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
if err := s.CreateIssue(ctx, child, "test"); err != nil {
|
|
t.Fatalf("Failed to create child: %v", err)
|
|
}
|
|
if err := s.AddDependency(ctx, &types.Dependency{
|
|
IssueID: child.ID,
|
|
DependsOnID: root.ID,
|
|
Type: types.DepParentChild,
|
|
}, "test"); err != nil {
|
|
t.Fatalf("Failed to add parent-child: %v", err)
|
|
}
|
|
|
|
// Create grandchild
|
|
grandchild := &types.Issue{
|
|
Title: "Grandchild Step",
|
|
Status: types.StatusOpen,
|
|
Priority: 3,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
if err := s.CreateIssue(ctx, grandchild, "test"); err != nil {
|
|
t.Fatalf("Failed to create grandchild: %v", err)
|
|
}
|
|
if err := s.AddDependency(ctx, &types.Dependency{
|
|
IssueID: grandchild.ID,
|
|
DependsOnID: child.ID,
|
|
Type: types.DepParentChild,
|
|
}, "test"); err != nil {
|
|
t.Fatalf("Failed to add grandchild parent-child: %v", err)
|
|
}
|
|
|
|
// Find parent molecule from grandchild
|
|
moleculeID := mustFindParentMolecule(t, ctx, s, grandchild.ID)
|
|
if moleculeID != root.ID {
|
|
t.Errorf("findParentMolecule(grandchild) = %q, want %q", moleculeID, root.ID)
|
|
}
|
|
|
|
// Find parent molecule from child
|
|
moleculeID = mustFindParentMolecule(t, ctx, s, child.ID)
|
|
if moleculeID != root.ID {
|
|
t.Errorf("findParentMolecule(child) = %q, want %q", moleculeID, root.ID)
|
|
}
|
|
|
|
// Find parent molecule from root
|
|
moleculeID = mustFindParentMolecule(t, ctx, s, root.ID)
|
|
if moleculeID != root.ID {
|
|
t.Errorf("findParentMolecule(root) = %q, want %q", moleculeID, root.ID)
|
|
}
|
|
|
|
// Create orphan issue (not part of any molecule)
|
|
orphan := &types.Issue{
|
|
Title: "Orphan Issue",
|
|
Status: types.StatusOpen,
|
|
Priority: 2,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
if err := s.CreateIssue(ctx, orphan, "test"); err != nil {
|
|
t.Fatalf("Failed to create orphan: %v", err)
|
|
}
|
|
|
|
// Should return empty for orphan
|
|
moleculeID = mustFindParentMolecule(t, ctx, s, orphan.ID)
|
|
if moleculeID != "" {
|
|
t.Errorf("findParentMolecule(orphan) = %q, want empty", moleculeID)
|
|
}
|
|
}
|
|
|
|
// TestFindParentMoleculesBatch tests batch-finding molecule roots (bd-hn4q)
|
|
func TestFindParentMoleculesBatch(t *testing.T) {
|
|
ctx := context.Background()
|
|
dbPath := t.TempDir() + "/test.db"
|
|
s := newTestStore(t, dbPath)
|
|
|
|
// Create molecule root (epic with template label)
|
|
root := &types.Issue{
|
|
Title: "Molecule Root",
|
|
Status: types.StatusOpen,
|
|
Priority: 1,
|
|
IssueType: types.TypeEpic,
|
|
Labels: []string{BeadsTemplateLabel},
|
|
}
|
|
if err := s.CreateIssue(ctx, root, "test"); err != nil {
|
|
t.Fatalf("Failed to create root: %v", err)
|
|
}
|
|
|
|
// Create child step
|
|
child := &types.Issue{
|
|
Title: "Child Step",
|
|
Status: types.StatusOpen,
|
|
Priority: 2,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
if err := s.CreateIssue(ctx, child, "test"); err != nil {
|
|
t.Fatalf("Failed to create child: %v", err)
|
|
}
|
|
if err := s.AddDependency(ctx, &types.Dependency{
|
|
IssueID: child.ID,
|
|
DependsOnID: root.ID,
|
|
Type: types.DepParentChild,
|
|
}, "test"); err != nil {
|
|
t.Fatalf("Failed to add parent-child: %v", err)
|
|
}
|
|
|
|
// Create grandchild
|
|
grandchild := &types.Issue{
|
|
Title: "Grandchild Step",
|
|
Status: types.StatusOpen,
|
|
Priority: 3,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
if err := s.CreateIssue(ctx, grandchild, "test"); err != nil {
|
|
t.Fatalf("Failed to create grandchild: %v", err)
|
|
}
|
|
if err := s.AddDependency(ctx, &types.Dependency{
|
|
IssueID: grandchild.ID,
|
|
DependsOnID: child.ID,
|
|
Type: types.DepParentChild,
|
|
}, "test"); err != nil {
|
|
t.Fatalf("Failed to add grandchild parent-child: %v", err)
|
|
}
|
|
|
|
// Create orphan issue (not part of any molecule)
|
|
orphan := &types.Issue{
|
|
Title: "Orphan Issue",
|
|
Status: types.StatusOpen,
|
|
Priority: 2,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
if err := s.CreateIssue(ctx, orphan, "test"); err != nil {
|
|
t.Fatalf("Failed to create orphan: %v", err)
|
|
}
|
|
|
|
// Batch-find molecule roots for all issues at once
|
|
roots, err := moleculeRootsOf(ctx, s, []string{grandchild.ID, child.ID, root.ID, orphan.ID})
|
|
if err != nil {
|
|
t.Fatalf("findParentMolecules: %v", err)
|
|
}
|
|
|
|
if got := roots[grandchild.ID]; got != root.ID {
|
|
t.Errorf("findParentMolecules[grandchild] = %q, want %q", got, root.ID)
|
|
}
|
|
if got := roots[child.ID]; got != root.ID {
|
|
t.Errorf("findParentMolecules[child] = %q, want %q", got, root.ID)
|
|
}
|
|
if got := roots[root.ID]; got != root.ID {
|
|
t.Errorf("findParentMolecules[root] = %q, want %q", got, root.ID)
|
|
}
|
|
if got := roots[orphan.ID]; got != "" {
|
|
t.Errorf("findParentMolecules[orphan] = %q, want empty", got)
|
|
}
|
|
|
|
// Empty input answers no roots.
|
|
if result, err := moleculeRootsOf(ctx, s, nil); err != nil || len(result) != 0 {
|
|
t.Errorf("MoleculeRoots(nil) = %v, %v, want none", result, err)
|
|
}
|
|
}
|
|
|
|
// TestFindParentMolecule_RootShapes covers every shape of molecule root that
|
|
// findParentMolecule(s) needs to recognize:
|
|
// - TypeEpic + BeadsTemplateLabel (distilled / older formula path)
|
|
// - TypeMolecule with no label (`bd mol pour` path — gastownhall/beads#3719)
|
|
// - TypeTask + BeadsTemplateLabel (template-label-only fallback)
|
|
// - TypeTask, no label (not a molecule)
|
|
//
|
|
// Before the fix for #3719 the TypeMolecule case returned "", which caused
|
|
// `bd close --continue` to silently no-op for any poured molecule.
|
|
func TestFindParentMolecule_RootShapes(t *testing.T) {
|
|
ctx := context.Background()
|
|
dbPath := t.TempDir() + "/test.db"
|
|
s := newTestStore(t, dbPath)
|
|
|
|
tests := []struct {
|
|
name string
|
|
rootType types.IssueType
|
|
rootLabels []string
|
|
isMolecule bool
|
|
}{
|
|
{"epic with template label", types.TypeEpic, []string{BeadsTemplateLabel}, true},
|
|
{"poured molecule (no label)", types.TypeMolecule, nil, true},
|
|
{"task with template label", types.TypeTask, []string{BeadsTemplateLabel}, true},
|
|
{"plain task is not a molecule", types.TypeTask, nil, false},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
root := &types.Issue{
|
|
Title: tt.name + " root",
|
|
Status: types.StatusOpen,
|
|
Priority: 1,
|
|
IssueType: tt.rootType,
|
|
Labels: tt.rootLabels,
|
|
}
|
|
if err := s.CreateIssue(ctx, root, "test"); err != nil {
|
|
t.Fatalf("Failed to create root: %v", err)
|
|
}
|
|
|
|
child := &types.Issue{
|
|
Title: tt.name + " child",
|
|
Status: types.StatusOpen,
|
|
Priority: 2,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
if err := s.CreateIssue(ctx, child, "test"); err != nil {
|
|
t.Fatalf("Failed to create child: %v", err)
|
|
}
|
|
if err := s.AddDependency(ctx, &types.Dependency{
|
|
IssueID: child.ID,
|
|
DependsOnID: root.ID,
|
|
Type: types.DepParentChild,
|
|
}, "test"); err != nil {
|
|
t.Fatalf("Failed to add parent-child: %v", err)
|
|
}
|
|
|
|
wantSingle := ""
|
|
if tt.isMolecule {
|
|
wantSingle = root.ID
|
|
}
|
|
if got := mustFindParentMolecule(t, ctx, s, child.ID); got != wantSingle {
|
|
t.Errorf("findParentMolecule(child) = %q, want %q", got, wantSingle)
|
|
}
|
|
|
|
wantBatch := map[string]string{child.ID: wantSingle}
|
|
gotBatch, err := moleculeRootsOf(ctx, s, []string{child.ID})
|
|
if err != nil {
|
|
t.Fatalf("findParentMolecules: %v", err)
|
|
}
|
|
if got := gotBatch[child.ID]; got != wantBatch[child.ID] {
|
|
t.Errorf("findParentMolecules(child) = %q, want %q", got, wantBatch[child.ID])
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestAdvanceToNextStep_PouredMolecule reproduces gastownhall/beads#3719: when a
|
|
// step lives under a TypeMolecule root (as `bd mol pour` creates), AdvanceToNextStep
|
|
// must find the parent molecule and (with autoClaim=true) claim the next ready step.
|
|
// Before the fix it silently returned (nil, nil).
|
|
func TestAdvanceToNextStep_PouredMolecule(t *testing.T) {
|
|
ctx := context.Background()
|
|
dbPath := t.TempDir() + "/test.db"
|
|
s := newTestStore(t, dbPath)
|
|
|
|
// Root shaped like `bd mol pour` output: TypeMolecule, no template label.
|
|
root := &types.Issue{
|
|
Title: "Poured Molecule",
|
|
Status: types.StatusOpen,
|
|
Priority: 1,
|
|
IssueType: types.TypeMolecule,
|
|
}
|
|
if err := s.CreateIssue(ctx, root, "test"); err != nil {
|
|
t.Fatalf("Failed to create root: %v", err)
|
|
}
|
|
|
|
step1 := &types.Issue{
|
|
Title: "Step 1",
|
|
Status: types.StatusClosed,
|
|
Priority: 2,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
step2 := &types.Issue{
|
|
Title: "Step 2",
|
|
Status: types.StatusOpen,
|
|
Priority: 2,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
for _, step := range []*types.Issue{step1, step2} {
|
|
if err := s.CreateIssue(ctx, step, "test"); err != nil {
|
|
t.Fatalf("Failed to create step: %v", err)
|
|
}
|
|
if err := s.AddDependency(ctx, &types.Dependency{
|
|
IssueID: step.ID,
|
|
DependsOnID: root.ID,
|
|
Type: types.DepParentChild,
|
|
}, "test"); err != nil {
|
|
t.Fatalf("Failed to add parent-child: %v", err)
|
|
}
|
|
}
|
|
if err := s.AddDependency(ctx, &types.Dependency{
|
|
IssueID: step2.ID,
|
|
DependsOnID: step1.ID,
|
|
Type: types.DepBlocks,
|
|
}, "test"); err != nil {
|
|
t.Fatalf("Failed to add blocking dep: %v", err)
|
|
}
|
|
|
|
result, err := advanceForTest(ctx, s, step1.ID, true, "test")
|
|
if err != nil {
|
|
t.Fatalf("AdvanceToNextStep failed: %v", err)
|
|
}
|
|
if result == nil {
|
|
t.Fatal("AdvanceToNextStep returned nil result; expected molecule advance for TypeMolecule root")
|
|
}
|
|
if result.MoleculeID != root.ID {
|
|
t.Errorf("MoleculeID = %q, want %q", result.MoleculeID, root.ID)
|
|
}
|
|
if result.NextStep == nil || result.NextStep.ID != step2.ID {
|
|
t.Errorf("NextStep = %v, want step2 (%s)", result.NextStep, step2.ID)
|
|
}
|
|
if !result.AutoAdvanced {
|
|
t.Errorf("AutoAdvanced = false; want true (autoClaim was on)")
|
|
}
|
|
|
|
// Confirm step2 is in_progress now.
|
|
got, err := s.GetIssue(ctx, step2.ID)
|
|
if err != nil {
|
|
t.Fatalf("GetIssue(step2) failed: %v", err)
|
|
}
|
|
if got.Status != types.StatusInProgress {
|
|
t.Errorf("step2 status = %q, want %q", got.Status, types.StatusInProgress)
|
|
}
|
|
}
|
|
|
|
// TestFindHookedMolecules tests finding molecules bonded to hooked issues
|
|
func TestFindHookedMolecules(t *testing.T) {
|
|
ctx := context.Background()
|
|
dbPath := t.TempDir() + "/test.db"
|
|
s := newTestStore(t, dbPath)
|
|
|
|
// Create molecule root (epic)
|
|
molecule := &types.Issue{
|
|
Title: "Test Molecule",
|
|
Status: types.StatusOpen,
|
|
Priority: 1,
|
|
IssueType: types.TypeEpic,
|
|
}
|
|
if err := s.CreateIssue(ctx, molecule, "test"); err != nil {
|
|
t.Fatalf("Failed to create molecule: %v", err)
|
|
}
|
|
|
|
// Create step as child of molecule
|
|
step := &types.Issue{
|
|
Title: "Step 1",
|
|
Status: types.StatusOpen,
|
|
Priority: 2,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
if err := s.CreateIssue(ctx, step, "test"); err != nil {
|
|
t.Fatalf("Failed to create step: %v", err)
|
|
}
|
|
if err := s.AddDependency(ctx, &types.Dependency{
|
|
IssueID: step.ID,
|
|
DependsOnID: molecule.ID,
|
|
Type: types.DepParentChild,
|
|
}, "test"); err != nil {
|
|
t.Fatalf("Failed to add parent-child: %v", err)
|
|
}
|
|
|
|
// Create hooked issue with blocks dependency on molecule
|
|
hookedIssue := &types.Issue{
|
|
Title: "Hooked Work",
|
|
Status: types.StatusHooked,
|
|
Priority: 2,
|
|
IssueType: types.TypeTask,
|
|
Assignee: "test-agent",
|
|
}
|
|
if err := s.CreateIssue(ctx, hookedIssue, "test"); err != nil {
|
|
t.Fatalf("Failed to create hooked issue: %v", err)
|
|
}
|
|
if err := s.AddDependency(ctx, &types.Dependency{
|
|
IssueID: hookedIssue.ID,
|
|
DependsOnID: molecule.ID,
|
|
Type: types.DepBlocks,
|
|
}, "test"); err != nil {
|
|
t.Fatalf("Failed to add blocks dependency: %v", err)
|
|
}
|
|
|
|
// Test: findHookedMolecules should find the molecule for this agent
|
|
molecules := mustFindHookedMolecules(t, ctx, s, "test-agent")
|
|
if len(molecules) != 1 {
|
|
t.Fatalf("findHookedMolecules() got %d molecules, want 1", len(molecules))
|
|
}
|
|
if molecules[0].MoleculeID != molecule.ID {
|
|
t.Errorf("findHookedMolecules() got molecule %q, want %q", molecules[0].MoleculeID, molecule.ID)
|
|
}
|
|
|
|
// Test: different agent should not find the molecule
|
|
molecules = mustFindHookedMolecules(t, ctx, s, "other-agent")
|
|
if len(molecules) != 0 {
|
|
t.Errorf("findHookedMolecules(other-agent) got %d molecules, want 0", len(molecules))
|
|
}
|
|
|
|
// Test: no agent filter should find the molecule
|
|
molecules = mustFindHookedMolecules(t, ctx, s, "")
|
|
if len(molecules) != 1 {
|
|
t.Errorf("findHookedMolecules('') got %d molecules, want 1", len(molecules))
|
|
}
|
|
}
|
|
|
|
// TestAdvanceToNextStep tests auto-advancing to next step
|
|
func TestAdvanceToNextStep(t *testing.T) {
|
|
ctx := context.Background()
|
|
dbPath := t.TempDir() + "/test.db"
|
|
s := newTestStore(t, dbPath)
|
|
|
|
// Create molecule with sequential steps
|
|
root := &types.Issue{
|
|
Title: "Advance Test Molecule",
|
|
Status: types.StatusOpen,
|
|
Priority: 1,
|
|
IssueType: types.TypeEpic,
|
|
Labels: []string{BeadsTemplateLabel},
|
|
}
|
|
if err := s.CreateIssue(ctx, root, "test"); err != nil {
|
|
t.Fatalf("Failed to create root: %v", err)
|
|
}
|
|
|
|
step1 := &types.Issue{
|
|
Title: "Step 1",
|
|
Status: types.StatusClosed,
|
|
Priority: 2,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
step2 := &types.Issue{
|
|
Title: "Step 2",
|
|
Status: types.StatusOpen,
|
|
Priority: 2,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
|
|
for _, step := range []*types.Issue{step1, step2} {
|
|
if err := s.CreateIssue(ctx, step, "test"); err != nil {
|
|
t.Fatalf("Failed to create step: %v", err)
|
|
}
|
|
if err := s.AddDependency(ctx, &types.Dependency{
|
|
IssueID: step.ID,
|
|
DependsOnID: root.ID,
|
|
Type: types.DepParentChild,
|
|
}, "test"); err != nil {
|
|
t.Fatalf("Failed to add dependency: %v", err)
|
|
}
|
|
}
|
|
|
|
// step2 blocks on step1
|
|
if err := s.AddDependency(ctx, &types.Dependency{
|
|
IssueID: step2.ID,
|
|
DependsOnID: step1.ID,
|
|
Type: types.DepBlocks,
|
|
}, "test"); err != nil {
|
|
t.Fatalf("Failed to add blocking dependency: %v", err)
|
|
}
|
|
|
|
// Advance from step1 (just closed) without auto-claim
|
|
result, err := advanceForTest(ctx, s, step1.ID, false, "test")
|
|
if err != nil {
|
|
t.Fatalf("AdvanceToNextStep failed: %v", err)
|
|
}
|
|
if result == nil {
|
|
t.Fatal("result should not be nil")
|
|
}
|
|
if result.MoleculeID != root.ID {
|
|
t.Errorf("MoleculeID = %q, want %q", result.MoleculeID, root.ID)
|
|
}
|
|
if result.NextStep == nil {
|
|
t.Error("NextStep should not be nil")
|
|
} else if result.NextStep.ID != step2.ID {
|
|
t.Errorf("NextStep.ID = %q, want %q", result.NextStep.ID, step2.ID)
|
|
}
|
|
if result.AutoAdvanced {
|
|
t.Error("AutoAdvanced should be false when not requested")
|
|
}
|
|
|
|
// Verify step2 is still open
|
|
step2Updated, _ := s.GetIssue(ctx, step2.ID)
|
|
if step2Updated.Status != types.StatusOpen {
|
|
t.Errorf("Step2 status = %v, want open (no auto-claim)", step2Updated.Status)
|
|
}
|
|
|
|
// Now test with auto-claim
|
|
result, err = advanceForTest(ctx, s, step1.ID, true, "test")
|
|
if err != nil {
|
|
t.Fatalf("AdvanceToNextStep with auto-claim failed: %v", err)
|
|
}
|
|
if !result.AutoAdvanced {
|
|
t.Error("AutoAdvanced should be true when requested")
|
|
}
|
|
|
|
// Verify step2 is now in_progress
|
|
step2Updated, _ = s.GetIssue(ctx, step2.ID)
|
|
if step2Updated.Status != types.StatusInProgress {
|
|
t.Errorf("Step2 status = %v, want in_progress (auto-claim)", step2Updated.Status)
|
|
}
|
|
}
|
|
|
|
// TestAdvanceToNextStepMoleculeComplete tests behavior when molecule is complete
|
|
func TestAdvanceToNextStepMoleculeComplete(t *testing.T) {
|
|
ctx := context.Background()
|
|
dbPath := t.TempDir() + "/test.db"
|
|
s := newTestStore(t, dbPath)
|
|
|
|
// Create molecule with single step
|
|
root := &types.Issue{
|
|
Title: "Complete Test Molecule",
|
|
Status: types.StatusOpen,
|
|
Priority: 1,
|
|
IssueType: types.TypeEpic,
|
|
Labels: []string{BeadsTemplateLabel},
|
|
}
|
|
if err := s.CreateIssue(ctx, root, "test"); err != nil {
|
|
t.Fatalf("Failed to create root: %v", err)
|
|
}
|
|
|
|
step1 := &types.Issue{
|
|
Title: "Only Step",
|
|
Status: types.StatusClosed,
|
|
Priority: 2,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
if err := s.CreateIssue(ctx, step1, "test"); err != nil {
|
|
t.Fatalf("Failed to create step: %v", err)
|
|
}
|
|
if err := s.AddDependency(ctx, &types.Dependency{
|
|
IssueID: step1.ID,
|
|
DependsOnID: root.ID,
|
|
Type: types.DepParentChild,
|
|
}, "test"); err != nil {
|
|
t.Fatalf("Failed to add dependency: %v", err)
|
|
}
|
|
|
|
// Advance from the only step (molecule should be complete)
|
|
result, err := advanceForTest(ctx, s, step1.ID, false, "test")
|
|
if err != nil {
|
|
t.Fatalf("AdvanceToNextStep failed: %v", err)
|
|
}
|
|
if result == nil {
|
|
t.Fatal("result should not be nil")
|
|
}
|
|
if !result.MolComplete {
|
|
t.Error("MolComplete should be true when all steps are done")
|
|
}
|
|
if result.NextStep != nil {
|
|
t.Error("NextStep should be nil when molecule is complete")
|
|
}
|
|
}
|
|
|
|
// TestAdvanceToNextStepOrphanIssue tests behavior for non-molecule issues
|
|
func TestAdvanceToNextStepOrphanIssue(t *testing.T) {
|
|
ctx := context.Background()
|
|
dbPath := t.TempDir() + "/test.db"
|
|
s := newTestStore(t, dbPath)
|
|
|
|
// Create standalone issue (not part of molecule)
|
|
orphan := &types.Issue{
|
|
Title: "Standalone Issue",
|
|
Status: types.StatusClosed,
|
|
Priority: 2,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
if err := s.CreateIssue(ctx, orphan, "test"); err != nil {
|
|
t.Fatalf("Failed to create orphan: %v", err)
|
|
}
|
|
|
|
// Advance should return nil (not part of molecule)
|
|
result, err := advanceForTest(ctx, s, orphan.ID, false, "test")
|
|
if err != nil {
|
|
t.Fatalf("AdvanceToNextStep failed: %v", err)
|
|
}
|
|
if result != nil {
|
|
t.Error("result should be nil for orphan issue")
|
|
}
|
|
}
|
|
|
|
// TestAdvanceToNextStepConcurrentClaim tests optimistic concurrency: when the
|
|
// first ready step has already been claimed by another agent, AdvanceToNextStep
|
|
// falls back to the next available ready step.
|
|
func TestAdvanceToNextStepConcurrentClaim(t *testing.T) {
|
|
ctx := context.Background()
|
|
dbPath := t.TempDir() + "/test.db"
|
|
s := newTestStore(t, dbPath)
|
|
|
|
// Create molecule: root -> step1 (closed), step2 (open), step3 (open)
|
|
// step2 and step3 both depend on step1, so both become ready when step1 closes.
|
|
root := &types.Issue{
|
|
Title: "Concurrent Claim Test Molecule",
|
|
Status: types.StatusOpen,
|
|
Priority: 1,
|
|
IssueType: types.TypeEpic,
|
|
Labels: []string{BeadsTemplateLabel},
|
|
}
|
|
if err := s.CreateIssue(ctx, root, "test"); err != nil {
|
|
t.Fatalf("Failed to create root: %v", err)
|
|
}
|
|
|
|
step1 := &types.Issue{
|
|
Title: "Step 1 (closed)",
|
|
Status: types.StatusClosed,
|
|
Priority: 2,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
step2 := &types.Issue{
|
|
Title: "Step 2 (will be pre-claimed)",
|
|
Status: types.StatusOpen,
|
|
Priority: 2,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
step3 := &types.Issue{
|
|
Title: "Step 3 (fallback target)",
|
|
Status: types.StatusOpen,
|
|
Priority: 2,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
|
|
for _, step := range []*types.Issue{step1, step2, step3} {
|
|
if err := s.CreateIssue(ctx, step, "test"); err != nil {
|
|
t.Fatalf("Failed to create step: %v", err)
|
|
}
|
|
if err := s.AddDependency(ctx, &types.Dependency{
|
|
IssueID: step.ID,
|
|
DependsOnID: root.ID,
|
|
Type: types.DepParentChild,
|
|
}, "test"); err != nil {
|
|
t.Fatalf("Failed to add parent dependency: %v", err)
|
|
}
|
|
}
|
|
|
|
// step2 and step3 both depend on step1
|
|
for _, step := range []*types.Issue{step2, step3} {
|
|
if err := s.AddDependency(ctx, &types.Dependency{
|
|
IssueID: step.ID,
|
|
DependsOnID: step1.ID,
|
|
Type: types.DepBlocks,
|
|
}, "test"); err != nil {
|
|
t.Fatalf("Failed to add blocking dependency: %v", err)
|
|
}
|
|
}
|
|
|
|
// Simulate another agent claiming step2 (TOCTOU race scenario)
|
|
if err := s.UpdateIssue(ctx, step2.ID, map[string]interface{}{
|
|
"status": types.StatusInProgress,
|
|
}, "other-agent"); err != nil {
|
|
t.Fatalf("Failed to pre-claim step2: %v", err)
|
|
}
|
|
|
|
// Now our agent tries to advance with autoClaim — step2 was identified as
|
|
// ready during the read phase, but it's already in_progress. The function
|
|
// should fall back to step3.
|
|
result, err := advanceForTest(ctx, s, step1.ID, true, "our-agent")
|
|
if err != nil {
|
|
t.Fatalf("AdvanceToNextStep failed: %v", err)
|
|
}
|
|
if result == nil {
|
|
t.Fatal("result should not be nil")
|
|
}
|
|
if !result.AutoAdvanced {
|
|
t.Error("AutoAdvanced should be true (should have claimed step3)")
|
|
}
|
|
if result.NextStep == nil {
|
|
t.Fatal("NextStep should not be nil")
|
|
}
|
|
if result.NextStep.ID != step3.ID {
|
|
t.Errorf("NextStep.ID = %q, want %q (should fall back to step3)", result.NextStep.ID, step3.ID)
|
|
}
|
|
|
|
// Verify step3 is now in_progress
|
|
step3Updated, _ := s.GetIssue(ctx, step3.ID)
|
|
if step3Updated.Status != types.StatusInProgress {
|
|
t.Errorf("Step3 status = %v, want in_progress", step3Updated.Status)
|
|
}
|
|
}
|
|
|
|
// TestAdvanceToNextStepAllClaimed tests that when all ready steps are already
|
|
// claimed by other agents, AdvanceToNextStep reports no auto-advance.
|
|
func TestAdvanceToNextStepAllClaimed(t *testing.T) {
|
|
ctx := context.Background()
|
|
dbPath := t.TempDir() + "/test.db"
|
|
s := newTestStore(t, dbPath)
|
|
|
|
// Create molecule: root -> step1 (closed), step2 (open, will be pre-claimed)
|
|
root := &types.Issue{
|
|
Title: "All Claimed Test Molecule",
|
|
Status: types.StatusOpen,
|
|
Priority: 1,
|
|
IssueType: types.TypeEpic,
|
|
Labels: []string{BeadsTemplateLabel},
|
|
}
|
|
if err := s.CreateIssue(ctx, root, "test"); err != nil {
|
|
t.Fatalf("Failed to create root: %v", err)
|
|
}
|
|
|
|
step1 := &types.Issue{
|
|
Title: "Step 1 (closed)",
|
|
Status: types.StatusClosed,
|
|
Priority: 2,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
step2 := &types.Issue{
|
|
Title: "Step 2 (only ready, will be pre-claimed)",
|
|
Status: types.StatusOpen,
|
|
Priority: 2,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
|
|
for _, step := range []*types.Issue{step1, step2} {
|
|
if err := s.CreateIssue(ctx, step, "test"); err != nil {
|
|
t.Fatalf("Failed to create step: %v", err)
|
|
}
|
|
if err := s.AddDependency(ctx, &types.Dependency{
|
|
IssueID: step.ID,
|
|
DependsOnID: root.ID,
|
|
Type: types.DepParentChild,
|
|
}, "test"); err != nil {
|
|
t.Fatalf("Failed to add parent dependency: %v", err)
|
|
}
|
|
}
|
|
|
|
// step2 depends on step1
|
|
if err := s.AddDependency(ctx, &types.Dependency{
|
|
IssueID: step2.ID,
|
|
DependsOnID: step1.ID,
|
|
Type: types.DepBlocks,
|
|
}, "test"); err != nil {
|
|
t.Fatalf("Failed to add blocking dependency: %v", err)
|
|
}
|
|
|
|
// Pre-claim the only ready step
|
|
if err := s.UpdateIssue(ctx, step2.ID, map[string]interface{}{
|
|
"status": types.StatusInProgress,
|
|
}, "other-agent"); err != nil {
|
|
t.Fatalf("Failed to pre-claim step2: %v", err)
|
|
}
|
|
|
|
// Advance with autoClaim — should fail gracefully (no steps to claim)
|
|
result, err := advanceForTest(ctx, s, step1.ID, true, "our-agent")
|
|
if err != nil {
|
|
t.Fatalf("AdvanceToNextStep failed: %v", err)
|
|
}
|
|
if result == nil {
|
|
t.Fatal("result should not be nil")
|
|
}
|
|
if result.AutoAdvanced {
|
|
t.Error("AutoAdvanced should be false when all ready steps are claimed")
|
|
}
|
|
}
|
|
|
|
// =============================================================================
|
|
// Dynamic Bonding Tests (bd-xo1o.1)
|
|
// =============================================================================
|
|
|
|
// TestGenerateBondedID tests the custom ID generation for dynamic bonding
|
|
func TestGenerateBondedID(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
oldID string
|
|
rootID string
|
|
opts CloneOptions
|
|
wantID string
|
|
wantErr bool
|
|
errMatch string
|
|
}{
|
|
{
|
|
name: "root issue with simple childRef",
|
|
oldID: "mol-arm",
|
|
rootID: "mol-arm",
|
|
opts: CloneOptions{
|
|
ParentID: "patrol-x7k",
|
|
ChildRef: "arm-ace",
|
|
},
|
|
wantID: "patrol-x7k.arm-ace",
|
|
},
|
|
{
|
|
name: "root issue with variable substitution",
|
|
oldID: "mol-arm",
|
|
rootID: "mol-arm",
|
|
opts: CloneOptions{
|
|
ParentID: "patrol-x7k",
|
|
ChildRef: "arm-{{polecat_name}}",
|
|
Vars: map[string]string{"polecat_name": "ace"},
|
|
},
|
|
wantID: "patrol-x7k.arm-ace",
|
|
},
|
|
{
|
|
name: "child issue with relative ID",
|
|
oldID: "mol-arm.capture",
|
|
rootID: "mol-arm",
|
|
opts: CloneOptions{
|
|
ParentID: "patrol-x7k",
|
|
ChildRef: "arm-ace",
|
|
},
|
|
wantID: "patrol-x7k.arm-ace.capture",
|
|
},
|
|
{
|
|
name: "nested child issue",
|
|
oldID: "mol-arm.capture.sub",
|
|
rootID: "mol-arm",
|
|
opts: CloneOptions{
|
|
ParentID: "patrol-x7k",
|
|
ChildRef: "arm-ace",
|
|
},
|
|
wantID: "patrol-x7k.arm-ace.capture.sub",
|
|
},
|
|
{
|
|
name: "no parent ID returns empty (not a bonded operation)",
|
|
oldID: "mol-arm",
|
|
rootID: "mol-arm",
|
|
opts: CloneOptions{},
|
|
wantID: "",
|
|
},
|
|
{
|
|
name: "empty childRef after substitution is error",
|
|
oldID: "mol-arm",
|
|
rootID: "mol-arm",
|
|
opts: CloneOptions{
|
|
ParentID: "patrol-x7k",
|
|
ChildRef: "{{missing_var}}",
|
|
},
|
|
wantErr: true,
|
|
errMatch: "invalid childRef",
|
|
},
|
|
{
|
|
name: "childRef with special chars is error",
|
|
oldID: "mol-arm",
|
|
rootID: "mol-arm",
|
|
opts: CloneOptions{
|
|
ParentID: "patrol-x7k",
|
|
ChildRef: "arm/ace",
|
|
},
|
|
wantErr: true,
|
|
errMatch: "invalid childRef",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
gotID, err := generateBondedID(tt.oldID, tt.rootID, tt.opts)
|
|
|
|
if tt.wantErr {
|
|
if err == nil {
|
|
t.Errorf("generateBondedID() expected error containing %q, got nil", tt.errMatch)
|
|
} else if !strings.Contains(err.Error(), tt.errMatch) {
|
|
t.Errorf("generateBondedID() error = %q, want error containing %q", err.Error(), tt.errMatch)
|
|
}
|
|
return
|
|
}
|
|
|
|
if err != nil {
|
|
t.Errorf("generateBondedID() unexpected error: %v", err)
|
|
return
|
|
}
|
|
|
|
if gotID != tt.wantID {
|
|
t.Errorf("generateBondedID() = %q, want %q", gotID, tt.wantID)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestGetRelativeID tests extracting relative portion from child IDs
|
|
func TestGetRelativeID(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
oldID string
|
|
rootID string
|
|
want string
|
|
}{
|
|
{
|
|
name: "same ID returns empty",
|
|
oldID: "mol-arm",
|
|
rootID: "mol-arm",
|
|
want: "",
|
|
},
|
|
{
|
|
name: "child with single step",
|
|
oldID: "mol-arm.capture",
|
|
rootID: "mol-arm",
|
|
want: "capture",
|
|
},
|
|
{
|
|
name: "child with nested steps",
|
|
oldID: "mol-arm.capture.sub.deep",
|
|
rootID: "mol-arm",
|
|
want: "capture.sub.deep",
|
|
},
|
|
{
|
|
name: "unrelated IDs returns empty",
|
|
oldID: "other-123",
|
|
rootID: "mol-arm",
|
|
want: "",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := getRelativeID(tt.oldID, tt.rootID)
|
|
if got != tt.want {
|
|
t.Errorf("getRelativeID() = %q, want %q", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestBondProtoMolWithRef tests dynamic bonding with custom child references
|
|
func TestBondProtoMolWithRef(t *testing.T) {
|
|
ctx := context.Background()
|
|
dbPath := t.TempDir() + "/test.db"
|
|
s := newTestStoreWithPrefix(t, dbPath, "patrol")
|
|
|
|
// Create a proto with child steps (mol-polecat-arm template)
|
|
protoRoot := &types.Issue{
|
|
Title: "Polecat Arm: {{polecat_name}}",
|
|
IssueType: types.TypeEpic,
|
|
Status: types.StatusOpen,
|
|
Priority: 2,
|
|
Labels: []string{MoleculeLabel},
|
|
}
|
|
if err := s.CreateIssue(ctx, protoRoot, "test"); err != nil {
|
|
t.Fatalf("Failed to create proto root: %v", err)
|
|
}
|
|
|
|
// Add proto steps
|
|
protoCapture := &types.Issue{
|
|
Title: "Capture {{polecat_name}}",
|
|
IssueType: types.TypeTask,
|
|
Status: types.StatusOpen,
|
|
Priority: 2,
|
|
}
|
|
if err := s.CreateIssue(ctx, protoCapture, "test"); err != nil {
|
|
t.Fatalf("Failed to create proto capture: %v", err)
|
|
}
|
|
if err := s.AddDependency(ctx, &types.Dependency{
|
|
IssueID: protoCapture.ID,
|
|
DependsOnID: protoRoot.ID,
|
|
Type: types.DepParentChild,
|
|
}, "test"); err != nil {
|
|
t.Fatalf("Failed to add proto dependency: %v", err)
|
|
}
|
|
|
|
// Create target molecule (patrol-xxx)
|
|
patrol := &types.Issue{
|
|
Title: "Witness Patrol",
|
|
IssueType: types.TypeEpic,
|
|
Status: types.StatusInProgress,
|
|
Priority: 1,
|
|
}
|
|
if err := s.CreateIssue(ctx, patrol, "test"); err != nil {
|
|
t.Fatalf("Failed to create patrol: %v", err)
|
|
}
|
|
|
|
// Bond proto to patrol with custom child ref
|
|
vars := map[string]string{"polecat_name": "ace"}
|
|
childRef := "arm-{{polecat_name}}"
|
|
result, err := bondProtoMol(ctx, s, protoRoot, patrol, types.BondTypeSequential, vars, childRef, "test", false, false)
|
|
if err != nil {
|
|
t.Fatalf("bondProtoMol failed: %v", err)
|
|
}
|
|
|
|
// Verify spawned count
|
|
if result.Spawned != 2 {
|
|
t.Errorf("Spawned = %d, want 2", result.Spawned)
|
|
}
|
|
|
|
// Verify root ID follows pattern: patrol.arm-ace
|
|
expectedRootID := patrol.ID + ".arm-ace"
|
|
if result.IDMapping[protoRoot.ID] != expectedRootID {
|
|
t.Errorf("Root ID = %q, want %q", result.IDMapping[protoRoot.ID], expectedRootID)
|
|
}
|
|
|
|
// Verify child ID follows pattern: patrol.arm-ace.relative
|
|
// The child's ID should be patrol.arm-ace.capture (but relative part depends on proto structure)
|
|
childID := result.IDMapping[protoCapture.ID]
|
|
if !strings.HasPrefix(childID, expectedRootID+".") {
|
|
t.Errorf("Child ID %q should start with %q", childID, expectedRootID+".")
|
|
}
|
|
|
|
// Verify the spawned issues exist and have correct titles
|
|
spawnedRoot, err := s.GetIssue(ctx, expectedRootID)
|
|
if err != nil {
|
|
t.Fatalf("Failed to get spawned root: %v", err)
|
|
}
|
|
if !strings.Contains(spawnedRoot.Title, "ace") {
|
|
t.Errorf("Spawned root title %q should contain 'ace'", spawnedRoot.Title)
|
|
}
|
|
}
|
|
|
|
// TestBondProtoMolMultipleArms tests bonding multiple arms to the same parent
|
|
func TestBondProtoMolMultipleArms(t *testing.T) {
|
|
ctx := context.Background()
|
|
dbPath := t.TempDir() + "/test.db"
|
|
s := newTestStoreWithPrefix(t, dbPath, "patrol")
|
|
|
|
// Create simple proto
|
|
proto := &types.Issue{
|
|
Title: "Arm: {{name}}",
|
|
IssueType: types.TypeTask,
|
|
Status: types.StatusOpen,
|
|
Priority: 2,
|
|
Labels: []string{MoleculeLabel},
|
|
}
|
|
if err := s.CreateIssue(ctx, proto, "test"); err != nil {
|
|
t.Fatalf("Failed to create proto: %v", err)
|
|
}
|
|
|
|
// Create parent patrol
|
|
patrol := &types.Issue{
|
|
Title: "Patrol",
|
|
IssueType: types.TypeEpic,
|
|
Status: types.StatusOpen,
|
|
Priority: 1,
|
|
}
|
|
if err := s.CreateIssue(ctx, patrol, "test"); err != nil {
|
|
t.Fatalf("Failed to create patrol: %v", err)
|
|
}
|
|
|
|
// Bond arm-ace
|
|
varsAce := map[string]string{"name": "ace"}
|
|
resultAce, err := bondProtoMol(ctx, s, proto, patrol, types.BondTypeParallel, varsAce, "arm-{{name}}", "test", false, false)
|
|
if err != nil {
|
|
t.Fatalf("bondProtoMol (ace) failed: %v", err)
|
|
}
|
|
|
|
// Bond arm-nux
|
|
varsNux := map[string]string{"name": "nux"}
|
|
resultNux, err := bondProtoMol(ctx, s, proto, patrol, types.BondTypeParallel, varsNux, "arm-{{name}}", "test", false, false)
|
|
if err != nil {
|
|
t.Fatalf("bondProtoMol (nux) failed: %v", err)
|
|
}
|
|
|
|
// Verify IDs are correct and distinct
|
|
aceID := resultAce.IDMapping[proto.ID]
|
|
nuxID := resultNux.IDMapping[proto.ID]
|
|
|
|
expectedAceID := patrol.ID + ".arm-ace"
|
|
expectedNuxID := patrol.ID + ".arm-nux"
|
|
|
|
if aceID != expectedAceID {
|
|
t.Errorf("Ace ID = %q, want %q", aceID, expectedAceID)
|
|
}
|
|
if nuxID != expectedNuxID {
|
|
t.Errorf("Nux ID = %q, want %q", nuxID, expectedNuxID)
|
|
}
|
|
|
|
// Verify both exist
|
|
aceIssue, err := s.GetIssue(ctx, aceID)
|
|
if err != nil || aceIssue == nil {
|
|
t.Errorf("Ace issue not found: %v", err)
|
|
}
|
|
nuxIssue, err := s.GetIssue(ctx, nuxID)
|
|
if err != nil || nuxIssue == nil {
|
|
t.Errorf("Nux issue not found: %v", err)
|
|
}
|
|
}
|
|
|
|
// =============================================================================
|
|
// Parallel Detection Tests (bd-xo1o.4)
|
|
// =============================================================================
|
|
|
|
// TestAnalyzeMoleculeParallelNoBlocking tests parallel detection with no blocking deps
|
|
func TestAnalyzeMoleculeParallelNoBlocking(t *testing.T) {
|
|
// Create a simple molecule with parallel children (no blocking deps between them)
|
|
root := &types.Issue{
|
|
ID: "mol-test",
|
|
Title: "Test Molecule",
|
|
Status: types.StatusOpen,
|
|
IssueType: types.TypeEpic,
|
|
}
|
|
child1 := &types.Issue{
|
|
ID: "mol-test.step1",
|
|
Title: "Step 1",
|
|
Status: types.StatusOpen,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
child2 := &types.Issue{
|
|
ID: "mol-test.step2",
|
|
Title: "Step 2",
|
|
Status: types.StatusOpen,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
|
|
subgraph := &MoleculeSubgraph{
|
|
Root: root,
|
|
Issues: []*types.Issue{root, child1, child2},
|
|
IssueMap: map[string]*types.Issue{
|
|
root.ID: root,
|
|
child1.ID: child1,
|
|
child2.ID: child2,
|
|
},
|
|
Dependencies: []*types.Dependency{
|
|
{IssueID: child1.ID, DependsOnID: root.ID, Type: types.DepParentChild},
|
|
{IssueID: child2.ID, DependsOnID: root.ID, Type: types.DepParentChild},
|
|
},
|
|
}
|
|
|
|
analysis := analyzeMoleculeParallel(subgraph)
|
|
|
|
// All 3 should be ready (root + 2 children with no blocking deps)
|
|
if analysis.ReadySteps != 3 {
|
|
t.Errorf("ReadySteps = %d, want 3", analysis.ReadySteps)
|
|
}
|
|
|
|
// Children should be in the same parallel group
|
|
step1Info := analysis.Steps[child1.ID]
|
|
step2Info := analysis.Steps[child2.ID]
|
|
|
|
if step1Info.ParallelGroup == "" {
|
|
t.Error("Step1 should be in a parallel group")
|
|
}
|
|
if step1Info.ParallelGroup != step2Info.ParallelGroup {
|
|
t.Errorf("Step1 and Step2 should be in same parallel group: %s vs %s",
|
|
step1Info.ParallelGroup, step2Info.ParallelGroup)
|
|
}
|
|
|
|
// Check can_parallel
|
|
found := false
|
|
for _, id := range step1Info.CanParallel {
|
|
if id == child2.ID {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
t.Errorf("Step1.CanParallel should contain Step2.ID")
|
|
}
|
|
}
|
|
|
|
// TestAnalyzeMoleculeParallelWithBlocking tests parallel detection with blocking deps
|
|
func TestAnalyzeMoleculeParallelWithBlocking(t *testing.T) {
|
|
// Create a sequential molecule: step1 blocks step2
|
|
root := &types.Issue{
|
|
ID: "mol-seq",
|
|
Title: "Sequential Molecule",
|
|
Status: types.StatusOpen,
|
|
IssueType: types.TypeEpic,
|
|
}
|
|
step1 := &types.Issue{
|
|
ID: "mol-seq.step1",
|
|
Title: "Step 1",
|
|
Status: types.StatusOpen,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
step2 := &types.Issue{
|
|
ID: "mol-seq.step2",
|
|
Title: "Step 2 (blocked by Step 1)",
|
|
Status: types.StatusOpen,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
|
|
subgraph := &MoleculeSubgraph{
|
|
Root: root,
|
|
Issues: []*types.Issue{root, step1, step2},
|
|
IssueMap: map[string]*types.Issue{
|
|
root.ID: root,
|
|
step1.ID: step1,
|
|
step2.ID: step2,
|
|
},
|
|
Dependencies: []*types.Dependency{
|
|
{IssueID: step1.ID, DependsOnID: root.ID, Type: types.DepParentChild},
|
|
{IssueID: step2.ID, DependsOnID: root.ID, Type: types.DepParentChild},
|
|
{IssueID: step2.ID, DependsOnID: step1.ID, Type: types.DepBlocks}, // step2 blocked by step1
|
|
},
|
|
}
|
|
|
|
analysis := analyzeMoleculeParallel(subgraph)
|
|
|
|
// Only root and step1 should be ready (step2 is blocked)
|
|
if analysis.ReadySteps != 2 {
|
|
t.Errorf("ReadySteps = %d, want 2 (step2 blocked)", analysis.ReadySteps)
|
|
}
|
|
|
|
step1Info := analysis.Steps[step1.ID]
|
|
step2Info := analysis.Steps[step2.ID]
|
|
|
|
if !step1Info.IsReady {
|
|
t.Error("Step1 should be ready")
|
|
}
|
|
if step2Info.IsReady {
|
|
t.Error("Step2 should NOT be ready (blocked by step1)")
|
|
}
|
|
if len(step2Info.BlockedBy) != 1 || step2Info.BlockedBy[0] != step1.ID {
|
|
t.Errorf("Step2.BlockedBy = %v, want [%s]", step2Info.BlockedBy, step1.ID)
|
|
}
|
|
|
|
// Step1 and Step2 should NOT be in the same parallel group
|
|
if step1Info.ParallelGroup != "" && step1Info.ParallelGroup == step2Info.ParallelGroup {
|
|
t.Error("Blocking steps should NOT be in the same parallel group")
|
|
}
|
|
}
|
|
|
|
// TestAnalyzeMoleculeParallelCompletedBlockers tests that completed steps don't block
|
|
func TestAnalyzeMoleculeParallelCompletedBlockers(t *testing.T) {
|
|
// Create molecule where step1 is completed, so step2 should be ready
|
|
root := &types.Issue{
|
|
ID: "mol-done",
|
|
Title: "Molecule with completed step",
|
|
Status: types.StatusOpen,
|
|
IssueType: types.TypeEpic,
|
|
}
|
|
step1 := &types.Issue{
|
|
ID: "mol-done.step1",
|
|
Title: "Step 1 (completed)",
|
|
Status: types.StatusClosed, // Completed!
|
|
IssueType: types.TypeTask,
|
|
}
|
|
step2 := &types.Issue{
|
|
ID: "mol-done.step2",
|
|
Title: "Step 2 (depends on step1)",
|
|
Status: types.StatusOpen,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
|
|
subgraph := &MoleculeSubgraph{
|
|
Root: root,
|
|
Issues: []*types.Issue{root, step1, step2},
|
|
IssueMap: map[string]*types.Issue{
|
|
root.ID: root,
|
|
step1.ID: step1,
|
|
step2.ID: step2,
|
|
},
|
|
Dependencies: []*types.Dependency{
|
|
{IssueID: step1.ID, DependsOnID: root.ID, Type: types.DepParentChild},
|
|
{IssueID: step2.ID, DependsOnID: root.ID, Type: types.DepParentChild},
|
|
{IssueID: step2.ID, DependsOnID: step1.ID, Type: types.DepBlocks},
|
|
},
|
|
}
|
|
|
|
analysis := analyzeMoleculeParallel(subgraph)
|
|
|
|
step2Info := analysis.Steps[step2.ID]
|
|
|
|
// Step2 should be ready since step1 is closed
|
|
if !step2Info.IsReady {
|
|
t.Error("Step2 should be ready (step1 is completed)")
|
|
}
|
|
if len(step2Info.BlockedBy) != 0 {
|
|
t.Errorf("Step2.BlockedBy = %v, want empty (step1 completed)", step2Info.BlockedBy)
|
|
}
|
|
}
|
|
|
|
func TestAnalyzeMoleculeParallelWaitsForChildrenOfSpawner(t *testing.T) {
|
|
root := &types.Issue{
|
|
ID: "mol-fanout",
|
|
Title: "Fanout Molecule",
|
|
Status: types.StatusOpen,
|
|
IssueType: types.TypeEpic,
|
|
}
|
|
implement := &types.Issue{
|
|
ID: "mol-fanout.implement",
|
|
Title: "Implement",
|
|
Status: types.StatusOpen,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
otherSpawner := &types.Issue{
|
|
ID: "mol-fanout.other",
|
|
Title: "Other spawner",
|
|
Status: types.StatusOpen,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
review := &types.Issue{
|
|
ID: "mol-fanout.review",
|
|
Title: "Review",
|
|
Status: types.StatusOpen,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
implChild := &types.Issue{
|
|
ID: "mol-fanout.implement.arm-1",
|
|
Title: "Implement child",
|
|
Status: types.StatusOpen,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
otherChild := &types.Issue{
|
|
ID: "mol-fanout.other.arm-1",
|
|
Title: "Other child",
|
|
Status: types.StatusOpen,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
|
|
subgraph := &MoleculeSubgraph{
|
|
Root: root,
|
|
Issues: []*types.Issue{root, implement, otherSpawner, review, implChild, otherChild},
|
|
IssueMap: map[string]*types.Issue{
|
|
root.ID: root,
|
|
implement.ID: implement,
|
|
otherSpawner.ID: otherSpawner,
|
|
review.ID: review,
|
|
implChild.ID: implChild,
|
|
otherChild.ID: otherChild,
|
|
},
|
|
Dependencies: []*types.Dependency{
|
|
{IssueID: implement.ID, DependsOnID: root.ID, Type: types.DepParentChild},
|
|
{IssueID: otherSpawner.ID, DependsOnID: root.ID, Type: types.DepParentChild},
|
|
{IssueID: review.ID, DependsOnID: root.ID, Type: types.DepParentChild},
|
|
{IssueID: implChild.ID, DependsOnID: implement.ID, Type: types.DepParentChild},
|
|
{IssueID: otherChild.ID, DependsOnID: otherSpawner.ID, Type: types.DepParentChild},
|
|
{
|
|
IssueID: review.ID,
|
|
DependsOnID: implement.ID,
|
|
Type: types.DepWaitsFor,
|
|
Metadata: `{"gate":"all-children"}`,
|
|
},
|
|
},
|
|
}
|
|
|
|
t.Run("blocked-before-child-close", func(t *testing.T) {
|
|
analysis := analyzeMoleculeParallel(subgraph)
|
|
reviewInfo := analysis.Steps[review.ID]
|
|
if reviewInfo.IsReady {
|
|
t.Fatalf("review should be blocked while %s is open", implChild.ID)
|
|
}
|
|
|
|
hasImplChildBlocker := false
|
|
for _, blocker := range reviewInfo.BlockedBy {
|
|
if blocker == implChild.ID {
|
|
hasImplChildBlocker = true
|
|
}
|
|
if blocker == otherChild.ID {
|
|
t.Fatalf("review should not be blocked by unrelated child %s", otherChild.ID)
|
|
}
|
|
}
|
|
if !hasImplChildBlocker {
|
|
t.Fatalf("expected review to be blocked by child of implement spawner")
|
|
}
|
|
})
|
|
|
|
t.Run("ready-after-child-close", func(t *testing.T) {
|
|
implChild.Status = types.StatusClosed
|
|
analysisAfterClose := analyzeMoleculeParallel(subgraph)
|
|
if !analysisAfterClose.Steps[review.ID].IsReady {
|
|
t.Fatalf("review should become ready after %s closes", implChild.ID)
|
|
}
|
|
})
|
|
}
|
|
|
|
// TestAnalyzeMoleculeParallelMultipleArms tests parallel detection across bonded arms
|
|
func TestAnalyzeMoleculeParallelMultipleArms(t *testing.T) {
|
|
// Create molecule with two arms that can run in parallel
|
|
root := &types.Issue{
|
|
ID: "patrol",
|
|
Title: "Patrol",
|
|
Status: types.StatusOpen,
|
|
IssueType: types.TypeEpic,
|
|
}
|
|
armAce := &types.Issue{
|
|
ID: "patrol.arm-ace",
|
|
Title: "Arm: ace",
|
|
Status: types.StatusOpen,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
armNux := &types.Issue{
|
|
ID: "patrol.arm-nux",
|
|
Title: "Arm: nux",
|
|
Status: types.StatusOpen,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
|
|
subgraph := &MoleculeSubgraph{
|
|
Root: root,
|
|
Issues: []*types.Issue{root, armAce, armNux},
|
|
IssueMap: map[string]*types.Issue{
|
|
root.ID: root,
|
|
armAce.ID: armAce,
|
|
armNux.ID: armNux,
|
|
},
|
|
Dependencies: []*types.Dependency{
|
|
{IssueID: armAce.ID, DependsOnID: root.ID, Type: types.DepParentChild},
|
|
{IssueID: armNux.ID, DependsOnID: root.ID, Type: types.DepParentChild},
|
|
// No blocking deps between arms
|
|
},
|
|
}
|
|
|
|
analysis := analyzeMoleculeParallel(subgraph)
|
|
|
|
// All 3 should be ready
|
|
if analysis.ReadySteps != 3 {
|
|
t.Errorf("ReadySteps = %d, want 3", analysis.ReadySteps)
|
|
}
|
|
|
|
// Arms should be in the same parallel group
|
|
aceInfo := analysis.Steps[armAce.ID]
|
|
nuxInfo := analysis.Steps[armNux.ID]
|
|
|
|
if aceInfo.ParallelGroup == "" {
|
|
t.Error("arm-ace should be in a parallel group")
|
|
}
|
|
if aceInfo.ParallelGroup != nuxInfo.ParallelGroup {
|
|
t.Errorf("Arms should be in same parallel group: %s vs %s",
|
|
aceInfo.ParallelGroup, nuxInfo.ParallelGroup)
|
|
}
|
|
|
|
// Should have at least one parallel group with both arms
|
|
foundGroup := false
|
|
for _, members := range analysis.ParallelGroups {
|
|
hasAce := false
|
|
hasNux := false
|
|
for _, id := range members {
|
|
if id == armAce.ID {
|
|
hasAce = true
|
|
}
|
|
if id == armNux.ID {
|
|
hasNux = true
|
|
}
|
|
}
|
|
if hasAce && hasNux {
|
|
foundGroup = true
|
|
break
|
|
}
|
|
}
|
|
if !foundGroup {
|
|
t.Error("Should have a parallel group containing both arms")
|
|
}
|
|
}
|
|
|
|
// TestSpawnMoleculeEphemeralFlag verifies that spawnMolecule with ephemeral=true
|
|
// creates issues with the Ephemeral flag set (bd-phin)
|
|
func TestSpawnMoleculeEphemeralFlag(t *testing.T) {
|
|
ctx := context.Background()
|
|
dbPath := t.TempDir() + "/test.db"
|
|
s := newTestStore(t, dbPath)
|
|
|
|
// Create a template with a child (IDs will be auto-generated)
|
|
root := &types.Issue{
|
|
Title: "Template Epic",
|
|
Status: types.StatusOpen,
|
|
Priority: 2,
|
|
IssueType: types.TypeEpic,
|
|
Labels: []string{MoleculeLabel}, // Required for loadTemplateSubgraph
|
|
}
|
|
child := &types.Issue{
|
|
Title: "Template Task",
|
|
Status: types.StatusOpen,
|
|
Priority: 2,
|
|
IssueType: types.TypeTask,
|
|
}
|
|
|
|
if err := s.CreateIssue(ctx, root, "test"); err != nil {
|
|
t.Fatalf("Failed to create template root: %v", err)
|
|
}
|
|
if err := s.CreateIssue(ctx, child, "test"); err != nil {
|
|
t.Fatalf("Failed to create template child: %v", err)
|
|
}
|
|
|
|
// Add parent-child dependency
|
|
if err := s.AddDependency(ctx, &types.Dependency{
|
|
IssueID: child.ID,
|
|
DependsOnID: root.ID,
|
|
Type: types.DepParentChild,
|
|
}, "test"); err != nil {
|
|
t.Fatalf("Failed to add parent-child dependency: %v", err)
|
|
}
|
|
|
|
// Load subgraph
|
|
subgraph, err := loadTemplateSubgraph(ctx, s, root.ID)
|
|
if err != nil {
|
|
t.Fatalf("Failed to load subgraph: %v", err)
|
|
}
|
|
|
|
// Spawn with ephemeral=true
|
|
result, err := spawnMolecule(ctx, s, subgraph, nil, "", "test", true, "wisp")
|
|
if err != nil {
|
|
t.Fatalf("spawnMolecule failed: %v", err)
|
|
}
|
|
|
|
// Verify all spawned issues have Ephemeral=true
|
|
for oldID, newID := range result.IDMapping {
|
|
spawned, err := s.GetIssue(ctx, newID)
|
|
if err != nil {
|
|
t.Fatalf("Failed to get spawned issue %s: %v", newID, err)
|
|
}
|
|
if !spawned.Ephemeral {
|
|
t.Errorf("Spawned issue %s (from %s) should have Ephemeral=true, got false", newID, oldID)
|
|
}
|
|
}
|
|
|
|
// Verify spawned issues have the correct prefix
|
|
for _, newID := range result.IDMapping {
|
|
if !strings.HasPrefix(newID, "test-wisp-") {
|
|
t.Errorf("Spawned issue ID %s should have prefix 'test-wisp-'", newID)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestSpawnMoleculeFromFormulaEphemeral verifies that spawning from a cooked formula
|
|
// with ephemeral=true creates issues with the Ephemeral flag set (bd-phin)
|
|
func TestSpawnMoleculeFromFormulaEphemeral(t *testing.T) {
|
|
ctx := context.Background()
|
|
dbPath := t.TempDir() + "/test.db"
|
|
s := newTestStore(t, dbPath)
|
|
|
|
// Create a minimal in-memory subgraph (simulating cookFormulaToSubgraph output)
|
|
root := &types.Issue{
|
|
ID: "test-formula",
|
|
Title: "Test Formula",
|
|
Status: types.StatusOpen,
|
|
Priority: 2,
|
|
IssueType: types.TypeEpic,
|
|
IsTemplate: true,
|
|
}
|
|
step := &types.Issue{
|
|
ID: "test-formula.step1",
|
|
Title: "Step 1",
|
|
Status: types.StatusOpen,
|
|
Priority: 2,
|
|
IssueType: types.TypeTask,
|
|
IsTemplate: true,
|
|
}
|
|
|
|
subgraph := &TemplateSubgraph{
|
|
Root: root,
|
|
Issues: []*types.Issue{root, step},
|
|
Dependencies: []*types.Dependency{
|
|
{
|
|
IssueID: step.ID,
|
|
DependsOnID: root.ID,
|
|
Type: types.DepParentChild,
|
|
},
|
|
},
|
|
IssueMap: map[string]*types.Issue{
|
|
root.ID: root,
|
|
step.ID: step,
|
|
},
|
|
}
|
|
|
|
// Spawn with ephemeral=true (simulating bd mol wisp <formula>)
|
|
result, err := spawnMolecule(ctx, s, subgraph, nil, "", "test", true, "wisp")
|
|
if err != nil {
|
|
t.Fatalf("spawnMolecule failed: %v", err)
|
|
}
|
|
|
|
// Verify all spawned issues have Ephemeral=true
|
|
for oldID, newID := range result.IDMapping {
|
|
spawned, err := s.GetIssue(ctx, newID)
|
|
if err != nil {
|
|
t.Fatalf("Failed to get spawned issue %s: %v", newID, err)
|
|
}
|
|
if !spawned.Ephemeral {
|
|
t.Errorf("Spawned issue %s (from %s) should have Ephemeral=true, got false", newID, oldID)
|
|
}
|
|
t.Logf("Issue %s: Ephemeral=%v", newID, spawned.Ephemeral)
|
|
}
|
|
|
|
// Verify they have the correct prefix
|
|
for _, newID := range result.IDMapping {
|
|
if !strings.HasPrefix(newID, "test-wisp-") {
|
|
t.Errorf("Spawned issue ID %s should have prefix 'test-wisp-'", newID)
|
|
}
|
|
}
|
|
|
|
// Verify ephemeral issues are excluded from ready work
|
|
readyWork, err := s.GetReadyWork(ctx, types.WorkFilter{})
|
|
if err != nil {
|
|
t.Fatalf("GetReadyWork failed: %v", err)
|
|
}
|
|
for _, issue := range readyWork {
|
|
for _, spawnedID := range result.IDMapping {
|
|
if issue.ID == spawnedID {
|
|
t.Errorf("Ephemeral issue %s should not appear in ready work", spawnedID)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestSpawnMolecule_PreservesStepMetadata verifies that a cooked formula with
|
|
// per-step metadata results in spawned issues whose Metadata field carries the
|
|
// declared keys through to the database. Regression for gastownhall/beads#3341.
|
|
func TestSpawnMolecule_PreservesStepMetadata(t *testing.T) {
|
|
ctx := context.Background()
|
|
dbPath := t.TempDir() + "/test.db"
|
|
s := newTestStore(t, dbPath)
|
|
|
|
f := &formula.Formula{
|
|
Formula: "meta-test",
|
|
Version: 1,
|
|
Type: formula.TypeWorkflow,
|
|
Steps: []*formula.Step{
|
|
{
|
|
ID: "work",
|
|
Title: "Do the work",
|
|
Metadata: map[string]interface{}{
|
|
"reminder_list": "brewery",
|
|
"origin": "repro",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
subgraph, err := cookFormulaToSubgraph(f, "meta-test")
|
|
if err != nil {
|
|
t.Fatalf("cookFormulaToSubgraph failed: %v", err)
|
|
}
|
|
|
|
result, err := spawnMolecule(ctx, s, subgraph, nil, "", "test", false, types.IDPrefixMol)
|
|
if err != nil {
|
|
t.Fatalf("spawnMolecule failed: %v", err)
|
|
}
|
|
|
|
newWorkID, ok := result.IDMapping["meta-test.work"]
|
|
if !ok {
|
|
t.Fatalf("result.IDMapping missing entry for meta-test.work; got %v", result.IDMapping)
|
|
}
|
|
spawned, err := s.GetIssue(ctx, newWorkID)
|
|
if err != nil {
|
|
t.Fatalf("GetIssue(%s) failed: %v", newWorkID, err)
|
|
}
|
|
if len(spawned.Metadata) == 0 {
|
|
t.Fatalf("spawned issue %s has empty Metadata; want step metadata carried through", newWorkID)
|
|
}
|
|
var decoded map[string]interface{}
|
|
if err := json.Unmarshal(spawned.Metadata, &decoded); err != nil {
|
|
t.Fatalf("spawned.Metadata is not valid JSON: %v (raw: %s)", err, string(spawned.Metadata))
|
|
}
|
|
if got := decoded["reminder_list"]; got != "brewery" {
|
|
t.Errorf("spawned.Metadata[reminder_list] = %v, want \"brewery\"", got)
|
|
}
|
|
if got := decoded["origin"]; got != "repro" {
|
|
t.Errorf("spawned.Metadata[origin] = %v, want \"repro\"", got)
|
|
}
|
|
}
|
|
|
|
// TestCompoundMoleculeVisualization tests the compound molecule display in mol show
|
|
func TestCompoundMoleculeVisualization(t *testing.T) {
|
|
// Test IsCompound() and GetConstituents()
|
|
tests := []struct {
|
|
name string
|
|
bondedFrom []types.BondRef
|
|
isCompound bool
|
|
expectedCount int
|
|
}{
|
|
{
|
|
name: "not a compound - no BondedFrom",
|
|
bondedFrom: nil,
|
|
isCompound: false,
|
|
expectedCount: 0,
|
|
},
|
|
{
|
|
name: "not a compound - empty BondedFrom",
|
|
bondedFrom: []types.BondRef{},
|
|
isCompound: false,
|
|
expectedCount: 0,
|
|
},
|
|
{
|
|
name: "compound with one constituent",
|
|
bondedFrom: []types.BondRef{
|
|
{SourceID: "proto-a", BondType: types.BondTypeSequential},
|
|
},
|
|
isCompound: true,
|
|
expectedCount: 1,
|
|
},
|
|
{
|
|
name: "compound with two constituents - sequential bond",
|
|
bondedFrom: []types.BondRef{
|
|
{SourceID: "proto-a", BondType: types.BondTypeSequential},
|
|
{SourceID: "proto-b", BondType: types.BondTypeSequential},
|
|
},
|
|
isCompound: true,
|
|
expectedCount: 2,
|
|
},
|
|
{
|
|
name: "compound with parallel bond",
|
|
bondedFrom: []types.BondRef{
|
|
{SourceID: "proto-a", BondType: types.BondTypeParallel},
|
|
{SourceID: "proto-b", BondType: types.BondTypeParallel},
|
|
},
|
|
isCompound: true,
|
|
expectedCount: 2,
|
|
},
|
|
{
|
|
name: "compound with bond point",
|
|
bondedFrom: []types.BondRef{
|
|
{SourceID: "proto-a", BondType: types.BondTypeSequential, BondPoint: "step-2"},
|
|
},
|
|
isCompound: true,
|
|
expectedCount: 1,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
issue := &types.Issue{
|
|
ID: "test-compound",
|
|
Title: "Test Compound Molecule",
|
|
BondedFrom: tt.bondedFrom,
|
|
}
|
|
|
|
if got := issue.IsCompound(); got != tt.isCompound {
|
|
t.Errorf("IsCompound() = %v, want %v", got, tt.isCompound)
|
|
}
|
|
|
|
constituents := issue.GetConstituents()
|
|
if len(constituents) != tt.expectedCount {
|
|
t.Errorf("GetConstituents() returned %d items, want %d", len(constituents), tt.expectedCount)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestFormatBondType tests the formatBondType helper function
|
|
func TestFormatBondType(t *testing.T) {
|
|
tests := []struct {
|
|
bondType string
|
|
expected string
|
|
}{
|
|
{types.BondTypeSequential, "sequential"},
|
|
{types.BondTypeParallel, "parallel"},
|
|
{types.BondTypeConditional, "on-failure"},
|
|
{types.BondTypeRoot, "root"},
|
|
{"", "default"},
|
|
{"custom-type", "custom-type"},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.bondType, func(t *testing.T) {
|
|
if got := formatBondType(tt.bondType); got != tt.expected {
|
|
t.Errorf("formatBondType(%q) = %q, want %q", tt.bondType, got, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestPourRootTitleDescSubstitution verifies that the root molecule's title and description
|
|
// are substituted with {{title}} and {{desc}} variables when pouring a formula.
|
|
// This is a tracer bullet test for GitHub issue #852:
|
|
// https://github.com/steveyegge/beads/issues/852
|
|
func TestPourRootTitleDescSubstitution(t *testing.T) {
|
|
ctx := context.Background()
|
|
dbPath := t.TempDir() + "/test.db"
|
|
s := newTestStoreWithPrefix(t, dbPath, "mol")
|
|
|
|
// Create a formula that has title and desc variables
|
|
f := &formula.Formula{
|
|
Formula: "mol-task",
|
|
Description: "Standard task workflow for 2-8 hour work...",
|
|
Version: 1,
|
|
Type: formula.TypeWorkflow,
|
|
Vars: map[string]*formula.VarDef{
|
|
"title": {
|
|
Description: "Task title",
|
|
Required: true,
|
|
},
|
|
"desc": {
|
|
Description: "Task description",
|
|
Required: false,
|
|
Default: formula.StringPtr("No description provided"),
|
|
},
|
|
},
|
|
Steps: []*formula.Step{
|
|
{ID: "plan", Title: "Plan: {{title}}", Type: "task"},
|
|
{ID: "implement", Title: "Implement: {{title}}", Type: "task", DependsOn: []string{"plan"}},
|
|
{ID: "verify", Title: "Verify: {{title}}", Type: "task", DependsOn: []string{"implement"}},
|
|
{ID: "review", Title: "Review: {{title}}", Type: "task", DependsOn: []string{"verify"}},
|
|
},
|
|
}
|
|
|
|
// Cook the formula to a subgraph (in-memory, no DB)
|
|
subgraph, err := cookFormulaToSubgraphWithVars(f, f.Formula, f.Vars)
|
|
if err != nil {
|
|
t.Fatalf("Failed to cook formula: %v", err)
|
|
}
|
|
|
|
// Spawn with title and desc variables
|
|
vars := map[string]string{
|
|
"title": "My Task",
|
|
"desc": "My description",
|
|
}
|
|
|
|
result, err := spawnMolecule(ctx, s, subgraph, vars, "", "test", false, types.IDPrefixMol)
|
|
if err != nil {
|
|
t.Fatalf("spawnMolecule failed: %v", err)
|
|
}
|
|
|
|
// Get the spawned root issue
|
|
spawnedRoot, err := s.GetIssue(ctx, result.NewEpicID)
|
|
if err != nil {
|
|
t.Fatalf("Failed to get spawned root: %v", err)
|
|
}
|
|
|
|
// BUG: The root title should contain "My Task" but currently contains "mol-task"
|
|
// because cookFormulaToSubgraph sets root.Title = f.Formula instead of using
|
|
// a template that includes {{title}}.
|
|
if !strings.Contains(spawnedRoot.Title, "My Task") {
|
|
t.Errorf("Root title should contain 'My Task' from variable substitution, got: %q", spawnedRoot.Title)
|
|
}
|
|
|
|
// BUG: The root description should contain "My description" but currently
|
|
// contains the formula's static description.
|
|
if !strings.Contains(spawnedRoot.Description, "My description") {
|
|
t.Errorf("Root description should contain 'My description' from variable substitution, got: %q", spawnedRoot.Description)
|
|
}
|
|
|
|
// Verify child beads DO have correct substitution (this should pass)
|
|
for oldID, newID := range result.IDMapping {
|
|
if oldID == f.Formula {
|
|
continue // Skip root
|
|
}
|
|
spawned, err := s.GetIssue(ctx, newID)
|
|
if err != nil {
|
|
t.Fatalf("Failed to get spawned issue %s: %v", newID, err)
|
|
}
|
|
if !strings.Contains(spawned.Title, "My Task") {
|
|
t.Errorf("Child issue %s (from %s) title should contain 'My Task', got: %q", newID, oldID, spawned.Title)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestPourRootTitleOnly verifies edge case: only title var defined, no desc.
|
|
// Root should use {{title}} for title, but keep formula description.
|
|
func TestPourRootTitleOnly(t *testing.T) {
|
|
ctx := context.Background()
|
|
dbPath := t.TempDir() + "/test.db"
|
|
s := newTestStoreWithPrefix(t, dbPath, "mol")
|
|
|
|
// Formula with only title var (no desc)
|
|
f := &formula.Formula{
|
|
Formula: "mol-simple",
|
|
Description: "Static description that should be preserved",
|
|
Version: 1,
|
|
Type: formula.TypeWorkflow,
|
|
Vars: map[string]*formula.VarDef{
|
|
"title": {Description: "Task title", Required: true},
|
|
},
|
|
Steps: []*formula.Step{
|
|
{ID: "work", Title: "Do: {{title}}", Type: "task"},
|
|
},
|
|
}
|
|
|
|
subgraph, err := cookFormulaToSubgraphWithVars(f, f.Formula, f.Vars)
|
|
if err != nil {
|
|
t.Fatalf("Failed to cook formula: %v", err)
|
|
}
|
|
|
|
vars := map[string]string{"title": "Custom Title"}
|
|
result, err := spawnMolecule(ctx, s, subgraph, vars, "", "test", false, types.IDPrefixMol)
|
|
if err != nil {
|
|
t.Fatalf("spawnMolecule failed: %v", err)
|
|
}
|
|
|
|
spawnedRoot, err := s.GetIssue(ctx, result.NewEpicID)
|
|
if err != nil {
|
|
t.Fatalf("Failed to get spawned root: %v", err)
|
|
}
|
|
|
|
// Title should be substituted
|
|
if !strings.Contains(spawnedRoot.Title, "Custom Title") {
|
|
t.Errorf("Root title should contain 'Custom Title', got: %q", spawnedRoot.Title)
|
|
}
|
|
|
|
// Description should be the static formula description (no desc var)
|
|
if spawnedRoot.Description != "Static description that should be preserved" {
|
|
t.Errorf("Root description should be static formula desc, got: %q", spawnedRoot.Description)
|
|
}
|
|
}
|
|
|
|
// TestPourRootNoVars verifies backward compatibility: no title/desc vars defined.
|
|
// Root should use formula name and formula description (original behavior).
|
|
func TestPourRootNoVars(t *testing.T) {
|
|
ctx := context.Background()
|
|
dbPath := t.TempDir() + "/test.db"
|
|
s := newTestStoreWithPrefix(t, dbPath, "mol")
|
|
|
|
// Formula with no title/desc vars (uses different var names)
|
|
f := &formula.Formula{
|
|
Formula: "mol-release",
|
|
Description: "Release workflow for version bumps",
|
|
Version: 1,
|
|
Type: formula.TypeWorkflow,
|
|
Vars: map[string]*formula.VarDef{
|
|
"version": {Description: "Version number", Required: true},
|
|
},
|
|
Steps: []*formula.Step{
|
|
{ID: "bump", Title: "Bump to {{version}}", Type: "task"},
|
|
{ID: "tag", Title: "Tag {{version}}", Type: "task", DependsOn: []string{"bump"}},
|
|
},
|
|
}
|
|
|
|
subgraph, err := cookFormulaToSubgraphWithVars(f, f.Formula, f.Vars)
|
|
if err != nil {
|
|
t.Fatalf("Failed to cook formula: %v", err)
|
|
}
|
|
|
|
vars := map[string]string{"version": "1.2.3"}
|
|
result, err := spawnMolecule(ctx, s, subgraph, vars, "", "test", false, types.IDPrefixMol)
|
|
if err != nil {
|
|
t.Fatalf("spawnMolecule failed: %v", err)
|
|
}
|
|
|
|
spawnedRoot, err := s.GetIssue(ctx, result.NewEpicID)
|
|
if err != nil {
|
|
t.Fatalf("Failed to get spawned root: %v", err)
|
|
}
|
|
|
|
// Title should be formula name (no title var defined)
|
|
if spawnedRoot.Title != "mol-release" {
|
|
t.Errorf("Root title should be formula name 'mol-release', got: %q", spawnedRoot.Title)
|
|
}
|
|
|
|
// Description should be formula description (no desc var defined)
|
|
if spawnedRoot.Description != "Release workflow for version bumps" {
|
|
t.Errorf("Root description should be formula desc, got: %q", spawnedRoot.Description)
|
|
}
|
|
}
|
|
|
|
// The molecule tests below drive the library's molecule rules through the
|
|
// store's own roles — the path every route takes — via these thin shims.
|
|
|
|
func testMolRoles(t *testing.T, s storage.DoltStorage) moleculeRoles {
|
|
t.Helper()
|
|
roles, err := directMoleculeRoles(s)
|
|
if err != nil {
|
|
t.Fatalf("directMoleculeRoles: %v", err)
|
|
}
|
|
return roles
|
|
}
|
|
|
|
func mustFindParentMolecule(t *testing.T, ctx context.Context, s storage.DoltStorage, id string) string {
|
|
t.Helper()
|
|
got, err := issueops.MoleculeRoot(ctx, testMolRoles(t, s).molecule, id)
|
|
if err != nil {
|
|
t.Fatalf("MoleculeRoot(%s): %v", id, err)
|
|
}
|
|
return got
|
|
}
|
|
|
|
func moleculeRootsOf(ctx context.Context, s storage.DoltStorage, ids []string) (map[string]string, error) {
|
|
roles, err := directMoleculeRoles(s)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return issueops.MoleculeRoots(ctx, roles.molecule, ids)
|
|
}
|
|
|
|
func viewMoleculeProgress(ctx context.Context, s storage.DoltStorage, id string) (*MoleculeProgress, error) {
|
|
roles, err := directMoleculeRoles(s)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
view, err := issueops.ViewMolecule(ctx, roles.molecule, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return moleculeProgressOf(view), nil
|
|
}
|
|
|
|
func mustFindHookedMolecules(t *testing.T, ctx context.Context, s storage.DoltStorage, agent string) []*MoleculeProgress {
|
|
t.Helper()
|
|
roles := testMolRoles(t, s)
|
|
ids, err := issueops.ActiveMoleculeIDs(ctx, roles.reader, roles.molecule, agent)
|
|
if err != nil {
|
|
t.Fatalf("ActiveMoleculeIDs(%q): %v", agent, err)
|
|
}
|
|
got, err := viewMolecules(ctx, roles, ids)
|
|
if err != nil {
|
|
t.Fatalf("viewMolecules(%q): %v", agent, err)
|
|
}
|
|
return got
|
|
}
|
|
|
|
func advanceForTest(ctx context.Context, s storage.DoltStorage, closedStepID string, autoClaim bool, actor string) (*ContinueResult, error) {
|
|
stepper, err := s.MoleculeStepper()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result, err := stepper.Advance(ctx, issueops.AdvanceRequest{Actor: actor, ClosedStepID: closedStepID, AutoClaim: autoClaim})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return continueResultOf(result), nil
|
|
}
|