From 5d457ce338373c5d86f6d04fcf23ca18c5483c5e Mon Sep 17 00:00:00 2001 From: Piotr Kukielka Date: Tue, 14 Jan 2025 18:20:11 +0100 Subject: [PATCH] Fix and add new tests --- .vscode/settings.json | 10 ++- .../protocol_generated/AuthenticationError.kt | 12 ++++ .../CodyContextFilterItem.kt | 11 ++- .../agent/protocol_generated/Constants.kt | 2 + .../src/configuration/auth-resolver.test.ts | 67 ++++++++++++++++++- 5 files changed, 97 insertions(+), 5 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 7dd5a69e09b1..4e19fcfea7a5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -42,5 +42,13 @@ "[typescriptreact]": { "editor.defaultFormatter": "biomejs.biome" }, - "rust-analyzer.procMacro.ignored": { "napi-derive": ["napi"] } + "rust-analyzer.procMacro.ignored": { + "napi-derive": [ + "napi" + ] + }, + "debug.javascript.defaultRuntimeExecutable": { + "pwa-node": "/Users/pkukielka/.local/share/mise/shims/node" + }, + "python.defaultInterpreterPath": "/Users/pkukielka/.local/share/mise/shims/python" } diff --git a/agent/bindings/kotlin/lib/src/main/kotlin/com/sourcegraph/cody/agent/protocol_generated/AuthenticationError.kt b/agent/bindings/kotlin/lib/src/main/kotlin/com/sourcegraph/cody/agent/protocol_generated/AuthenticationError.kt index 3535471ab4be..1a9d1c571983 100644 --- a/agent/bindings/kotlin/lib/src/main/kotlin/com/sourcegraph/cody/agent/protocol_generated/AuthenticationError.kt +++ b/agent/bindings/kotlin/lib/src/main/kotlin/com/sourcegraph/cody/agent/protocol_generated/AuthenticationError.kt @@ -16,6 +16,7 @@ sealed class AuthenticationError { "network-error" -> context.deserialize(element, NetworkAuthError::class.java) "invalid-access-token" -> context.deserialize(element, InvalidAccessTokenError::class.java) "enterprise-user-logged-into-dotcom" -> context.deserialize(element, EnterpriseUserDotComError::class.java) + "auth-config-error" -> context.deserialize(element, AuthConfigError::class.java) else -> throw Exception("Unknown discriminator ${element}") } } @@ -50,3 +51,14 @@ data class EnterpriseUserDotComError( } } +data class AuthConfigError( + val title: String? = null, + val message: String, + val type: TypeEnum, // Oneof: auth-config-error +) : AuthenticationError() { + + enum class TypeEnum { + @SerializedName("auth-config-error") `Auth-config-error`, + } +} + diff --git a/agent/bindings/kotlin/lib/src/main/kotlin/com/sourcegraph/cody/agent/protocol_generated/CodyContextFilterItem.kt b/agent/bindings/kotlin/lib/src/main/kotlin/com/sourcegraph/cody/agent/protocol_generated/CodyContextFilterItem.kt index 471920ca5ab1..11eb2f828a4f 100644 --- a/agent/bindings/kotlin/lib/src/main/kotlin/com/sourcegraph/cody/agent/protocol_generated/CodyContextFilterItem.kt +++ b/agent/bindings/kotlin/lib/src/main/kotlin/com/sourcegraph/cody/agent/protocol_generated/CodyContextFilterItem.kt @@ -1,8 +1,15 @@ @file:Suppress("FunctionName", "ClassName", "unused", "EnumEntryName", "UnusedImport") package com.sourcegraph.cody.agent.protocol_generated; +import com.google.gson.annotations.SerializedName; + data class CodyContextFilterItem( - val repoNamePattern: String, + val repoNamePattern: RepoNamePatternEnum, // Oneof: .* val filePathPatterns: List? = null, -) +) { + + enum class RepoNamePatternEnum { + @SerializedName(".*") ``, + } +} diff --git a/agent/bindings/kotlin/lib/src/main/kotlin/com/sourcegraph/cody/agent/protocol_generated/Constants.kt b/agent/bindings/kotlin/lib/src/main/kotlin/com/sourcegraph/cody/agent/protocol_generated/Constants.kt index 895fbbd02b2c..f40ef40c6123 100644 --- a/agent/bindings/kotlin/lib/src/main/kotlin/com/sourcegraph/cody/agent/protocol_generated/Constants.kt +++ b/agent/bindings/kotlin/lib/src/main/kotlin/com/sourcegraph/cody/agent/protocol_generated/Constants.kt @@ -2,6 +2,7 @@ package com.sourcegraph.cody.agent.protocol_generated; object Constants { + const val `` = ".*" const val Applied = "Applied" const val Applying = "Applying" const val Automatic = "Automatic" @@ -15,6 +16,7 @@ object Constants { const val agentic = "agentic" const val ask = "ask" const val assistant = "assistant" + const val `auth-config-error` = "auth-config-error" const val authenticated = "authenticated" const val autocomplete = "autocomplete" const val balanced = "balanced" diff --git a/lib/shared/src/configuration/auth-resolver.test.ts b/lib/shared/src/configuration/auth-resolver.test.ts index 52f69e429e38..13dc504ffb60 100644 --- a/lib/shared/src/configuration/auth-resolver.test.ts +++ b/lib/shared/src/configuration/auth-resolver.test.ts @@ -61,9 +61,10 @@ describe('auth-resolver', () => { }) test('resolve custom auth provider', async () => { + const expirationEpoch = 2736874802 const credentialsJson = JSON.stringify({ headers: { Authorization: 'token X' }, - expiration: 1337, + expiration: expirationEpoch, }) const auth = await resolveAuth( @@ -91,11 +92,73 @@ describe('auth-resolver', () => { expect(auth.serverEndpoint).toBe('https://my-server.com/') const headerCredential = auth.credentials as HeaderCredential - expect(headerCredential.expiration).toBe(1337) + expect(headerCredential.expiration).toBe(expirationEpoch) expect(headerCredential.getHeaders()).toStrictEqual({ Authorization: 'token X', }) expect(JSON.stringify(headerCredential)).not.toContain('token X') }) + + test('resolve custom auth provider error handling - bad JSON', async () => { + const auth = await resolveAuth( + 'sourcegraph.com', + { + authExternalProviders: [ + { + endpoint: 'https://my-server.com', + executable: { + commandLine: ['echo '], + shell: isWindows() ? process.env.ComSpec : '/bin/bash', + timeout: 5000, + windowsHide: true, + }, + }, + ], + overrideServerEndpoint: 'https://my-server.com', + overrideAuthToken: undefined, + }, + new TempClientSecrets(new Map()) + ) + + expect(auth.serverEndpoint).toBe('https://my-server.com/') + + expect(auth.credentials).toBe(undefined) + expect(auth.error.message).toContain('Unexpected end of JSON input') + }) + + test('resolve custom auth provider error handling - bad expiration', async () => { + const expirationEpoch = 1636865002 + const credentialsJson = JSON.stringify({ + headers: { Authorization: 'token X' }, + expiration: expirationEpoch, + }) + + const auth = await resolveAuth( + 'sourcegraph.com', + { + authExternalProviders: [ + { + endpoint: 'https://my-server.com', + executable: { + commandLine: [ + isWindows() ? `echo ${credentialsJson}` : `echo '${credentialsJson}'`, + ], + shell: isWindows() ? process.env.ComSpec : '/bin/bash', + timeout: 5000, + windowsHide: true, + }, + }, + ], + overrideServerEndpoint: 'https://my-server.com', + overrideAuthToken: undefined, + }, + new TempClientSecrets(new Map()) + ) + + expect(auth.serverEndpoint).toBe('https://my-server.com/') + + expect(auth.credentials).toBe(undefined) + expect(auth.error.message).toContain('Credentials expiration cannot be se to the past date') + }) })