| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/wire" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | func TestPlacementPromptAndTools(t *testing.T) { |
| 14 | text := opening("person@example.com", nil) |
| 15 | for _, bad := range []string{"Never ask them to choose a repository", "If it is ambiguous, take the best fit", "Never ask which repository", "so do not repeat it"} { |
| 16 | if strings.Contains(text, bad) { |
| 17 | t.Fatal("old rule", bad) |
| 18 | } |
| 19 | } |
| 20 | for _, want := range []string{"Use the named project or the clear continuation", "Always confirm a new repository", "Wait for the answer before recording or starting work", "project corrections in your notebook", "tool's agent name and sharing hint"} { |
| 21 | if !strings.Contains(text, want) { |
| 22 | t.Fatal("missing placement rule", want) |
| 23 | } |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | func TestPlacementNotesSurviveRunnerRestart(t *testing.T) { |
| 28 | ctx := context.Background() |
| 29 | dir := t.TempDir() |
| 30 | s, err := OpenState(ctx, dir) |
| 31 | if err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | defer func() { _ = s.Close() }() |
| 35 | m := wire.MessageCommand{Person: "p", TurnID: "turn-place", Attempt: 1, Kind: wire.TurnPerson, Text: "Hello", PlacementNotes: []wire.NotebookEntry{{ID: "mv1", Source: wire.NoteOwn, Text: "The calendar belongs in apps/calendar."}}} |
| 36 | if err := s.Accept(ctx, "f-place", m); err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | if err := s.Close(); err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | s, err = OpenState(ctx, dir) |
| 43 | if err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | got, ok, err := s.next(ctx, "p", "") |
| 47 | if err != nil || !ok || len(got.PlacementNotes) != 1 || got.PlacementNotes[0].Text != m.PlacementNotes[0].Text { |
| 48 | t.Fatalf("lost durable correction: %+v %v", got, err) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func TestPlacementOldInstanceCannotCreateRepository(t *testing.T) { |
| 53 | state, err := OpenState(context.Background(), t.TempDir()) |
| 54 | if err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | defer state.Close() |
| 58 | forge := &countingForge{} |
| 59 | b := &bridge{State: state, Forge: forge, speaks: wire.Protocol24, trip: func(_ context.Context, c wire.ToolCall) wire.ToolResult { |
| 60 | return wire.ToolResult{Code: "create", Repo: "amadan.net/oficina/site"} |
| 61 | }} |
| 62 | if _, failed := b.Call(context.Background(), "p", "t", 1, wire.ToolCreateProject, "create", json.RawMessage(`{"namespace":"oficina","name":"site"}`)); !failed || forge.creates != 0 { |
| 63 | t.Fatal("old instance bypassed confirmation", failed, forge.creates) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func TestPlacementDeskSeparateSlotAndRemoval(t *testing.T) { |
| 68 | d, err := OpenDesk(t.TempDir(), "person") |
| 69 | if err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | notes := []wire.NotebookEntry{} |
| 73 | for i := 0; i < wire.MaxNoteEntries; i++ { |
| 74 | notes = append(notes, wire.NotebookEntry{ID: itoa(int64(i)), Source: wire.NotePerson, Text: strings.Repeat("p", 300)}) |
| 75 | } |
| 76 | if err := d.SaveEntries(notes); err != nil { |
| 77 | t.Fatal(err) |
| 78 | } |
| 79 | corrections := []wire.NotebookEntry{} |
| 80 | for i := 0; i < wire.MaxPlacementNotes+3; i++ { |
| 81 | corrections = append(corrections, wire.NotebookEntry{ID: "mv" + itoa(int64(i)), Source: wire.NoteSystem, Text: "Task tk1 moved from oficina/old to oficina/new."}) |
| 82 | } |
| 83 | if err := d.SavePlacementNotes(corrections); err != nil { |
| 84 | t.Fatal(err) |
| 85 | } |
| 86 | if len(d.Entries()) != wire.MaxNoteEntries+wire.MaxPlacementNotes || len(wire.PersonalNotes(d.Entries())) != wire.MaxNoteEntries { |
| 87 | t.Fatal("corrections evicted personal notes", d.Entries()) |
| 88 | } |
| 89 | if !strings.Contains(d.Notebook(1<<20), "System-observed project correction:") { |
| 90 | t.Fatal("missing system attribution") |
| 91 | } |
| 92 | if err := d.SaveEntries(notes); err != nil { |
| 93 | t.Fatal(err) |
| 94 | } |
| 95 | if len(d.PlacementNotes()) != wire.MaxPlacementNotes { |
| 96 | t.Fatal("ordinary notebook write removed corrections") |
| 97 | } |
| 98 | remaining := d.PlacementNotes()[1:] |
| 99 | if err := d.SavePlacementNotes(remaining); err != nil { |
| 100 | t.Fatal(err) |
| 101 | } |
| 102 | if len(d.Entries()) != wire.MaxNoteEntries+wire.MaxPlacementNotes-1 { |
| 103 | t.Fatal("removed correction remained") |
| 104 | } |
| 105 | if err := d.SavePlacementNotes(nil); err != nil { |
| 106 | t.Fatal(err) |
| 107 | } |
| 108 | if len(d.Entries()) != wire.MaxNoteEntries { |
| 109 | t.Fatal("empty snapshot did not clear corrections") |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | func TestPlacementCorrectionHasReservedContext(t *testing.T) { |
| 114 | d, err := OpenDesk(t.TempDir(), "person") |
| 115 | if err != nil { |
| 116 | t.Fatal(err) |
| 117 | } |
| 118 | own := []wire.NotebookEntry{} |
| 119 | for i := 0; i < wire.MaxNoteEntries; i++ { |
| 120 | own = append(own, wire.NotebookEntry{ID: itoa(int64(i)), Source: wire.NotePerson, Text: "PERSON WORDS " + strings.Repeat("x", 280)}) |
| 121 | } |
| 122 | if err := d.SaveEntries(own); err != nil { |
| 123 | t.Fatal(err) |
| 124 | } |
| 125 | if err := d.SavePlacementNotes([]wire.NotebookEntry{{ID: "mv1", Source: wire.NoteSystem, Text: "Task tk1 moved from oficina/old to oficina/new."}}); err != nil { |
| 126 | t.Fatal(err) |
| 127 | } |
| 128 | text := d.Notebook(notebookContext) |
| 129 | if !strings.Contains(text, "Task tk1 moved") || !strings.Contains(text, "PERSON WORDS") || len(text) > notebookContext { |
| 130 | t.Fatal("missing independent context slot", text) |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | func TestPlacementEmptySnapshotKeepsFreshDeskEmpty(t *testing.T) { |
| 135 | d, err := OpenDesk(t.TempDir(), "person") |
| 136 | if err != nil { |
| 137 | t.Fatal(err) |
| 138 | } |
| 139 | if err := d.SavePlacementNotes(nil); err != nil { |
| 140 | t.Fatal(err) |
| 141 | } |
| 142 | for _, path := range []string{d.entries, filepath.Join(filepath.Dir(d.entries), "placement.json")} { |
| 143 | if _, err := os.Stat(path); !os.IsNotExist(err) { |
| 144 | t.Fatal("empty delivery created notebook state", path, err) |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |