|
| 1 | +package heals_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/githubnext/apm/internal/install/heals" |
| 7 | +) |
| 8 | + |
| 9 | +func TestNewHealContext(t *testing.T) { |
| 10 | + hctx := heals.NewHealContext("owner/repo@pkg", true, false, false) |
| 11 | + if hctx.PackageKey != "owner/repo@pkg" { |
| 12 | + t.Errorf("PackageKey: got %q", hctx.PackageKey) |
| 13 | + } |
| 14 | + if !hctx.LockfileMatch { |
| 15 | + t.Error("LockfileMatch should be true") |
| 16 | + } |
| 17 | + if hctx.LockfileMatchViaContentHashOnly { |
| 18 | + t.Error("LockfileMatchViaContentHashOnly should be false") |
| 19 | + } |
| 20 | + if hctx.BypassKeys == nil { |
| 21 | + t.Error("BypassKeys should be initialized") |
| 22 | + } |
| 23 | + if hctx.FiredGroups == nil { |
| 24 | + t.Error("FiredGroups should be initialized") |
| 25 | + } |
| 26 | + if len(hctx.Messages) != 0 { |
| 27 | + t.Error("Messages should be empty") |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func TestHealContextEmit(t *testing.T) { |
| 32 | + hctx := heals.NewHealContext("pkg", false, false, false) |
| 33 | + hctx.Emit(heals.HealMessageInfo, "info message") |
| 34 | + hctx.Emit(heals.HealMessageWarn, "warn message") |
| 35 | + if len(hctx.Messages) != 2 { |
| 36 | + t.Fatalf("expected 2 messages, got %d", len(hctx.Messages)) |
| 37 | + } |
| 38 | + if hctx.Messages[0].Level != heals.HealMessageInfo { |
| 39 | + t.Errorf("first message level: got %d", hctx.Messages[0].Level) |
| 40 | + } |
| 41 | + if hctx.Messages[0].Text != "info message" { |
| 42 | + t.Errorf("first message text: got %q", hctx.Messages[0].Text) |
| 43 | + } |
| 44 | + if hctx.Messages[0].PackageKey != "pkg" { |
| 45 | + t.Errorf("message PackageKey: got %q", hctx.Messages[0].PackageKey) |
| 46 | + } |
| 47 | + if hctx.Messages[1].Level != heals.HealMessageWarn { |
| 48 | + t.Errorf("second message level: got %d", hctx.Messages[1].Level) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func TestHealContextAddBypassKey(t *testing.T) { |
| 53 | + hctx := heals.NewHealContext("pkg", false, false, false) |
| 54 | + hctx.AddBypassKey("dep/a") |
| 55 | + hctx.AddBypassKey("dep/b") |
| 56 | + if !hctx.BypassKeys["dep/a"] { |
| 57 | + t.Error("dep/a should be a bypass key") |
| 58 | + } |
| 59 | + if !hctx.BypassKeys["dep/b"] { |
| 60 | + t.Error("dep/b should be a bypass key") |
| 61 | + } |
| 62 | + if hctx.BypassKeys["dep/c"] { |
| 63 | + t.Error("dep/c should not be a bypass key") |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// mockHeal implements heals.Heal for testing. |
| 68 | +type mockHeal struct { |
| 69 | + name string |
| 70 | + order int |
| 71 | + group string |
| 72 | + applies bool |
| 73 | + executed bool |
| 74 | +} |
| 75 | + |
| 76 | +func (m *mockHeal) Name() string { return m.name } |
| 77 | +func (m *mockHeal) Order() int { return m.order } |
| 78 | +func (m *mockHeal) ExclusiveGroup() string { return m.group } |
| 79 | +func (m *mockHeal) Applies(hctx *heals.HealContext) bool { return m.applies } |
| 80 | +func (m *mockHeal) Execute(hctx *heals.HealContext) { m.executed = true } |
| 81 | + |
| 82 | +func TestRunHealChain_ExclusiveGroup(t *testing.T) { |
| 83 | + h1 := &mockHeal{name: "h1", order: 1, group: "grp", applies: true} |
| 84 | + h2 := &mockHeal{name: "h2", order: 2, group: "grp", applies: true} |
| 85 | + hctx := heals.NewHealContext("pkg", false, false, false) |
| 86 | + heals.RunHealChain(&hctx, []heals.Heal{h1, h2}) |
| 87 | + if !h1.executed { |
| 88 | + t.Error("h1 should have executed") |
| 89 | + } |
| 90 | + if h2.executed { |
| 91 | + t.Error("h2 should NOT have executed (exclusive group already fired)") |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func TestRunHealChain_NotApplies(t *testing.T) { |
| 96 | + h1 := &mockHeal{name: "h1", order: 1, group: "", applies: false} |
| 97 | + hctx := heals.NewHealContext("pkg", false, false, false) |
| 98 | + heals.RunHealChain(&hctx, []heals.Heal{h1}) |
| 99 | + if h1.executed { |
| 100 | + t.Error("h1 should NOT execute when Applies returns false") |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func TestRunHealChain_MultipleGroups(t *testing.T) { |
| 105 | + h1 := &mockHeal{name: "h1", order: 1, group: "grp1", applies: true} |
| 106 | + h2 := &mockHeal{name: "h2", order: 2, group: "grp2", applies: true} |
| 107 | + hctx := heals.NewHealContext("pkg", false, false, false) |
| 108 | + heals.RunHealChain(&hctx, []heals.Heal{h1, h2}) |
| 109 | + if !h1.executed { |
| 110 | + t.Error("h1 should have executed") |
| 111 | + } |
| 112 | + if !h2.executed { |
| 113 | + t.Error("h2 should have executed (different group)") |
| 114 | + } |
| 115 | +} |
0 commit comments