11import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest' ;
22import { openTemplateCommand } from '../../../src/commands/templates/open' ;
33import * as browser from '../../../src/lib/browser' ;
4+ import {
5+ captureTestEnv ,
6+ expectExit1 ,
7+ mockExitThrow ,
8+ setNonInteractive ,
9+ setupOutputSpies ,
10+ } from '../../helpers' ;
11+
12+ const templateId = '78261eea-8f8b-4381-83c6-79fa7120f1cf' ;
13+
14+ const mockGet = vi . fn ( async ( _id : string ) => ( {
15+ data : { object : 'template' as const , id : templateId } ,
16+ error : null as { message : string } | null ,
17+ } ) ) ;
18+
19+ vi . mock ( 'resend' , ( ) => ( {
20+ Resend : class MockResend {
21+ constructor ( public key : string ) { }
22+ templates = { get : mockGet } ;
23+ } ,
24+ } ) ) ;
425
526describe ( 'templates open command' , ( ) => {
27+ const restoreEnv = captureTestEnv ( ) ;
28+
629 beforeEach ( ( ) => {
30+ process . env . RESEND_API_KEY = 're_test_key' ;
731 vi . spyOn ( browser , 'openInBrowserOrLog' ) . mockResolvedValue ( ) ;
32+ mockGet . mockClear ( ) ;
833 } ) ;
934
1035 afterEach ( ( ) => {
36+ restoreEnv ( ) ;
1137 vi . restoreAllMocks ( ) ;
1238 } ) ;
1339
@@ -21,16 +47,38 @@ describe('templates open command', () => {
2147 ) ;
2248 } ) ;
2349
24- it ( 'with id opens template URL' , async ( ) => {
25- await openTemplateCommand . parseAsync (
26- [ '78261eea-8f8b-4381-83c6-79fa7120f1cf' ] ,
27- { from : 'user' } ,
50+ it ( 'with id opens template URL without an API call' , async ( ) => {
51+ await openTemplateCommand . parseAsync ( [ templateId ] , { from : 'user' } ) ;
52+
53+ expect ( mockGet ) . not . toHaveBeenCalled ( ) ;
54+ expect ( browser . openInBrowserOrLog ) . toHaveBeenCalledWith (
55+ browser . RESEND_URLS . template ( templateId ) ,
56+ expect . any ( Object ) ,
2857 ) ;
58+ } ) ;
2959
30- expect ( browser . openInBrowserOrLog ) . toHaveBeenCalledTimes ( 1 ) ;
60+ it ( 'with alias opens the URL of the template it resolves to' , async ( ) => {
61+ await openTemplateCommand . parseAsync ( [ 'welcome-email' ] , { from : 'user' } ) ;
62+
63+ expect ( mockGet ) . toHaveBeenCalledWith ( 'welcome-email' ) ;
3164 expect ( browser . openInBrowserOrLog ) . toHaveBeenCalledWith (
32- browser . RESEND_URLS . template ( '78261eea-8f8b-4381-83c6-79fa7120f1cf' ) ,
65+ browser . RESEND_URLS . template ( templateId ) ,
3366 expect . any ( Object ) ,
3467 ) ;
3568 } ) ;
69+
70+ it ( 'with unknown alias exits without opening the browser' , async ( ) => {
71+ setNonInteractive ( ) ;
72+ setupOutputSpies ( ) ;
73+ mockExitThrow ( ) ;
74+ mockGet . mockResolvedValueOnce ( {
75+ data : null as never ,
76+ error : { message : 'Template not found' } ,
77+ } ) ;
78+
79+ await expectExit1 ( ( ) =>
80+ openTemplateCommand . parseAsync ( [ 'missing-alias' ] , { from : 'user' } ) ,
81+ ) ;
82+ expect ( browser . openInBrowserOrLog ) . not . toHaveBeenCalled ( ) ;
83+ } ) ;
3684} ) ;
0 commit comments