1+ import { filesystem , system } from 'gluegun' ;
2+
3+ const src = filesystem . path ( __dirname , '..' ) ;
4+
5+ const cli = async ( cmd : string ) =>
6+ system . run ( `node ${ filesystem . path ( src , 'bin' , 'lt' ) } ${ cmd } ` ) ;
7+
8+ export { } ;
9+
10+ // Directus commands require Docker or external services
11+ // These tests verify commands exist and handle missing dependencies gracefully
12+ // They do not create actual Directus instances or require running services
13+
14+ describe ( 'Directus Commands' , ( ) => {
15+ describe ( 'lt directus docker-setup' , ( ) => {
16+ test ( 'checks for Docker installation' , async ( ) => {
17+ try {
18+ const output = await cli ( 'directus docker-setup --noConfirm --name test-instance' ) ;
19+ // If Docker is installed, command should proceed (though may fail for other reasons)
20+ expect ( output ) . toBeDefined ( ) ;
21+ } catch ( e : any ) {
22+ // If Docker is not installed, should show error message
23+ const errorMsg = e . message || e . stderr || '' ;
24+ expect (
25+ errorMsg . includes ( 'Docker' ) ||
26+ errorMsg . includes ( 'docker' ) ||
27+ errorMsg . includes ( 'Instance name is required' )
28+ ) . toBe ( true ) ;
29+ }
30+ } ) ;
31+
32+ test ( 'requires instance name with --noConfirm' , async ( ) => {
33+ try {
34+ const output = await cli ( 'directus docker-setup --noConfirm' ) ;
35+ // Should fail because name is required with noConfirm
36+ expect ( output ) . toContain ( 'Instance name is required' ) ;
37+ } catch ( e : any ) {
38+ // Command might fail, but error should be related to missing name or Docker
39+ const errorMsg = e . message || e . stderr || '' ;
40+ expect ( errorMsg ) . toBeDefined ( ) ;
41+ }
42+ } ) ;
43+ } ) ;
44+
45+ describe ( 'lt directus remove' , ( ) => {
46+ test ( 'handles missing directus directory gracefully' , async ( ) => {
47+ try {
48+ const output = await cli ( 'directus remove non-existent-instance --noConfirm' ) ;
49+ // Should either report no instances found or instance not found
50+ expect (
51+ output . includes ( 'No Directus instances found' ) ||
52+ output . includes ( 'not found' )
53+ ) . toBe ( true ) ;
54+ } catch ( e : any ) {
55+ // Error is acceptable - command handles non-existent instances
56+ const errorMsg = e . message || e . stderr || '' ;
57+ expect ( errorMsg ) . toBeDefined ( ) ;
58+ }
59+ } ) ;
60+
61+ test ( 'requires instance name with --noConfirm' , async ( ) => {
62+ try {
63+ const output = await cli ( 'directus remove --noConfirm' ) ;
64+ // Should fail because name is required with noConfirm
65+ expect (
66+ output . includes ( 'Instance name is required' ) ||
67+ output . includes ( 'No Directus instances found' )
68+ ) . toBe ( true ) ;
69+ } catch ( e : any ) {
70+ const errorMsg = e . message || e . stderr || '' ;
71+ expect ( errorMsg ) . toBeDefined ( ) ;
72+ }
73+ } ) ;
74+ } ) ;
75+
76+ describe ( 'lt directus typegen' , ( ) => {
77+ test ( 'requires token with --noConfirm' , async ( ) => {
78+ try {
79+ const output = await cli ( 'directus typegen --noConfirm --url http://localhost:8055' ) ;
80+ // Should fail because token is required
81+ expect ( output ) . toContain ( 'token is required' ) ;
82+ } catch ( e : any ) {
83+ const errorMsg = e . message || e . stderr || '' ;
84+ expect ( errorMsg . includes ( 'token' ) ) . toBe ( true ) ;
85+ }
86+ } ) ;
87+
88+ test ( 'requires url with --noConfirm' , async ( ) => {
89+ try {
90+ const output = await cli ( 'directus typegen --noConfirm --token test-token' ) ;
91+ // Command should proceed with default URL or fail gracefully
92+ expect ( output ) . toBeDefined ( ) ;
93+ } catch ( e : any ) {
94+ // Expected to fail when connecting to Directus
95+ const errorMsg = e . message || e . stderr || '' ;
96+ expect ( errorMsg ) . toBeDefined ( ) ;
97+ }
98+ } ) ;
99+
100+ test ( 'handles invalid URL/token gracefully' , async ( ) => {
101+ // Use a temp directory for output to avoid creating files in project
102+ const tempDir = filesystem . path ( filesystem . homedir ( ) , `.lt-test-${ Date . now ( ) } ` ) ;
103+ filesystem . dir ( tempDir ) ;
104+ const outputFile = filesystem . path ( tempDir , 'test-schema.ts' ) ;
105+
106+ try {
107+ await cli (
108+ `directus typegen --noConfirm --url http://localhost:9999 --token invalid-token --output ${ outputFile } `
109+ ) ;
110+ } catch ( e : any ) {
111+ // Should fail when trying to connect to invalid Directus instance
112+ const errorMsg = e . message || e . stderr || '' ;
113+ expect (
114+ errorMsg . includes ( 'Failed' ) ||
115+ errorMsg . includes ( 'error' ) ||
116+ errorMsg . includes ( 'connect' )
117+ ) . toBe ( true ) ;
118+ } finally {
119+ filesystem . remove ( tempDir ) ;
120+ }
121+ } ) ;
122+ } ) ;
123+
124+ } ) ;
0 commit comments