-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved configuration validation (#278)
* Improved configuration validation from aider feedback plus added additional validation * Add tests, remove hard coded strings in favor of constants, typo fixes * Remove accidental import * Undo change * Fix linting issue * add comment to test * Remove optional value from validation * Add valid test cases * Improve tests --------- Co-authored-by: Christopher Speller <[email protected]>
- Loading branch information
1 parent
0e1814d
commit 06c7e57
Showing
4 changed files
with
346 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,304 @@ | ||
package ai | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBotConfig_IsValid(t *testing.T) { | ||
type fields struct { | ||
ID string | ||
Name string | ||
DisplayName string | ||
CustomInstructions string | ||
Service ServiceConfig | ||
EnableVision bool | ||
DisableTools bool | ||
ChannelAccessLevel ChannelAccessLevel | ||
ChannelIDs []string | ||
UserAccessLevel UserAccessLevel | ||
UserIDs []string | ||
TeamIDs []string | ||
MaxFileSize int64 | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
want bool | ||
}{ | ||
{ | ||
name: "Valid OpenAI configuration with minimal required fields", | ||
fields: fields{ | ||
ID: "xxx", | ||
Name: "xxx", | ||
DisplayName: "xxx", | ||
CustomInstructions: "", | ||
Service: ServiceConfig{ | ||
Name: "Copilot", | ||
Type: "openai", | ||
APIKey: "sk-xyz", | ||
OrgID: "org-xyz", | ||
DefaultModel: "gpt-40", | ||
TokenLimit: 100, | ||
StreamingTimeoutSeconds: 60, | ||
}, | ||
ChannelAccessLevel: ChannelAccessLevelAll, | ||
UserAccessLevel: UserAccessLevelAll, | ||
}, | ||
want: true, | ||
}, | ||
{ | ||
name: "Valid OpenAI configuration with ChannelAccessLevelNone", | ||
fields: fields{ | ||
ID: "xxx", | ||
Name: "xxx", | ||
DisplayName: "xxx", | ||
CustomInstructions: "", | ||
Service: ServiceConfig{ | ||
Name: "Copilot", | ||
Type: "openai", | ||
APIKey: "sk-xyz", | ||
OrgID: "org-xyz", | ||
DefaultModel: "gpt-40", | ||
TokenLimit: 100, | ||
StreamingTimeoutSeconds: 60, | ||
}, | ||
ChannelAccessLevel: ChannelAccessLevelNone, | ||
UserAccessLevel: UserAccessLevelAll, | ||
}, | ||
want: true, | ||
}, | ||
{ | ||
name: "Bot name cannot be empty", | ||
fields: fields{ | ||
ID: "xxx", | ||
Name: "", // bad | ||
DisplayName: "xxx", | ||
CustomInstructions: "", | ||
Service: ServiceConfig{ | ||
Name: "Copilot", | ||
Type: "openai", | ||
APIKey: "sk-xyz", | ||
OrgID: "org-xyz", | ||
DefaultModel: "gpt-40", | ||
TokenLimit: 100, | ||
StreamingTimeoutSeconds: 60, | ||
}, | ||
ChannelAccessLevel: ChannelAccessLevelAll, | ||
UserAccessLevel: UserAccessLevelAll, | ||
}, | ||
want: false, | ||
}, | ||
{ | ||
name: "Bot display name cannot be empty", | ||
fields: fields{ | ||
ID: "xxx", | ||
Name: "xxx", | ||
DisplayName: "", // bad | ||
CustomInstructions: "", | ||
Service: ServiceConfig{ | ||
Name: "Copilot", | ||
Type: "openai", | ||
APIKey: "sk-xyz", | ||
OrgID: "org-xyz", | ||
DefaultModel: "gpt-40", | ||
TokenLimit: 100, | ||
StreamingTimeoutSeconds: 60, | ||
}, | ||
ChannelAccessLevel: ChannelAccessLevelAll, | ||
UserAccessLevel: UserAccessLevelAll, | ||
}, | ||
want: false, | ||
}, | ||
{ | ||
name: "Service type must be one of the supported providers", | ||
fields: fields{ | ||
ID: "xxx", | ||
Name: "xxx", | ||
DisplayName: "xxx", | ||
CustomInstructions: "", | ||
Service: ServiceConfig{ | ||
Name: "Copilot", | ||
Type: "mattermostllm", // bad | ||
APIKey: "sk-xyz", | ||
OrgID: "org-xyz", | ||
DefaultModel: "gpt-40", | ||
TokenLimit: 100, | ||
StreamingTimeoutSeconds: 60, | ||
}, | ||
ChannelAccessLevel: ChannelAccessLevelAll, | ||
UserAccessLevel: UserAccessLevelAll, | ||
}, | ||
want: false, | ||
}, | ||
{ | ||
name: "Channel access level cannot be less than ChannelAccessLevelAll (0)", | ||
fields: fields{ | ||
ID: "xxx", | ||
Name: "xxx", | ||
DisplayName: "xxx", | ||
CustomInstructions: "", | ||
Service: ServiceConfig{ | ||
Name: "Copilot", | ||
Type: "openai", | ||
APIKey: "sk-xyz", | ||
OrgID: "org-xyz", | ||
DefaultModel: "gpt-40", | ||
TokenLimit: 100, | ||
StreamingTimeoutSeconds: 60, | ||
}, | ||
ChannelAccessLevel: ChannelAccessLevelAll - 1, // bad | ||
UserAccessLevel: UserAccessLevelNone, | ||
}, | ||
want: false, | ||
}, | ||
{ | ||
name: "Channel access level cannot be greater than ChannelAccessLevelNone (3)", | ||
fields: fields{ | ||
ID: "xxx", | ||
Name: "xxx", | ||
DisplayName: "xxx", | ||
CustomInstructions: "", | ||
Service: ServiceConfig{ | ||
Name: "Copilot", | ||
Type: "openai", | ||
APIKey: "sk-xyz", | ||
OrgID: "org-xyz", | ||
DefaultModel: "gpt-40", | ||
TokenLimit: 100, | ||
StreamingTimeoutSeconds: 60, | ||
}, | ||
ChannelAccessLevel: ChannelAccessLevelNone + 1, // bad | ||
UserAccessLevel: UserAccessLevelNone, | ||
}, | ||
want: false, | ||
}, | ||
{ | ||
name: "User access level cannot be less than UserAccessLevelAll (0)", | ||
fields: fields{ | ||
ID: "xxx", | ||
Name: "xxx", | ||
DisplayName: "xxx", | ||
CustomInstructions: "", | ||
Service: ServiceConfig{ | ||
Name: "Copilot", | ||
Type: "openai", | ||
APIKey: "sk-xyz", | ||
OrgID: "org-xyz", | ||
DefaultModel: "gpt-40", | ||
TokenLimit: 100, | ||
StreamingTimeoutSeconds: 60, | ||
}, | ||
ChannelAccessLevel: ChannelAccessLevelAll, | ||
UserAccessLevel: UserAccessLevelAll - 1, // bad | ||
}, | ||
want: false, | ||
}, | ||
{ | ||
name: "User access level cannot be greater than UserAccessLevelNone (3)", | ||
fields: fields{ | ||
ID: "xxx", | ||
Name: "xxx", | ||
DisplayName: "xxx", | ||
CustomInstructions: "", | ||
Service: ServiceConfig{ | ||
Name: "Copilot", | ||
Type: "openai", | ||
APIKey: "sk-xyz", | ||
OrgID: "org-xyz", | ||
DefaultModel: "gpt-40", | ||
TokenLimit: 100, | ||
StreamingTimeoutSeconds: 60, | ||
}, | ||
ChannelAccessLevel: ChannelAccessLevelAll, | ||
UserAccessLevel: UserAccessLevelNone + 1, // bad | ||
}, | ||
want: false, | ||
}, | ||
{ | ||
name: "OpenAI compatible service requires API URL to be set", | ||
fields: fields{ | ||
ID: "xxx", | ||
Name: "xxx", | ||
DisplayName: "xxx", | ||
CustomInstructions: "", | ||
Service: ServiceConfig{ | ||
Name: "Copilot", | ||
Type: "openaicompatible", | ||
APIKey: "sk-xyz", | ||
APIURL: "", // bad | ||
OrgID: "org-xyz", | ||
DefaultModel: "gpt-40", | ||
TokenLimit: 100, | ||
StreamingTimeoutSeconds: 60, | ||
}, | ||
ChannelAccessLevel: ChannelAccessLevelNone + 1, | ||
UserAccessLevel: UserAccessLevelNone, | ||
}, | ||
want: false, | ||
}, | ||
{ | ||
name: "Ask Sage service requires username to be set", | ||
fields: fields{ | ||
ID: "xxx", | ||
Name: "xxx", | ||
DisplayName: "xxx", | ||
CustomInstructions: "", | ||
Service: ServiceConfig{ | ||
Name: "Copilot", | ||
Type: "asksage", | ||
Username: "", // bad | ||
Password: "topsecret", | ||
DefaultModel: "xxx", | ||
TokenLimit: 100, | ||
StreamingTimeoutSeconds: 60, | ||
}, | ||
ChannelAccessLevel: ChannelAccessLevelNone + 1, | ||
UserAccessLevel: UserAccessLevelNone, | ||
}, | ||
want: false, | ||
}, | ||
{ | ||
name: "Ask Sage service requires password to be set", | ||
fields: fields{ | ||
ID: "xxx", | ||
Name: "xxx", | ||
DisplayName: "xxx", | ||
CustomInstructions: "", | ||
Service: ServiceConfig{ | ||
Name: "Copilot", | ||
Type: "asksage", | ||
Username: "myuser", | ||
Password: "", // bad | ||
DefaultModel: "xxx", | ||
TokenLimit: 100, | ||
StreamingTimeoutSeconds: 60, | ||
}, | ||
ChannelAccessLevel: ChannelAccessLevelNone + 1, | ||
UserAccessLevel: UserAccessLevelNone, | ||
}, | ||
want: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
c := &BotConfig{ | ||
ID: tt.fields.ID, | ||
Name: tt.fields.Name, | ||
DisplayName: tt.fields.DisplayName, | ||
CustomInstructions: tt.fields.CustomInstructions, | ||
Service: tt.fields.Service, | ||
EnableVision: tt.fields.EnableVision, | ||
DisableTools: tt.fields.DisableTools, | ||
ChannelAccessLevel: tt.fields.ChannelAccessLevel, | ||
ChannelIDs: tt.fields.ChannelIDs, | ||
UserAccessLevel: tt.fields.UserAccessLevel, | ||
UserIDs: tt.fields.UserIDs, | ||
TeamIDs: tt.fields.TeamIDs, | ||
MaxFileSize: tt.fields.MaxFileSize, | ||
} | ||
assert.Equalf(t, tt.want, c.IsValid(), "IsValid() for test case %q", tt.name) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters