@@ -332,11 +332,13 @@ describe('previewUtils', () => {
332332 } ) ;
333333
334334 it ( 'initializePreviewConnection succeeds with valid org' , async ( ) => {
335+ const mockConnection = {
336+ getUsername : ( ) => testUsername ,
337+ } ;
338+ const getConnectionStub = $$ . SANDBOX . stub ( ) . returns ( mockConnection ) ;
335339 const mockOrg = {
336- getConnection : ( ) => ( {
337- getUsername : ( ) => testUsername ,
338- } ) ,
339- } as Org ;
340+ getConnection : getConnectionStub ,
341+ } as unknown as Org ;
340342
341343 $$ . SANDBOX . stub ( OrgUtils , 'isLocalDevEnabled' ) . resolves ( true ) ;
342344 $$ . SANDBOX . stub ( OrgUtils , 'ensureMatchingAPIVersion' ) . returns ( ) ;
@@ -347,14 +349,38 @@ describe('previewUtils', () => {
347349 expect ( result . ldpServerId ) . to . equal ( testLdpServerId ) ;
348350 expect ( result . ldpServerToken ) . to . equal ( testLdpServerToken ) ;
349351 expect ( result . connection ) . to . exist ;
352+ expect ( getConnectionStub . calledWith ( undefined ) ) . to . be . true ;
353+ } ) ;
354+
355+ it ( 'initializePreviewConnection succeeds with valid org and apiVersion' , async ( ) => {
356+ const testApiVersion = '67.0' ;
357+ const mockConnection = {
358+ getUsername : ( ) => testUsername ,
359+ } ;
360+ const getConnectionStub = $$ . SANDBOX . stub ( ) . returns ( mockConnection ) ;
361+ const mockOrg = {
362+ getConnection : getConnectionStub ,
363+ } as unknown as Org ;
364+
365+ $$ . SANDBOX . stub ( OrgUtils , 'isLocalDevEnabled' ) . resolves ( true ) ;
366+ $$ . SANDBOX . stub ( OrgUtils , 'ensureMatchingAPIVersion' ) . returns ( ) ;
367+ $$ . SANDBOX . stub ( PreviewUtils , 'getOrCreateAppServerIdentity' ) . resolves ( testIdentityData ) ;
368+
369+ const result = await PreviewUtils . initializePreviewConnection ( mockOrg , testApiVersion ) ;
370+
371+ expect ( result . ldpServerId ) . to . equal ( testLdpServerId ) ;
372+ expect ( result . ldpServerToken ) . to . equal ( testLdpServerToken ) ;
373+ expect ( result . connection ) . to . exist ;
374+ expect ( getConnectionStub . calledWith ( testApiVersion ) ) . to . be . true ;
350375 } ) ;
351376
352377 it ( 'initializePreviewConnection rejects when username is not found' , async ( ) => {
378+ const mockConnection = {
379+ getUsername : ( ) => undefined ,
380+ } ;
353381 const mockOrg = {
354- getConnection : ( ) => ( {
355- getUsername : ( ) => undefined ,
356- } ) ,
357- } as Org ;
382+ getConnection : $$ . SANDBOX . stub ( ) . returns ( mockConnection ) ,
383+ } as unknown as Org ;
358384
359385 try {
360386 await PreviewUtils . initializePreviewConnection ( mockOrg ) ;
@@ -364,12 +390,32 @@ describe('previewUtils', () => {
364390 }
365391 } ) ;
366392
393+ it ( 'initializePreviewConnection rejects when username is not found with apiVersion' , async ( ) => {
394+ const testApiVersion = '67.0' ;
395+ const mockConnection = {
396+ getUsername : ( ) => undefined ,
397+ } ;
398+ const getConnectionStub = $$ . SANDBOX . stub ( ) . returns ( mockConnection ) ;
399+ const mockOrg = {
400+ getConnection : getConnectionStub ,
401+ } as unknown as Org ;
402+
403+ try {
404+ await PreviewUtils . initializePreviewConnection ( mockOrg , testApiVersion ) ;
405+ expect . fail ( 'Should have thrown an error' ) ;
406+ } catch ( error ) {
407+ expect ( ( error as Error ) . message ) . to . include ( 'Org must have a valid user' ) ;
408+ expect ( getConnectionStub . calledWith ( testApiVersion ) ) . to . be . true ;
409+ }
410+ } ) ;
411+
367412 it ( 'initializePreviewConnection rejects when local dev is not enabled' , async ( ) => {
413+ const mockConnection = {
414+ getUsername : ( ) => testUsername ,
415+ } ;
368416 const mockOrg = {
369- getConnection : ( ) => ( {
370- getUsername : ( ) => testUsername ,
371- } ) ,
372- } as Org ;
417+ getConnection : $$ . SANDBOX . stub ( ) . returns ( mockConnection ) ,
418+ } as unknown as Org ;
373419
374420 $$ . SANDBOX . stub ( OrgUtils , 'ensureMatchingAPIVersion' ) . returns ( ) ;
375421 $$ . SANDBOX . stub ( PreviewUtils , 'getOrCreateAppServerIdentity' ) . rejects (
@@ -384,12 +430,37 @@ describe('previewUtils', () => {
384430 }
385431 } ) ;
386432
433+ it ( 'initializePreviewConnection rejects when local dev is not enabled with apiVersion' , async ( ) => {
434+ const testApiVersion = '67.0' ;
435+ const mockConnection = {
436+ getUsername : ( ) => testUsername ,
437+ } ;
438+ const getConnectionStub = $$ . SANDBOX . stub ( ) . returns ( mockConnection ) ;
439+ const mockOrg = {
440+ getConnection : getConnectionStub ,
441+ } as unknown as Org ;
442+
443+ $$ . SANDBOX . stub ( OrgUtils , 'ensureMatchingAPIVersion' ) . returns ( ) ;
444+ $$ . SANDBOX . stub ( PreviewUtils , 'getOrCreateAppServerIdentity' ) . rejects (
445+ new Error ( sharedMessages . getMessage ( 'error.localdev.not.enabled' ) )
446+ ) ;
447+
448+ try {
449+ await PreviewUtils . initializePreviewConnection ( mockOrg , testApiVersion ) ;
450+ expect . fail ( 'Should have thrown an error' ) ;
451+ } catch ( error ) {
452+ expect ( ( error as Error ) . message ) . to . include ( 'Local Dev is not enabled' ) ;
453+ expect ( getConnectionStub . calledWith ( testApiVersion ) ) . to . be . true ;
454+ }
455+ } ) ;
456+
387457 it ( 'initializePreviewConnection rejects when ldpServerId is not found' , async ( ) => {
458+ const mockConnection = {
459+ getUsername : ( ) => testUsername ,
460+ } ;
388461 const mockOrg = {
389- getConnection : ( ) => ( {
390- getUsername : ( ) => testUsername ,
391- } ) ,
392- } as Org ;
462+ getConnection : $$ . SANDBOX . stub ( ) . returns ( mockConnection ) ,
463+ } as unknown as Org ;
393464
394465 const identityDataWithoutEntityId = {
395466 identityToken : testLdpServerToken ,
@@ -407,4 +478,32 @@ describe('previewUtils', () => {
407478 expect ( ( error as Error ) . message ) . to . include ( 'entity ID' ) ;
408479 }
409480 } ) ;
481+
482+ it ( 'initializePreviewConnection rejects when ldpServerId is not found with apiVersion' , async ( ) => {
483+ const testApiVersion = '67.0' ;
484+ const mockConnection = {
485+ getUsername : ( ) => testUsername ,
486+ } ;
487+ const getConnectionStub = $$ . SANDBOX . stub ( ) . returns ( mockConnection ) ;
488+ const mockOrg = {
489+ getConnection : getConnectionStub ,
490+ } as unknown as Org ;
491+
492+ const identityDataWithoutEntityId = {
493+ identityToken : testLdpServerToken ,
494+ usernameToServerEntityIdMap : { } ,
495+ } ;
496+
497+ $$ . SANDBOX . stub ( OrgUtils , 'isLocalDevEnabled' ) . resolves ( true ) ;
498+ $$ . SANDBOX . stub ( OrgUtils , 'ensureMatchingAPIVersion' ) . returns ( ) ;
499+ $$ . SANDBOX . stub ( PreviewUtils , 'getOrCreateAppServerIdentity' ) . resolves ( identityDataWithoutEntityId ) ;
500+
501+ try {
502+ await PreviewUtils . initializePreviewConnection ( mockOrg , testApiVersion ) ;
503+ expect . fail ( 'Should have thrown an error' ) ;
504+ } catch ( error ) {
505+ expect ( ( error as Error ) . message ) . to . include ( 'entity ID' ) ;
506+ expect ( getConnectionStub . calledWith ( testApiVersion ) ) . to . be . true ;
507+ }
508+ } ) ;
410509} ) ;
0 commit comments