| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "testing" |
| 6 | |
| 7 | "amadan.net/rastrillo/rastrillo/migrate" |
| 8 | ) |
| 9 | |
| 10 | // Migration 0113 moves an open place card raised in a job thread to its |
| 11 | // person's own conversation, where they can answer it (production, |
| 12 | // 2026-11-27: a publish_site asked in the conversation put its card in |
| 13 | // the task's job lane, c_mh62tz7zw2vq5wkojybcmzzcsv). Only open place |
| 14 | // cards in a job lane move; their revision moves with them; a second run |
| 15 | // changes nothing. |
| 16 | func TestPlaceLaneMigrationMovesOpenPlaceCards(t *testing.T) { |
| 17 | ctx := context.Background() |
| 18 | var before []migrate.Migration |
| 19 | var repair migrate.Migration |
| 20 | for _, m := range Schema.All() { |
| 21 | if m.ID == "equipo/0113_place_cards_in_conversation" { |
| 22 | repair = m |
| 23 | break |
| 24 | } |
| 25 | before = append(before, m) |
| 26 | } |
| 27 | if repair.SQL == "" { |
| 28 | t.Fatal("no migration 0113") |
| 29 | } |
| 30 | d, err := migrate.Replay(ctx, before) |
| 31 | if err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | defer d.Close() |
| 35 | if _, err := d.ExecContext(ctx, `INSERT INTO chat_cards (id, person, kind, state, lane, revision, created_at, updated_at) VALUES |
| 36 | ('c1', 'p@example.com', 'place', 'open', 'job:h_abc', 1, '2026-11-27', '2026-11-27'), |
| 37 | ('c2', 'p@example.com', 'place', 'answered', 'job:h_abc', 2, '2026-11-27', '2026-11-27'), |
| 38 | ('c3', 'p@example.com', 'place', 'open', '', 1, '2026-11-27', '2026-11-27'), |
| 39 | ('c4', 'p@example.com', 'publish', 'open', 'job:h_abc', 1, '2026-11-27', '2026-11-27'), |
| 40 | ('c5', 'p@example.com', 'place', 'open', 'assistant', 1, '2026-11-27', '2026-11-27')`); err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | for range 2 { |
| 44 | if _, err := d.ExecContext(ctx, repair.SQL); err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | } |
| 48 | want := map[string]string{"c1": "/2", "c2": "job:h_abc/2", "c3": "/1", "c4": "job:h_abc/1", "c5": "assistant/1"} |
| 49 | rows, err := d.QueryContext(ctx, `SELECT id, coalesce(lane, '') || '/' || revision FROM chat_cards ORDER BY id`) |
| 50 | if err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | defer rows.Close() |
| 54 | n := 0 |
| 55 | for rows.Next() { |
| 56 | var id, got string |
| 57 | if err := rows.Scan(&id, &got); err != nil { |
| 58 | t.Fatal(err) |
| 59 | } |
| 60 | n++ |
| 61 | if want[id] != got { |
| 62 | t.Fatalf("%s: %s, want %s", id, got, want[id]) |
| 63 | } |
| 64 | } |
| 65 | if n != len(want) { |
| 66 | t.Fatalf("%d cards", n) |
| 67 | } |
| 68 | } |
| 69 | |