| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "slices" |
| 7 | "testing" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/runner/forge" |
| 10 | "amadan.net/oficina/equipo/internal/runner/workspace" |
| 11 | "amadan.net/oficina/equipo/internal/wire" |
| 12 | ) |
| 13 | |
| 14 | type countingForge struct { |
| 15 | repos []string |
| 16 | creates int |
| 17 | } |
| 18 | |
| 19 | func (f *countingForge) Repos() []string { return slices.Clone(f.repos) } |
| 20 | func (f *countingForge) Reachable(context.Context) []string { return slices.Clone(f.repos) } |
| 21 | func (f *countingForge) CreateIn(context.Context) []string { return []string{"oficina"} } |
| 22 | func (f *countingForge) Agent() string { return "test" } |
| 23 | func (f *countingForge) Remote(context.Context, string, Use) (workspace.Remote, error) { |
| 24 | return workspace.Remote{}, workspace.ErrGitPassword |
| 25 | } |
| 26 | func (f *countingForge) Recheck() {} |
| 27 | func (f *countingForge) Merge(context.Context, string, string, forge.MergeRequest) (forge.MergeResult, error) { |
| 28 | return forge.MergeResult{}, forge.ErrMergeRefused |
| 29 | } |
| 30 | |
| 31 | func (f *countingForge) Exists(_ context.Context, repo string) (bool, error) { |
| 32 | return slices.Contains(f.repos, repo), nil |
| 33 | } |
| 34 | func (f *countingForge) Create(_ context.Context, repo string) error { |
| 35 | f.creates++ |
| 36 | f.repos = append(f.repos, repo) |
| 37 | return nil |
| 38 | } |
| 39 | |
| 40 | // Review fix 2: a repeated create_project (a resent call after a lost |
| 41 | // answer or a restart) never creates twice, and a repository that already |
| 42 | // exists (the first create's answer was lost) is adopted, not recreated. |
| 43 | func TestCreateProjectIsIdempotent(t *testing.T) { |
| 44 | state, err := OpenState(context.Background(), t.TempDir()) |
| 45 | if err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | defer state.Close() |
| 49 | f := &countingForge{} |
| 50 | var created []string |
| 51 | b := &bridge{State: state, Forge: f, speaks: wire.ProtocolCurrent, trip: func(_ context.Context, c wire.ToolCall) wire.ToolResult { |
| 52 | var in struct{ Repo string } |
| 53 | _ = json.Unmarshal(c.Args, &in) |
| 54 | if c.Name == wire.ToolCreateProject { |
| 55 | return wire.ToolResult{Code: "create", Repo: in.Repo} |
| 56 | } |
| 57 | created = append(created, in.Repo) |
| 58 | return wire.ToolResult{Code: "created", Repo: in.Repo, Text: "Created."} |
| 59 | }} |
| 60 | args := json.RawMessage(`{"namespace":"oficina","name":"site"}`) |
| 61 | for range 2 { |
| 62 | if text, isErr := b.Call(context.Background(), "p", "t1", 1, wire.ToolCreateProject, "toolu_1", args); isErr { |
| 63 | t.Fatalf("create: %s", text) |
| 64 | } |
| 65 | } |
| 66 | if f.creates != 1 || len(created) != 2 { |
| 67 | t.Fatalf("creates %d, confirmations %v", f.creates, created) |
| 68 | } |
| 69 | // The repository exists already (an earlier answer was lost): adopted. |
| 70 | f.repos = append(f.repos, "amadan.net/oficina/docs-site") |
| 71 | if text, isErr := b.Call(context.Background(), "p", "t2", 1, wire.ToolCreateProject, "toolu_2", json.RawMessage(`{"namespace":"oficina","name":"docs-site"}`)); isErr { |
| 72 | t.Fatalf("adopt: %s", text) |
| 73 | } |
| 74 | if f.creates != 1 { |
| 75 | t.Fatalf("an existing repository was created again: %d creates", f.creates) |
| 76 | } |
| 77 | } |
| 78 | |