@@ -10,6 +10,7 @@ import {
1010 isProductionFromSecretKey ,
1111 isPublishableKey ,
1212 parsePublishableKey ,
13+ publishableKeyFromHost ,
1314} from '../keys' ;
1415
1516describe ( 'buildPublishableKey(frontendApi)' , ( ) => {
@@ -245,6 +246,46 @@ describe('isProductionFromSecretKey(key)', () => {
245246 } ) ;
246247} ) ;
247248
249+ describe ( 'publishableKeyFromHost(host, fallbackKey?)' , ( ) => {
250+ it ( 'derives a pk_live_ key from a production hostname' , ( ) => {
251+ const result = publishableKeyFromHost ( 'example.com' ) ;
252+ expect ( result ) . toMatch ( / ^ p k _ l i v e _ / ) ;
253+ expect ( result ) . toBe ( buildPublishableKey ( 'clerk.example.com' ) ) ;
254+ } ) ;
255+
256+ it ( 'lowercases the host before deriving' , ( ) => {
257+ expect ( publishableKeyFromHost ( 'Example.COM' ) ) . toBe ( publishableKeyFromHost ( 'example.com' ) ) ;
258+ } ) ;
259+
260+ it ( 'returns the fallbackKey as-is when it is a development key' , ( ) => {
261+ const devKey = 'pk_test_Zm9vLWJhci0xMy5jbGVyay5hY2NvdW50cy5kZXYk' ;
262+ expect ( publishableKeyFromHost ( 'localhost' , devKey ) ) . toBe ( devKey ) ;
263+ } ) ;
264+
265+ it ( 'derives from host when fallbackKey is a production key' , ( ) => {
266+ const prodKey = 'pk_live_ZmFrZS1jbGVyay10ZXN0LmNsZXJrLmFjY291bnRzLmRldiQ=' ;
267+ const result = publishableKeyFromHost ( 'custom-domain.com' , prodKey ) ;
268+ expect ( result ) . toMatch ( / ^ p k _ l i v e _ / ) ;
269+ expect ( result ) . toBe ( buildPublishableKey ( 'clerk.custom-domain.com' ) ) ;
270+ } ) ;
271+
272+ it ( 'derives from host when no fallbackKey is provided' , ( ) => {
273+ expect ( publishableKeyFromHost ( 'custom-domain.com' ) ) . toBe ( buildPublishableKey ( 'clerk.custom-domain.com' ) ) ;
274+ } ) ;
275+
276+ it ( 'strips the port from the host before deriving' , ( ) => {
277+ expect ( publishableKeyFromHost ( 'example.com:3000' ) ) . toBe ( publishableKeyFromHost ( 'example.com' ) ) ;
278+ } ) ;
279+
280+ it ( 'strips the port even when combined with case normalization' , ( ) => {
281+ expect ( publishableKeyFromHost ( 'Example.COM:8080' ) ) . toBe ( publishableKeyFromHost ( 'example.com' ) ) ;
282+ } ) ;
283+
284+ it ( 'throws when host is empty' , ( ) => {
285+ expect ( ( ) => publishableKeyFromHost ( '' ) ) . toThrow ( 'Host must not be empty.' ) ;
286+ } ) ;
287+ } ) ;
288+
248289describe ( 'getCookieSuffix(publishableKey, subtle?)' , ( ) => {
249290 const cases : Array < [ string , string ] > = [
250291 [ 'pk_live_ZmFrZS1jbGVyay10ZXN0LmNsZXJrLmFjY291bnRzLmRldiQ=' , 'qReyu04C' ] ,
0 commit comments