import { PluginConfig, resolveConfig } from './config' import { buildHogEnv } from './env' const PERSONAL_API_KEY = 'phx_test' const PROJECT_ID = '1' const SELF_HOSTED_HOST = 'https://posthog.example.com' function testConfig(overrides: Partial = {}) { return resolveConfig({ personalApiKey: PERSONAL_API_KEY, projectId: PROJECT_ID, cliBinaryPath: '/tmp/hog', ...overrides, }) } describe('buildHogEnv', () => { afterEach(() => { vi.restoreAllMocks() }) it.each([ { host: undefined, inherited: undefined, expected: 'https://us.posthog.com' }, { host: 'https://eu.i.posthog.com', inherited: undefined, expected: 'https://eu.posthog.com' }, { host: 'https://us.posthog.com', inherited: undefined, expected: 'https://us.posthog.com' }, { host: SELF_HOSTED_HOST, inherited: undefined, expected: SELF_HOSTED_HOST }, // A single-label self-hosted name must not match an inherited object property. { host: 'https://constructor', inherited: undefined, expected: 'https://constructor' }, // Without a configured host, an inherited one wins, so a login for another region stays there. { host: undefined, inherited: 'https://eu.posthog.com', expected: 'https://eu.posthog.com' }, { host: SELF_HOSTED_HOST, inherited: 'https://eu.posthog.com', expected: SELF_HOSTED_HOST }, ])('sends a keyless build to the app host for $host with HOG_HOST=$inherited', ({ host, inherited, expected }) => { // Logins and CI OIDC tokens are for the app host, so the ingestion host the plugins // default to would leave a keyless build without credentials. const config = testConfig({ host, personalApiKey: '' }) const env = buildHogEnv(config, inherited ? { HOG_HOST: inherited } : {}) expect(env.HOG_HOST).toBe(expected) }) it.each([ { host: undefined, inherited: undefined, expected: 'https://us.i.posthog.com' }, { host: 'https://eu.i.posthog.com', inherited: undefined, expected: 'https://eu.i.posthog.com' }, { host: undefined, inherited: 'https://eu.posthog.com', expected: 'https://eu.posthog.com' }, ])('keeps the host of a build with a key for $host with HOG_HOST=$inherited', ({ host, inherited, expected }) => { // A build that uploaded through posthog-cli talks to the same host, so its egress allowlist still holds. const config = testConfig({ host }) const env = buildHogEnv(config, inherited ? { HOG_HOST: inherited } : {}) expect(env.HOG_HOST).toBe(expected) }) it('passes the configured key and project to hog', () => { const env = buildHogEnv(testConfig(), {}) expect(env).toMatchObject({ HOG_PERSONAL_API_KEY: PERSONAL_API_KEY, HOG_PROJECT_ID: PROJECT_ID }) }) it('sets no key without one, so the CI job can log in with its OIDC token', () => { // HOG_PERSONAL_API_KEY wins over OIDC in hog, so even an empty value must stay unset. const keyless = testConfig({ personalApiKey: '' }) const env = buildHogEnv(keyless, {}) expect(env).not.toHaveProperty('HOG_PERSONAL_API_KEY') expect(env.HOG_PROJECT_ID).toBe(PROJECT_ID) }) it('carries posthog-cli settings over to hog without overriding hog ones, and warns once to rename them', () => { const warn = vi.spyOn(process.stderr, 'write').mockImplementation(() => true) const legacyRateLimit = '60' const hogConcurrency = '4' const inherited = { POSTHOG_CLIENT_RATE_LIMIT: legacyRateLimit, POSTHOG_CLI_SOURCEMAP_UPLOAD_CONCURRENCY: '20', HOG_UPLOAD_CONCURRENCY: hogConcurrency, POSTHOG_CLI_API_KEY: 'phx_legacy', } const env = buildHogEnv({ host: SELF_HOSTED_HOST, projectId: PROJECT_ID }, inherited) buildHogEnv({ host: SELF_HOSTED_HOST, projectId: PROJECT_ID }, inherited) expect(env.HOG_RATE_LIMIT).toBe(legacyRateLimit) expect(env.HOG_UPLOAD_CONCURRENCY).toBe(hogConcurrency) expect(env).not.toHaveProperty('HOG_PERSONAL_API_KEY') // One build runs hog several times, so a warning per run would repeat in the build log. expect(warn.mock.calls.map(([message]) => message)).toEqual([ expect.stringContaining('POSTHOG_CLIENT_RATE_LIMIT is a posthog-cli variable'), expect.stringContaining('Rename it to HOG_UPLOAD_CONCURRENCY'), ]) }) it('pins text output, so `release` prints only the id the plugins read', () => { const inherited = { HOG_OUTPUT: 'json', HOG_RAW: '1' } const env = buildHogEnv({ host: SELF_HOSTED_HOST, projectId: PROJECT_ID }, inherited) expect(env.HOG_OUTPUT).toBe('text') expect(env).not.toHaveProperty('HOG_RAW') }) it('turns on hog request logging only for the debug log level', () => { const debug = testConfig({ logLevel: 'debug' }) const debugEnv = buildHogEnv(debug, {}) const infoEnv = buildHogEnv(testConfig(), {}) expect(debugEnv.HOG_DEBUG).toBe('1') expect(infoEnv).not.toHaveProperty('HOG_DEBUG') }) }) describe('the OIDC hint', () => { // A fresh module per case, because the hint shows once per process. async function freshBuildHogEnv(): Promise { vi.resetModules() return (await import('./env')).buildHogEnv } afterEach(() => { vi.restoreAllMocks() }) it.each([ { provider: 'Depot CI', ci: { DEPOT_JOB_URL: 'https://depot.dev/orgs/acme/jobs/1' } }, { provider: 'GitHub Actions', ci: { GITHUB_ACTIONS: 'true' } }, { provider: 'GitLab.com', ci: { GITLAB_CI: 'true', CI_SERVER_URL: 'https://gitlab.com' } }, ])('points a $provider build with a key at its OIDC token, once', async ({ provider, ci }) => { const stderr = vi.spyOn(process.stderr, 'write').mockImplementation(() => true) const build = await freshBuildHogEnv() build(testConfig(), ci) build(testConfig(), ci) expect(stderr.mock.calls.map(([message]) => message)).toEqual([ expect.stringContaining(`This build runs in ${provider}, which can upload with the job's OIDC token`), ]) }) it.each([ { name: 'without a key', personalApiKey: '', ci: { GITHUB_ACTIONS: 'true' } }, { name: 'outside CI', personalApiKey: PERSONAL_API_KEY, ci: {} }, // Self-managed GitLab can't trade its token, so the hint would send the build nowhere. { name: 'on self-managed GitLab', personalApiKey: PERSONAL_API_KEY, ci: { GITLAB_CI: 'true', CI_SERVER_URL: 'https://gitlab.example.com' }, }, ])('stays quiet $name', async ({ personalApiKey, ci }) => { const stderr = vi.spyOn(process.stderr, 'write').mockImplementation(() => true) const build = await freshBuildHogEnv() build(testConfig({ personalApiKey }), ci) expect(stderr).not.toHaveBeenCalled() }) })