| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "io" |
| 6 | "log/slog" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "reflect" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | |
| 13 | "amadan.net/rastrillo/rastrillo/db" |
| 14 | "amadan.net/rastrillo/rastrillo/migrate" |
| 15 | "amadan.net/rastrillo/rastrillo/migrate/dump" |
| 16 | ) |
| 17 | |
| 18 | func TestMigrationModels(t *testing.T) { |
| 19 | // Rastrillo v0.30.1's gormlite parser strips double quotes but retains |
| 20 | // single quotes on defaults. Normalize only this check's in-memory SQL; |
| 21 | // the snapshot and default tests verify the executable migration unchanged. |
| 22 | migrations := append([]migrate.Migration(nil), Schema.All()...) |
| 23 | for i := range migrations { |
| 24 | if migrations[i].ID == "equipo/0032_plan_defaults" { |
| 25 | migrations[i].SQL = strings.ReplaceAll(migrations[i].SQL, "DEFAULT 'bug'", `DEFAULT "bug"`) |
| 26 | migrations[i].SQL = strings.ReplaceAll(migrations[i].SQL, "DEFAULT ''", `DEFAULT ""`) |
| 27 | } |
| 28 | } |
| 29 | payload, err := dump.Compute(migrations, BootSchema.All(), Models) |
| 30 | if err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | for _, change := range payload.Changes { |
| 34 | t.Errorf("models and migrations disagree: %s", change.SQL) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | func TestConversationMigrationSnapshot(t *testing.T) { |
| 39 | ctx := context.Background() |
| 40 | // The pinned SchemaSQL dumper sorts triggers before tables. Replay instead |
| 41 | // so this comparison also covers executable triggers and their source tables. |
| 42 | actual, err := migrate.Replay(ctx, Schema.All()) |
| 43 | if err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | defer actual.Close() |
| 47 | saved, err := os.ReadFile("migrations/schema.sql") |
| 48 | if err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | d, err := db.Open(filepath.Join(t.TempDir(), "snapshot.db"), slog.New(slog.NewTextHandler(io.Discard, nil))) |
| 52 | if err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | t.Cleanup(func() { |
| 56 | if err := d.Close(); err != nil { |
| 57 | t.Error(err) |
| 58 | } |
| 59 | }) |
| 60 | if _, err = d.Writer().Exec(string(saved)); err != nil { |
| 61 | t.Fatal(err) |
| 62 | } |
| 63 | savedSchema, err := migrate.Read(ctx, d.Writer()) |
| 64 | if err != nil { |
| 65 | t.Fatal(err) |
| 66 | } |
| 67 | migrationSchema, err := migrate.Read(ctx, actual) |
| 68 | if err != nil { |
| 69 | t.Fatal(err) |
| 70 | } |
| 71 | if !savedSchema.Equal(migrationSchema) { |
| 72 | t.Fatal("schema snapshot differs from ordered migrations", savedSchema.Diff(migrationSchema)) |
| 73 | } |
| 74 | triggers := func(q migrate.Querier) map[string]string { |
| 75 | rows, err := q.QueryContext(ctx, "SELECT name,sql FROM sqlite_master WHERE type='trigger' ORDER BY name") |
| 76 | if err != nil { |
| 77 | t.Fatal(err) |
| 78 | } |
| 79 | defer rows.Close() |
| 80 | out := map[string]string{} |
| 81 | for rows.Next() { |
| 82 | var name, sql string |
| 83 | if err = rows.Scan(&name, &sql); err != nil { |
| 84 | t.Fatal(err) |
| 85 | } |
| 86 | out[name] = strings.TrimSpace(sql) |
| 87 | } |
| 88 | if err = rows.Err(); err != nil { |
| 89 | t.Fatal(err) |
| 90 | } |
| 91 | return out |
| 92 | } |
| 93 | if !reflect.DeepEqual(triggers(actual), triggers(d.Writer())) { |
| 94 | t.Fatal("schema snapshot triggers differ from ordered migrations") |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // Messages written before lanes stay the Coordinator's, every key |
| 99 | // included, and the Assistant may use the same keys in its own lane: a |
| 100 | // key is unique per person and lane. |
| 101 | func TestAssistantLaneMigrationKeepsKeys(t *testing.T) { |
| 102 | ctx := context.Background() |
| 103 | all := Schema.All() |
| 104 | at := -1 |
| 105 | for i, m := range all { |
| 106 | if m.ID == "equipo/0077_assistant_lane" { |
| 107 | at = i |
| 108 | } |
| 109 | } |
| 110 | if at < 0 { |
| 111 | t.Fatal("no 0077_assistant_lane") |
| 112 | } |
| 113 | d, err := migrate.Replay(ctx, all[:at]) |
| 114 | if err != nil { |
| 115 | t.Fatal(err) |
| 116 | } |
| 117 | defer d.Close() |
| 118 | insert := "INSERT INTO chat_messages(id, person, idempotency_key, body_hash, role, text, turn_id, revision, created_at) VALUES (?, 'ana@example.com', ?, 'h', 'person', 'old', 't_old', 1, '2026-11-01T00:00:00Z')" |
| 119 | for id, key := range map[string]string{"m_old1": "assistant:x", "m_old2": "x"} { |
| 120 | if _, err = d.ExecContext(ctx, insert, id, key); err != nil { |
| 121 | t.Fatal(err) |
| 122 | } |
| 123 | } |
| 124 | if _, err = d.ExecContext(ctx, all[at].SQL); err != nil { |
| 125 | t.Fatal(err) |
| 126 | } |
| 127 | var coordinator int |
| 128 | if err = d.QueryRowContext(ctx, "SELECT count(*) FROM chat_messages WHERE lane = ''").Scan(&coordinator); err != nil || coordinator != 2 { |
| 129 | t.Fatalf("historical messages in the Coordinator lane: %d %v", coordinator, err) |
| 130 | } |
| 131 | lane := "INSERT INTO chat_messages(id, person, idempotency_key, lane) VALUES (?, 'ana@example.com', ?, ?)" |
| 132 | for id, key := range map[string]string{"m_new1": "x", "m_new2": "assistant:x"} { |
| 133 | if _, err = d.ExecContext(ctx, lane, id, key, "assistant"); err != nil { |
| 134 | t.Fatalf("Assistant key %q beside a historical one: %v", key, err) |
| 135 | } |
| 136 | } |
| 137 | if _, err = d.ExecContext(ctx, lane, "m_new3", "assistant:x", ""); err == nil { |
| 138 | t.Fatal("a second Coordinator message with a historical key") |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | // Cards already waiting when push arrives opened before any device could |
| 143 | // hear of them: 0096 marks them alerted, so a later write of one (setting |
| 144 | // it aside included) never alerts it late. Others are not marked. |
| 145 | func TestPushMigrationMarksOpenCards(t *testing.T) { |
| 146 | ctx := context.Background() |
| 147 | all := Schema.All() |
| 148 | at := -1 |
| 149 | for i, m := range all { |
| 150 | if m.ID == "equipo/0096_push" { |
| 151 | at = i |
| 152 | } |
| 153 | } |
| 154 | if at < 0 { |
| 155 | t.Fatal("no 0096_push") |
| 156 | } |
| 157 | d, err := migrate.Replay(ctx, all[:at]) |
| 158 | if err != nil { |
| 159 | t.Fatal(err) |
| 160 | } |
| 161 | defer d.Close() |
| 162 | for id, state := range map[string]string{"c_open": "open", "c_ask": "ask", "c_answered": "answered"} { |
| 163 | if _, err = d.ExecContext(ctx, "INSERT INTO chat_cards(id, person, kind, state, revision) VALUES (?, 'ana@example.com', 'question', ?, 1)", id, state); err != nil { |
| 164 | t.Fatal(err) |
| 165 | } |
| 166 | } |
| 167 | if _, err = d.ExecContext(ctx, all[at].SQL); err != nil { |
| 168 | t.Fatal(err) |
| 169 | } |
| 170 | rows, err := d.QueryContext(ctx, "SELECT key FROM push_once WHERE person = 'ana@example.com' ORDER BY key") |
| 171 | if err != nil { |
| 172 | t.Fatal(err) |
| 173 | } |
| 174 | defer rows.Close() |
| 175 | var keys []string |
| 176 | for rows.Next() { |
| 177 | var k string |
| 178 | if err := rows.Scan(&k); err != nil { |
| 179 | t.Fatal(err) |
| 180 | } |
| 181 | keys = append(keys, k) |
| 182 | } |
| 183 | if strings.Join(keys, " ") != "card:c_ask card:c_open" { |
| 184 | t.Fatalf("marked %v", keys) |
| 185 | } |
| 186 | } |
| 187 | |