Skip to content

Commit

Permalink
Merge pull request #278 from quiltt/fix-debugging-config
Browse files Browse the repository at this point in the history
Fix debugging config
  • Loading branch information
rubendinho authored Jul 21, 2024
2 parents 8c841a8 + 0d9e43d commit 13ea062
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 31 deletions.
7 changes: 7 additions & 0 deletions .changeset/lazy-jokes-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@quiltt/core": patch
"@quiltt/react": patch
"@quiltt/react-native": patch
---

Fix debug config during CI
7 changes: 1 addition & 6 deletions biome.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
{
"$schema": "./node_modules/@biomejs/biome/configuration_schema.json",
"files": {
"ignore": [
"**/*.gen.ts",
"**/generated/**",
"**/spectaql.css",
"**/src/components/HomeImage.tsx"
]
"ignore": ["**/*.gen.ts", "**/generated/**"]
},
"organizeImports": {
"enabled": true
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/api/rest/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Revoke = NoContentData | UnauthorizedData
export type SessionResponse = FetchResponse<SessionData>
export type UnprocessableResponse = FetchResponse<UnprocessableData>

// https://www.quiltt.dev/api-reference/rest/auth#
// https://www.quiltt.dev/api-reference/auth
export class AuthAPI {
clientId: string | undefined

Expand Down
9 changes: 3 additions & 6 deletions packages/core/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ import { name as packageName, version as packageVersion } from '../package.json'

const QUILTT_API_INSECURE = (() => {
try {
if (process.env.QUILTT_API_INSECURE === 'true' || process.env.QUILTT_API_INSECURE === 'false') {
return process.env.QUILTT_API_INSECURE === 'true'
}
return process.env.QUILTT_API_INSECURE
return process.env.QUILTT_API_INSECURE === 'true'
} catch {
return undefined
return false
}
})()

Expand All @@ -21,7 +18,7 @@ const QUILTT_API_DOMAIN = (() => {

const QUILTT_DEBUG = (() => {
try {
return !!process.env.QUILTT_DEBUG || process.env.NODE_ENV !== 'production'
return process.env.NODE_ENV !== 'production' && process.env.QUILTT_DEBUG === 'true'
} catch {
return false
}
Expand Down
4 changes: 3 additions & 1 deletion packages/core/tests/api/rest/auth.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { AuthAPI, PasscodePayload, UsernamePayload } from '@/api/rest/auth'
import { AuthAPI } from '@/api/rest/auth'
import { fetchWithRetry } from '@/api/rest/fetchWithRetry'
import { endpointAuth } from '@/configuration'
import { describe, it, beforeEach, expect, vi } from 'vitest'

import type { PasscodePayload, UsernamePayload } from '@/api/rest/auth'
import type { Mock, MockInstance } from 'vitest'

vi.mock('@/api/rest/fetchWithRetry', () => ({
Expand Down
105 changes: 88 additions & 17 deletions packages/core/tests/configuration.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { describe, it, expect, beforeEach } from 'vitest'
import { name as packageName, version as packageVersion } from '../package.json'

// Save the original environment variables
const originalEnv = process.env

// Function to load the configuration with cache busting
const loadConfig = async (envConfig: Record<string, string | undefined>) => {
// Set environment variables
Expand All @@ -13,35 +16,103 @@ const loadConfig = async (envConfig: Record<string, string | undefined>) => {
return import(configModulePath)
}

// Reset the environment variables before each test
beforeEach(() => {
process.env = { ...originalEnv }
})

describe('Configuration Constants', () => {
it('should format the version correctly', async () => {
const config = await loadConfig({})
expect(config.version).toBe(`${packageName}: v${packageVersion}`)
})

it('should reflect the correct debugging status based on environment', async () => {
const config = await loadConfig({ NODE_ENV: 'development' })
expect(config.debugging).toBeTruthy()
it('should use secure protocols with quiltt.io by default', async () => {
const config = await loadConfig({})

expect(config.cdnBase).toMatch('https://cdn.quiltt.io')
expect(config.endpointAuth).toMatch('https://auth.quiltt.io/v1/users/session')
expect(config.endpointGraphQL).toMatch('https://api.quiltt.io/v1/graphql')
expect(config.endpointWebsockets).toMatch('wss://api.quiltt.io/websockets')
})
})

describe.each([
['true', 'http', 'ws'],
['false', 'https', 'wss'],
])('Configuration under insecure=%s', (insecure, httpProtocol, wsProtocol) => {
let config: any
// Ensure that packages work on React Native and other platforms with no process.env support
describe('when process.env is not available', () => {
beforeEach(async () => {
// @ts-ignore
// biome-ignore lint/performance/noDelete: <explanation>
delete process.env
})

it('should use secure protocols with quiltt.io', async () => {
const config = await loadConfig({})

beforeEach(async () => {
config = await loadConfig({ QUILTT_API_INSECURE: insecure })
expect(config.cdnBase).toBe('https://cdn.quiltt.io')
expect(config.endpointAuth).toBe('https://auth.quiltt.io/v1/users/session')
expect(config.endpointGraphQL).toBe('https://api.quiltt.io/v1/graphql')
expect(config.endpointWebsockets).toBe('wss://api.quiltt.io/websockets')
})
})

it(`should use ${httpProtocol.toUpperCase()} for CDN and endpoints`, () => {
expect(config.cdnBase.startsWith(`${httpProtocol}://`)).toBeTruthy()
expect(config.endpointAuth.startsWith(`${httpProtocol}://`)).toBeTruthy()
expect(config.endpointGraphQL.startsWith(`${httpProtocol}://`)).toBeTruthy()
describe('API Domain', () => {
describe.each([
[undefined, 'quiltt.io'],
['quiltt.io', 'quiltt.io'],
['lvh.me:3000', 'lvh.me:3000'],
])('when QUILTT_API_DOMAIN is %s', (quilttApiDomain, expectedDomain) => {
it(`should use ${expectedDomain}`, async () => {
const config = await loadConfig({ QUILTT_API_DOMAIN: quilttApiDomain })

expect(config.cdnBase).toMatch(new RegExp(`://cdn.${expectedDomain}$`))
expect(config.endpointAuth).toMatch(
new RegExp(`://auth.${expectedDomain}/v1/users/session$`)
)
expect(config.endpointGraphQL).toMatch(new RegExp(`://api.${expectedDomain}/v1/graphql$`))
expect(config.endpointWebsockets).toMatch(
new RegExp(`://api.${expectedDomain}/websockets$`)
)
})
})
})

it(`should use ${wsProtocol.toUpperCase()} for Websockets`, () => {
expect(config.endpointWebsockets.startsWith(`${wsProtocol}://`)).toBeTruthy()
describe('Protocols', () => {
describe.each([
['true', 'http', 'ws'],
['false', 'https', 'wss'],
[undefined, 'https', 'wss'],
])('Configuration when QUILTT_API_INSECURE is %s', (insecure, httpProtocol, wsProtocol) => {
let config: any

beforeEach(async () => {
config = await loadConfig({ QUILTT_API_INSECURE: insecure })
})

it(`should use ${httpProtocol.toUpperCase()} for CDN and endpoints`, () => {
expect(config.cdnBase).toMatch(new RegExp(`^${httpProtocol}://`))
expect(config.endpointAuth).toMatch(new RegExp(`^${httpProtocol}://`))
expect(config.endpointGraphQL).toMatch(new RegExp(`^${httpProtocol}://`))
})

it(`should use ${wsProtocol.toUpperCase()} for Websockets`, () => {
expect(config.endpointWebsockets).toMatch(new RegExp(`^${wsProtocol}://`))
})
})
})

describe('Debugging', () => {
describe.each([
['production', 'true', false],
['production', 'false', false],
['production', undefined, false],
['development', 'true', true],
['development', 'false', false],
['development', undefined, false],
])('when NODE_ENV is %s and QUILTT_DEBUG is %s', (nodeEnv, quilttDebug, expectedDebugging) => {
it(`should be ${expectedDebugging ? 'enabled' : 'disabled'}`, async () => {
const config = await loadConfig({ NODE_ENV: nodeEnv, QUILTT_DEBUG: quilttDebug })

expect(config.debugging).toBe(expectedDebugging)
})
})
})
})

0 comments on commit 13ea062

Please sign in to comment.