| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/wire" |
| 9 | ) |
| 10 | |
| 11 | func TestEngineeringChangeFilesAcceptanceAndDeviceView(t *testing.T) { |
| 12 | base, sha := strings.Repeat("a", 40), strings.Repeat("b", 40) |
| 13 | facts := &wire.ChangeFiles{Base: base, SHA: sha, Files: 1, Additions: 2, Entries: []wire.ChangedFile{{Path: "space 日本語.txt", Status: "added", Additions: 2}}} |
| 14 | good := stepRecord{ID: "s", State: wire.StepDone, Specialist: wire.SpecialistEngineering, BaseSHA: base, CandidateSHA: sha, Attempt: 2} |
| 15 | for name, tc := range map[string]struct { |
| 16 | protocol int |
| 17 | role string |
| 18 | step stepRecord |
| 19 | facts *wire.ChangeFiles |
| 20 | want bool |
| 21 | }{ |
| 22 | "primary": {31, wire.RolePrimary, good, facts, true}, |
| 23 | "worker": {31, wire.RoleWorker, good, facts, true}, |
| 24 | "old": {30, wire.RolePrimary, good, facts, false}, |
| 25 | "role": {31, "unknown", good, facts, false}, |
| 26 | "missing": {31, wire.RolePrimary, good, nil, false}, |
| 27 | "review": {31, wire.RolePrimary, stepRecord{Specialist: wire.SpecialistReview, BaseSHA: base, CandidateSHA: sha}, facts, false}, |
| 28 | "candidate": {31, wire.RolePrimary, stepRecord{Specialist: wire.SpecialistEngineering, BaseSHA: base, CandidateSHA: base}, facts, false}, |
| 29 | "base": {31, wire.RolePrimary, stepRecord{Specialist: wire.SpecialistEngineering, BaseSHA: sha, CandidateSHA: sha}, facts, false}, |
| 30 | } { |
| 31 | t.Run(name, func(t *testing.T) { |
| 32 | stored := acceptChangeFiles(Lease{Protocol: tc.protocol}, tc.role, tc.step, tc.facts) |
| 33 | if (stored != "") != tc.want { |
| 34 | t.Fatalf("accept: %q", stored) |
| 35 | } |
| 36 | s := tc.step |
| 37 | s.ChangeFiles = stored |
| 38 | v := stepView(s) |
| 39 | if v.ChangeFiles != nil { |
| 40 | t.Fatal("ordinary step view leaked facts") |
| 41 | } |
| 42 | v.ChangeFiles = stepChangeFilesView(s) |
| 43 | if (v.ChangeFiles != nil) != tc.want { |
| 44 | t.Fatalf("view %+v", v.ChangeFiles) |
| 45 | } |
| 46 | if tc.want { |
| 47 | a, _ := json.Marshal(v.ChangeFiles) |
| 48 | b, _ := json.Marshal(facts) |
| 49 | if string(a) != string(b) { |
| 50 | t.Fatalf("%s != %s", a, b) |
| 51 | } |
| 52 | } |
| 53 | }) |
| 54 | } |
| 55 | raw, _ := json.Marshal(facts) |
| 56 | for _, s := range []stepRecord{ |
| 57 | {State: wire.StepDone, Specialist: wire.SpecialistReview, BaseSHA: base, CandidateSHA: sha, ChangeFiles: string(raw)}, |
| 58 | {State: wire.StepDone, Specialist: wire.SpecialistEngineering, BaseSHA: base, CandidateSHA: base, ChangeFiles: string(raw)}, |
| 59 | {State: wire.StepDone, Specialist: wire.SpecialistEngineering, BaseSHA: sha, CandidateSHA: sha, ChangeFiles: string(raw)}, |
| 60 | {State: wire.StepRunning, Specialist: wire.SpecialistEngineering, BaseSHA: base, CandidateSHA: sha, ChangeFiles: string(raw)}, |
| 61 | {State: wire.StepDone, Specialist: wire.SpecialistEngineering, BaseSHA: base, CandidateSHA: sha, ChangeFiles: "{"}, |
| 62 | } { |
| 63 | if stepChangeFilesView(s) != nil { |
| 64 | t.Fatal("stale or malformed projection") |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |