mirror of
https://github.com/hanzo-apps/vote.git
synced 2026-10-30 20:00:39 +00:00
VITE_APP_AMPLITUDE_API_KEY is opt-in — commented out in .env and never
injected by CI, Docker, wrangler or vite.config. The guard tested
`=== ''`, which an ABSENT variable never matches, so Vite inlined the
read as `undefined`, folded the guard away as statically false, and the
shipped bundle called `amplitude.init(void 0, {defaultTracking:true})`.
The SDK came up with no API key and rejected every track() for the life
of the page. Confirmed in the deployed asset:
initAmplitude=()=>{init$4(void 0,{defaultTracking:!0})}
Guard on falsy so absent is as inert as empty, matching initHotjar's
`=== undefined` check on the same opt-in family next door. With the key
unset Vite now eliminates the init entirely; with one provisioned the
init is unchanged.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
45 lines
1.4 KiB
TypeScript
45 lines
1.4 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import * as amplitude from '@amplitude/analytics-browser';
|
|
import { initAmplitude } from '../src/insights/amplitude';
|
|
|
|
vi.mock('@amplitude/analytics-browser', () => ({ init: vi.fn() }));
|
|
|
|
const KEY = 'VITE_APP_AMPLITUDE_API_KEY';
|
|
const env = import.meta.env as unknown as Record<string, unknown>;
|
|
|
|
describe('initAmplitude', () => {
|
|
let original: unknown;
|
|
|
|
beforeEach(() => {
|
|
original = env[KEY];
|
|
vi.mocked(amplitude.init).mockClear();
|
|
});
|
|
|
|
afterEach(() => {
|
|
if (original === undefined) delete env[KEY];
|
|
else env[KEY] = original;
|
|
});
|
|
|
|
// The regression this file exists for: the guard was `=== ''`, which an ABSENT
|
|
// variable never matches. VITE_APP_AMPLITUDE_API_KEY is opt-in and unprovisioned,
|
|
// so the shipped bundle called `init(undefined, …)` and the SDK rejected every
|
|
// event for a missing API key. Absent must be as inert as empty.
|
|
it('stays inert when the key is absent', () => {
|
|
delete env[KEY];
|
|
initAmplitude();
|
|
expect(amplitude.init).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('stays inert when the key is empty', () => {
|
|
env[KEY] = '';
|
|
initAmplitude();
|
|
expect(amplitude.init).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('initializes when the key is provisioned', () => {
|
|
env[KEY] = 'abc123realkey';
|
|
initAmplitude();
|
|
expect(amplitude.init).toHaveBeenCalledWith('abc123realkey', { defaultTracking: true });
|
|
});
|
|
});
|