|
| 1 | +package manifest |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/slackapi/slack-cli/internal/shared/types" |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func Test_Diff(t *testing.T) { |
| 12 | + tests := map[string]struct { |
| 13 | + local types.AppManifest |
| 14 | + remote types.AppManifest |
| 15 | + expected []FieldDiff |
| 16 | + }{ |
| 17 | + "identical manifests produce no diffs": { |
| 18 | + local: types.AppManifest{ |
| 19 | + DisplayInformation: types.DisplayInformation{Name: "App"}, |
| 20 | + }, |
| 21 | + remote: types.AppManifest{ |
| 22 | + DisplayInformation: types.DisplayInformation{Name: "App"}, |
| 23 | + }, |
| 24 | + expected: nil, |
| 25 | + }, |
| 26 | + "modified field detected": { |
| 27 | + local: types.AppManifest{ |
| 28 | + DisplayInformation: types.DisplayInformation{Name: "App", Description: "Local desc"}, |
| 29 | + }, |
| 30 | + remote: types.AppManifest{ |
| 31 | + DisplayInformation: types.DisplayInformation{Name: "App", Description: "Remote desc"}, |
| 32 | + }, |
| 33 | + expected: []FieldDiff{ |
| 34 | + {Path: "display_information.description", Type: DiffModified, LocalValue: "Local desc", RemoteValue: "Remote desc"}, |
| 35 | + }, |
| 36 | + }, |
| 37 | + "local-only field detected": { |
| 38 | + local: types.AppManifest{ |
| 39 | + DisplayInformation: types.DisplayInformation{Name: "App", Description: "Has desc"}, |
| 40 | + }, |
| 41 | + remote: types.AppManifest{ |
| 42 | + DisplayInformation: types.DisplayInformation{Name: "App"}, |
| 43 | + }, |
| 44 | + expected: []FieldDiff{ |
| 45 | + {Path: "display_information.description", Type: DiffLocalOnly, LocalValue: "Has desc"}, |
| 46 | + }, |
| 47 | + }, |
| 48 | + "remote-only field detected": { |
| 49 | + local: types.AppManifest{ |
| 50 | + DisplayInformation: types.DisplayInformation{Name: "App"}, |
| 51 | + }, |
| 52 | + remote: types.AppManifest{ |
| 53 | + DisplayInformation: types.DisplayInformation{Name: "App", Description: "Remote only"}, |
| 54 | + }, |
| 55 | + expected: []FieldDiff{ |
| 56 | + {Path: "display_information.description", Type: DiffRemoteOnly, RemoteValue: "Remote only"}, |
| 57 | + }, |
| 58 | + }, |
| 59 | + "function added locally": { |
| 60 | + local: types.AppManifest{ |
| 61 | + DisplayInformation: types.DisplayInformation{Name: "App"}, |
| 62 | + Functions: map[string]types.ManifestFunction{ |
| 63 | + "greet": {Title: "Greet", Description: "Hello"}, |
| 64 | + }, |
| 65 | + }, |
| 66 | + remote: types.AppManifest{ |
| 67 | + DisplayInformation: types.DisplayInformation{Name: "App"}, |
| 68 | + }, |
| 69 | + expected: []FieldDiff{ |
| 70 | + {Path: "functions.greet.description", Type: DiffLocalOnly, LocalValue: "Hello"}, |
| 71 | + {Path: "functions.greet.title", Type: DiffLocalOnly, LocalValue: "Greet"}, |
| 72 | + }, |
| 73 | + }, |
| 74 | + "array values compared as wholes": { |
| 75 | + local: types.AppManifest{ |
| 76 | + DisplayInformation: types.DisplayInformation{Name: "App"}, |
| 77 | + OAuthConfig: &types.OAuthConfig{ |
| 78 | + Scopes: &types.ManifestScopes{ |
| 79 | + Bot: []string{"chat:write", "users:read"}, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + remote: types.AppManifest{ |
| 84 | + DisplayInformation: types.DisplayInformation{Name: "App"}, |
| 85 | + OAuthConfig: &types.OAuthConfig{ |
| 86 | + Scopes: &types.ManifestScopes{ |
| 87 | + Bot: []string{"chat:write", "files:read"}, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + expected: []FieldDiff{ |
| 92 | + { |
| 93 | + Path: "oauth_config.scopes.bot", |
| 94 | + Type: DiffModified, |
| 95 | + LocalValue: []any{"chat:write", "users:read"}, |
| 96 | + RemoteValue: []any{"chat:write", "files:read"}, |
| 97 | + }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + } |
| 101 | + for name, tc := range tests { |
| 102 | + t.Run(name, func(t *testing.T) { |
| 103 | + result, err := Diff(tc.local, tc.remote) |
| 104 | + require.NoError(t, err) |
| 105 | + if tc.expected == nil { |
| 106 | + assert.False(t, result.HasDifferences()) |
| 107 | + return |
| 108 | + } |
| 109 | + assert.True(t, result.HasDifferences()) |
| 110 | + for _, expectedDiff := range tc.expected { |
| 111 | + found := false |
| 112 | + for _, actualDiff := range result.Diffs { |
| 113 | + if actualDiff.Path == expectedDiff.Path { |
| 114 | + found = true |
| 115 | + assert.Equal(t, expectedDiff.Type, actualDiff.Type, "diff type mismatch for path %s", expectedDiff.Path) |
| 116 | + if expectedDiff.LocalValue != nil { |
| 117 | + assert.Equal(t, expectedDiff.LocalValue, actualDiff.LocalValue, "local value mismatch for path %s", expectedDiff.Path) |
| 118 | + } |
| 119 | + if expectedDiff.RemoteValue != nil { |
| 120 | + assert.Equal(t, expectedDiff.RemoteValue, actualDiff.RemoteValue, "remote value mismatch for path %s", expectedDiff.Path) |
| 121 | + } |
| 122 | + break |
| 123 | + } |
| 124 | + } |
| 125 | + assert.True(t, found, "expected diff not found for path %s", expectedDiff.Path) |
| 126 | + } |
| 127 | + }) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func Test_DiffResult_HasDifferences(t *testing.T) { |
| 132 | + t.Run("empty result has no differences", func(t *testing.T) { |
| 133 | + result := &DiffResult{} |
| 134 | + assert.False(t, result.HasDifferences()) |
| 135 | + }) |
| 136 | + |
| 137 | + t.Run("result with diffs has differences", func(t *testing.T) { |
| 138 | + result := &DiffResult{ |
| 139 | + Diffs: []FieldDiff{{Path: "test", Type: DiffModified}}, |
| 140 | + } |
| 141 | + assert.True(t, result.HasDifferences()) |
| 142 | + }) |
| 143 | +} |
0 commit comments