-
Notifications
You must be signed in to change notification settings - Fork 0
feat(codeTracking): add custom apiEndpoint and token configuration #172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -301,3 +301,136 @@ enabled = true` | |
| require.NotNil(t, config.CodeTracking, "CodeTracking should be present") | ||
| assert.True(t, *config.CodeTracking.Enabled, "CodeTracking.Enabled should be overridden by local config") | ||
| } | ||
|
|
||
| func TestCodeTrackingCustomEndpointAndToken(t *testing.T) { | ||
| // Create a temporary directory for test configs | ||
| tmpDir, err := os.MkdirTemp("", "shelltime-test-*") | ||
| require.NoError(t, err) | ||
| defer os.RemoveAll(tmpDir) | ||
|
|
||
| // Create config file with custom CodeTracking endpoint and token | ||
| baseConfigPath := filepath.Join(tmpDir, "config.toml") | ||
| baseConfig := `Token = 'global-token' | ||
| APIEndpoint = 'https://api.global.com' | ||
|
|
||
| [codeTracking] | ||
| enabled = true | ||
| apiEndpoint = 'https://api.custom-heartbeat.com' | ||
| token = 'custom-heartbeat-token'` | ||
| err = os.WriteFile(baseConfigPath, []byte(baseConfig), 0644) | ||
| require.NoError(t, err) | ||
|
|
||
| cs := NewConfigService(baseConfigPath) | ||
| config, err := cs.ReadConfigFile(context.Background()) | ||
| require.NoError(t, err) | ||
|
|
||
| // Verify global config values | ||
| assert.Equal(t, "global-token", config.Token) | ||
| assert.Equal(t, "https://api.global.com", config.APIEndpoint) | ||
|
|
||
| // Verify CodeTracking custom values | ||
| require.NotNil(t, config.CodeTracking, "CodeTracking should be present") | ||
| assert.True(t, *config.CodeTracking.Enabled) | ||
| assert.Equal(t, "https://api.custom-heartbeat.com", config.CodeTracking.APIEndpoint) | ||
| assert.Equal(t, "custom-heartbeat-token", config.CodeTracking.Token) | ||
| } | ||
|
|
||
| func TestCodeTrackingPartialCustomConfig(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| config string | ||
| expectedAPIEndpoint string | ||
| expectedToken string | ||
| }{ | ||
| { | ||
| name: "Only custom apiEndpoint", | ||
| config: `Token = 'global-token' | ||
| APIEndpoint = 'https://api.global.com' | ||
|
|
||
| [codeTracking] | ||
| enabled = true | ||
| apiEndpoint = 'https://api.custom.com'`, | ||
| expectedAPIEndpoint: "https://api.custom.com", | ||
| expectedToken: "", // empty, should fall back to global | ||
| }, | ||
| { | ||
| name: "Only custom token", | ||
| config: `Token = 'global-token' | ||
| APIEndpoint = 'https://api.global.com' | ||
|
|
||
| [codeTracking] | ||
| enabled = true | ||
| token = 'custom-token'`, | ||
| expectedAPIEndpoint: "", // empty, should fall back to global | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the previous test case, this comment could be misinterpreted. This test verifies that |
||
| expectedToken: "custom-token", | ||
| }, | ||
| { | ||
| name: "No custom endpoint or token", | ||
| config: `Token = 'global-token' | ||
| APIEndpoint = 'https://api.global.com' | ||
|
|
||
| [codeTracking] | ||
| enabled = true`, | ||
| expectedAPIEndpoint: "", | ||
| expectedToken: "", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| tmpDir, err := os.MkdirTemp("", "shelltime-test-*") | ||
| require.NoError(t, err) | ||
| defer os.RemoveAll(tmpDir) | ||
|
|
||
| baseConfigPath := filepath.Join(tmpDir, "config.toml") | ||
| err = os.WriteFile(baseConfigPath, []byte(tc.config), 0644) | ||
| require.NoError(t, err) | ||
|
|
||
| cs := NewConfigService(baseConfigPath) | ||
| config, err := cs.ReadConfigFile(context.Background()) | ||
| require.NoError(t, err) | ||
|
|
||
| require.NotNil(t, config.CodeTracking, "CodeTracking should be present") | ||
| assert.Equal(t, tc.expectedAPIEndpoint, config.CodeTracking.APIEndpoint) | ||
| assert.Equal(t, tc.expectedToken, config.CodeTracking.Token) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestCodeTrackingMergeEndpointFromLocal(t *testing.T) { | ||
| // Create a temporary directory for test configs | ||
| tmpDir, err := os.MkdirTemp("", "shelltime-test-*") | ||
| require.NoError(t, err) | ||
| defer os.RemoveAll(tmpDir) | ||
|
|
||
| // Create base config file with CodeTracking | ||
| baseConfigPath := filepath.Join(tmpDir, "config.toml") | ||
| baseConfig := `Token = 'base-token' | ||
| APIEndpoint = 'https://api.base.com' | ||
|
|
||
| [codeTracking] | ||
| enabled = true | ||
| apiEndpoint = 'https://api.base-heartbeat.com' | ||
| token = 'base-heartbeat-token'` | ||
| err = os.WriteFile(baseConfigPath, []byte(baseConfig), 0644) | ||
| require.NoError(t, err) | ||
|
|
||
| // Create local config file that overrides CodeTracking endpoint and token | ||
| localConfigPath := filepath.Join(tmpDir, "config.local.toml") | ||
| localConfig := `[codeTracking] | ||
| enabled = true | ||
| apiEndpoint = 'https://api.local-heartbeat.com' | ||
| token = 'local-heartbeat-token'` | ||
| err = os.WriteFile(localConfigPath, []byte(localConfig), 0644) | ||
| require.NoError(t, err) | ||
|
|
||
| cs := NewConfigService(baseConfigPath) | ||
| config, err := cs.ReadConfigFile(context.Background()) | ||
| require.NoError(t, err) | ||
|
|
||
| // Verify local config overrides base CodeTracking | ||
| require.NotNil(t, config.CodeTracking, "CodeTracking should be present") | ||
| assert.True(t, *config.CodeTracking.Enabled) | ||
| assert.Equal(t, "https://api.local-heartbeat.com", config.CodeTracking.APIEndpoint) | ||
| assert.Equal(t, "local-heartbeat-token", config.CodeTracking.Token) | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -34,7 +34,9 @@ type CCOtel struct { | |||||
|
|
||||||
| // CodeTracking configuration for coding activity heartbeat tracking | ||||||
| type CodeTracking struct { | ||||||
| Enabled *bool `toml:"enabled"` | ||||||
| Enabled *bool `toml:"enabled"` | ||||||
| APIEndpoint string `toml:"apiEndpoint"` // Custom API endpoint for heartbeats | ||||||
| Token string `toml:"token"` // Custom token for heartbeats | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For consistency with the surrounding code and Go formatting conventions, it's better to have a single space between the struct tag and the line comment. The extra spaces here seem to be a minor formatting artifact.
Suggested change
|
||||||
| } | ||||||
|
|
||||||
| // LogCleanup configuration for automatic log file cleanup | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is slightly misleading. This test correctly verifies that
config.CodeTracking.Tokenis empty when not provided in the config. However, it doesn't test the fallback behavior itself, which happens inSendHeartbeatsToServer. A clearer comment might be// empty, as it's not set in this configto avoid confusion about the scope of this test case.