import { googleConfig, GOOGLE_CALLBACK_PATH } from '../../src/google/config'; const CREDENTIALS = { GOOGLE_CLIENT_ID: 'id.apps.googleusercontent.com', GOOGLE_CLIENT_SECRET: 'shh' }; /** * The redirect URI is compared by Google as an exact string, and a mismatch * answers `redirect_uri_mismatch` — accurate, and silent about which half is * wrong. So these assert the derivation itself rather than that the function * returns something, and most of them are about what must *not* end up in it. */ describe('googleConfig', () => { it('builds the redirect URI from PUBLIC_URL', () => { const config = googleConfig({ ...CREDENTIALS, PUBLIC_URL: 'https://redefined-designs.com' }); expect(config.redirectUri).toBe(`https://redefined-designs.com${GOOGLE_CALLBACK_PATH}`); }); it('normalises a trailing slash away, rather than producing a double slash', () => { // The failure this prevents is worth naming: "https://x.com//api/..." is a // different string from the one registered in the console, and it is an // easy way to write PUBLIC_URL. const config = googleConfig({ ...CREDENTIALS, PUBLIC_URL: 'https://redefined-designs.com/' }); expect(config.redirectUri).toBe(`https://redefined-designs.com${GOOGLE_CALLBACK_PATH}`); expect(config.redirectUri).not.toContain('//api'); }); it('discards a path on PUBLIC_URL', () => { const config = googleConfig({ ...CREDENTIALS, PUBLIC_URL: 'https://redefined-designs.com/shop' }); expect(config.redirectUri).toBe(`https://redefined-designs.com${GOOGLE_CALLBACK_PATH}`); }); it('keeps a non-default port, because the console entry carries one too', () => { const config = googleConfig({ ...CREDENTIALS, PUBLIC_URL: 'http://localhost:5173' }); expect(config.redirectUri).toBe(`http://localhost:5173${GOOGLE_CALLBACK_PATH}`); }); it('falls back to localhost when PUBLIC_URL is unset', () => { // Local development legitimately has no PUBLIC_URL: envValidation only // demands it alongside SMTP. localhost is also the one host Google accepts // without an authorized domain, so the fallback is the only value that // could work here anyway. const config = googleConfig({ ...CREDENTIALS }); expect(config.redirectUri).toBe(`http://localhost:3000${GOOGLE_CALLBACK_PATH}`); }); it('refuses a PUBLIC_URL that is not a URL', () => { expect(() => googleConfig({ ...CREDENTIALS, PUBLIC_URL: 'redefined-designs.com' })).toThrow( /not a URL/ ); }); describe('enabled', () => { it('is true only when both credentials are present', () => { expect(googleConfig(CREDENTIALS).enabled).toBe(true); }); it('is false when neither is set, which is a legitimate environment', () => { // A developer without credentials gets a storefront that works and does // not offer the button, rather than one that offers it and fails. expect(googleConfig({}).enabled).toBe(false); }); it.each([ ['only the id', { GOOGLE_CLIENT_ID: 'id' }], ['only the secret', { GOOGLE_CLIENT_SECRET: 'shh' }] ])('is false with %s', (_label, env) => { expect(googleConfig(env).enabled).toBe(false); }); it('treats whitespace as absent', () => { // A stack variable set to an empty string is the same as one never set, // and it is the shape a copied-and-blanked entry takes. expect(googleConfig({ GOOGLE_CLIENT_ID: ' ', GOOGLE_CLIENT_SECRET: 'shh' }).enabled).toBe(false); }); }); });