| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | ) |
| 6 | |
| 7 | // Migration 0121's columns round-trip through the records under the |
| 8 | // names the migration gives them, and a record that leaves them unset |
| 9 | // keeps today's meaning: the team's specialist, no maker, no binding, a |
| 10 | // Research file. |
| 11 | func TestSpecialistStorageRoundTrip(t *testing.T) { |
| 12 | r := newHKRig(t) |
| 13 | g := r.s.db.G |
| 14 | r.must(g.Create(&specialistRecord{ID: "sp_d", Profile: "design-v1", ProjectID: "p1", Name: "Design", State: "active", Revision: 2}).Error) |
| 15 | r.must(g.Create(&taskRecord{ID: "t_d", ProjectID: "p1", Title: "Overhaul", MakerSpecialistID: "sp_d", DesignRuns: 3}).Error) |
| 16 | r.must(g.Create(&stepRecord{ID: "s_d", TaskID: "t_d", Specialist: "engineering", SpecialistID: "sp_d", SpecialistRevision: 2, SpecialistProject: "p1", SpecialistCaps: `["branch.edit","page.render"]`}).Error) |
| 17 | r.must(g.Create(&taskFileRecord{ID: "f_d", TaskID: "t_d", StepID: "s_d", Attempt: 1, Source: taskFileRender, Option: "calm", TreeDigest: "sha256:abc"}).Error) |
| 18 | |
| 19 | var sp specialistRecord |
| 20 | r.must(g.First(&sp, "id = ?", "sp_d").Error) |
| 21 | var task taskRecord |
| 22 | r.must(g.First(&task, "id = ?", "t_d").Error) |
| 23 | var step stepRecord |
| 24 | r.must(g.First(&step, "id = ?", "s_d").Error) |
| 25 | var file taskFileRecord |
| 26 | r.must(g.First(&file, "id = ?", "f_d").Error) |
| 27 | if sp.ProjectID != "p1" || task.MakerSpecialistID != "sp_d" || task.DesignRuns != 3 { |
| 28 | t.Fatalf("specialist %+v task maker %q runs %d", sp, task.MakerSpecialistID, task.DesignRuns) |
| 29 | } |
| 30 | if step.SpecialistID != "sp_d" || step.SpecialistRevision != 2 || step.SpecialistProject != "p1" || step.SpecialistCaps != `["branch.edit","page.render"]` { |
| 31 | t.Fatalf("step binding %q %d %q %q", step.SpecialistID, step.SpecialistRevision, step.SpecialistProject, step.SpecialistCaps) |
| 32 | } |
| 33 | if file.Source != taskFileRender || file.Option != "calm" || file.TreeDigest != "sha256:abc" { |
| 34 | t.Fatalf("file %q %q %q", file.Source, file.Option, file.TreeDigest) |
| 35 | } |
| 36 | var cols string |
| 37 | r.must(g.Raw(`SELECT s.project_id || '|' || t.maker_specialist_id || '|' || t.design_runs || '|' || st.specialist_id || '|' || st.specialist_revision || '|' || st.specialist_project || '|' || f.source || '|' || f."option" || '|' || f.tree_digest |
| 38 | FROM chat_specialists s, chat_tasks t, chat_steps st, chat_task_files f WHERE s.id = 'sp_d' AND t.id = 't_d' AND st.id = 's_d' AND f.id = 'f_d'`).Scan(&cols).Error) |
| 39 | if cols != "p1|sp_d|3|sp_d|2|p1|render|calm|sha256:abc" { |
| 40 | t.Fatalf("columns %q", cols) |
| 41 | } |
| 42 | |
| 43 | // Unset: today's meaning. |
| 44 | r.must(g.Create(&specialistRecord{ID: "sp_r", Profile: "research-v1", Name: "Research", State: "active", Revision: 1}).Error) |
| 45 | r.must(g.Create(&taskFileRecord{ID: "f_r", TaskID: "t_d", StepID: "s_d", Attempt: 1, ImageID: "i1"}).Error) |
| 46 | var plain struct { |
| 47 | Project string |
| 48 | Source string |
| 49 | } |
| 50 | r.must(g.Raw(`SELECT (SELECT project_id FROM chat_specialists WHERE id = 'sp_r') AS project, (SELECT source FROM chat_task_files WHERE id = 'f_r') AS source`).Scan(&plain).Error) |
| 51 | if plain.Project != "" || plain.Source != taskFileResearch { |
| 52 | t.Fatalf("unset columns %+v", plain) |
| 53 | } |
| 54 | |
| 55 | // The records' indexes hold the migration's scope rule too (stores |
| 56 | // built from Models): the team and p1 each one active design-v1 and |
| 57 | // one open proposal, never two. |
| 58 | r.must(g.Create(&specialistRecord{ID: "sp_team", Profile: "design-v1", Name: "Design", State: "active", Revision: 1}).Error) |
| 59 | r.must(g.Create(&specialistRecord{ID: "sp_p1_open", Profile: "design-v1", ProjectID: "p1", Name: "Design", State: "proposed", Revision: 1}).Error) |
| 60 | for _, dup := range []specialistRecord{ |
| 61 | {ID: "sp_p1_2", Profile: "design-v1", ProjectID: "p1", State: "active"}, |
| 62 | {ID: "sp_team_2", Profile: "design-v1", State: "active"}, |
| 63 | {ID: "sp_p1_open2", Profile: "design-v1", ProjectID: "p1", State: "proposed"}, |
| 64 | } { |
| 65 | if err := g.Create(&dup).Error; err == nil { |
| 66 | t.Fatalf("%s: a second %s design-v1 in scope %q", dup.ID, dup.State, dup.ProjectID) |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |