| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "testing" |
| 7 | |
| 8 | "amadan.net/rastrillo/rastrillo/migrate" |
| 9 | ) |
| 10 | |
| 11 | // Migration 0121 (dynamic specialists slice 1) adds the storage Design |
| 12 | // needs and changes what no existing row means: a specialist from before |
| 13 | // it is the team's (""), a task has no maker and no Design runs, a step |
| 14 | // is bound to no data specialist, and a task file is a Research one. |
| 15 | func TestSpecialistStorageMigrationKeepsMeaning(t *testing.T) { |
| 16 | ctx := context.Background() |
| 17 | var before []migrate.Migration |
| 18 | var storage migrate.Migration |
| 19 | for _, m := range Schema.All() { |
| 20 | if m.ID == "equipo/0121_specialist_storage" { |
| 21 | storage = m |
| 22 | break |
| 23 | } |
| 24 | before = append(before, m) |
| 25 | } |
| 26 | if storage.SQL == "" { |
| 27 | t.Fatal("no migration 0121") |
| 28 | } |
| 29 | d, err := migrate.Replay(ctx, before) |
| 30 | if err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | defer d.Close() |
| 34 | for _, q := range []string{ |
| 35 | `INSERT INTO chat_specialists (id, profile, name, state, revision) VALUES ('sp_old', 'research-v1', 'Research', 'active', 2)`, |
| 36 | `INSERT INTO chat_tasks (id, project_id, title, revision) VALUES ('t_old', 'p1', 'Old', 1)`, |
| 37 | `INSERT INTO chat_steps (id, task_id, idx, specialist, state, attempt, research_grant, research_grant_revision) VALUES ('s_old', 't_old', 0, 'research', 'done', 1, 'g_old', 2)`, |
| 38 | `INSERT INTO chat_task_files (id, task_id, step_id, attempt, image_id, kept) VALUES ('f_old', 't_old', 's_old', 1, 'i1', 1)`, |
| 39 | } { |
| 40 | if _, err := d.ExecContext(ctx, q); err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | } |
| 44 | if _, err := d.ExecContext(ctx, storage.SQL); err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | for _, c := range []struct{ q, want string }{ |
| 48 | {`SELECT project_id FROM chat_specialists WHERE id = 'sp_old'`, ""}, |
| 49 | {`SELECT maker_specialist_id || '/' || design_runs FROM chat_tasks WHERE id = 't_old'`, "/0"}, |
| 50 | {`SELECT specialist_id || '/' || specialist_revision || '/' || specialist_project || '/' || specialist_caps FROM chat_steps WHERE id = 's_old'`, "/0//"}, |
| 51 | {`SELECT source || '/' || option || '/' || tree_digest FROM chat_task_files WHERE id = 'f_old'`, "research//"}, |
| 52 | } { |
| 53 | var got sql.NullString |
| 54 | if err := d.QueryRowContext(ctx, c.q).Scan(&got); err != nil { |
| 55 | t.Fatalf("%s: %v", c.q, err) |
| 56 | } |
| 57 | if !got.Valid || got.String != c.want { |
| 58 | t.Fatalf("%s: %+v, want %q", c.q, got, c.want) |
| 59 | } |
| 60 | } |
| 61 | // A row written after 0121 without naming the columns means the same. |
| 62 | if _, err := d.ExecContext(ctx, `INSERT INTO chat_task_files (id, task_id, step_id, attempt, image_id) VALUES ('f_new', 't_old', 's_old', 1, 'i2')`); err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | var source string |
| 66 | if err := d.QueryRowContext(ctx, `SELECT source FROM chat_task_files WHERE id = 'f_new'`).Scan(&source); err != nil || source != "research" { |
| 67 | t.Fatalf("new file source %q %v", source, err) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // One open proposal and one active row per profile and scope: the team |
| 72 | // ("") and each project hold their own, and no scope holds two. Declined |
| 73 | // and revoked rows are history and never conflict. |
| 74 | func TestSpecialistScopeUniqueness(t *testing.T) { |
| 75 | ctx := context.Background() |
| 76 | d, err := migrate.Replay(ctx, Schema.All()) |
| 77 | if err != nil { |
| 78 | t.Fatal(err) |
| 79 | } |
| 80 | defer d.Close() |
| 81 | insert := func(id, project, state string) error { |
| 82 | _, err := d.ExecContext(ctx, `INSERT INTO chat_specialists (id, profile, project_id, name, state, revision) VALUES (?, 'design-v1', ?, 'Design', ?, 1)`, id, project, state) |
| 83 | return err |
| 84 | } |
| 85 | for _, ok := range []struct{ id, project, state string }{ |
| 86 | {"sp_team", "", "active"}, |
| 87 | {"sp_p1", "p1", "active"}, |
| 88 | {"sp_p2", "p2", "active"}, |
| 89 | {"sp_team_open", "", "proposed"}, |
| 90 | {"sp_p1_open", "p1", "proposed"}, |
| 91 | {"sp_p1_no", "p1", "declined"}, |
| 92 | {"sp_p1_no2", "p1", "declined"}, |
| 93 | {"sp_p1_gone", "p1", "revoked"}, |
| 94 | {"sp_p1_gone2", "p1", "revoked"}, |
| 95 | } { |
| 96 | if err := insert(ok.id, ok.project, ok.state); err != nil { |
| 97 | t.Fatalf("%s (%q %s) refused: %v", ok.id, ok.project, ok.state, err) |
| 98 | } |
| 99 | } |
| 100 | for _, bad := range []struct{ id, project, state string }{ |
| 101 | {"sp_team2", "", "active"}, |
| 102 | {"sp_p1_2", "p1", "active"}, |
| 103 | {"sp_team_open2", "", "proposed"}, |
| 104 | {"sp_p1_open2", "p1", "proposed"}, |
| 105 | } { |
| 106 | if err := insert(bad.id, bad.project, bad.state); err == nil { |
| 107 | t.Fatalf("%s: a second %s row for design-v1 in scope %q", bad.id, bad.state, bad.project) |
| 108 | } |
| 109 | } |
| 110 | // Another profile in the same scope is its own. |
| 111 | if _, err := d.ExecContext(ctx, `INSERT INTO chat_specialists (id, profile, project_id, name, state, revision) VALUES ('sp_r', 'research-v1', 'p1', 'Research', 'active', 1)`); err != nil { |
| 112 | t.Fatalf("research-v1 beside design-v1 in p1: %v", err) |
| 113 | } |
| 114 | // A proposal Add makes active while the scope's active row is revoked. |
| 115 | if _, err := d.ExecContext(ctx, `UPDATE chat_specialists SET state = 'revoked' WHERE id = 'sp_p1'`); err != nil { |
| 116 | t.Fatal(err) |
| 117 | } |
| 118 | if _, err := d.ExecContext(ctx, `UPDATE chat_specialists SET state = 'active' WHERE id = 'sp_p1_open'`); err != nil { |
| 119 | t.Fatalf("activating p1's proposal after the revoke: %v", err) |
| 120 | } |
| 121 | } |
| 122 | |