11import { Config } from '@oclif/core' ;
22import { captureOutput , runCommand } from '@oclif/test' ;
3- import { PowerSyncManagementClient } from '@powersync/management-client' ;
43import { existsSync , mkdirSync , mkdtempSync , rmSync , writeFileSync } from 'node:fs' ;
54import { tmpdir } from 'node:os' ;
65import { join } from 'node:path' ;
7- import { afterEach , beforeAll , beforeEach , describe , expect , it , vi } from 'vitest' ;
6+ import { afterEach , beforeAll , beforeEach , describe , expect , it } from 'vitest' ;
87
98import FetchConfigCommand from '../../../src/commands/fetch/config.js' ;
109import { root } from '../../helpers/root.js' ;
10+ import { managementClientMock , resetManagementClientMocks } from '../../setup.js' ;
1111
1212const PROJECT_DIR = 'powersync' ;
1313const SERVICE_FILENAME = 'service.yaml' ;
1414
1515/** Minimal valid cloud config decodable by ServiceCloudConfig. */
1616const MOCK_CONFIG = { _type : 'cloud' as const , name : 'test-instance' , region : 'us' } ;
1717
18- const mockCloudClient = {
19- deployInstance : vi . fn ( ) ,
20- getInstanceConfig : vi . fn ( )
21- } ;
22-
2318function writeServiceYaml ( projectDir : string , type : 'cloud' | 'self-hosted' ) {
2419 writeFileSync ( join ( projectDir , SERVICE_FILENAME ) , `_type: ${ type } \nregion: us\n` , 'utf8' ) ;
2520}
@@ -38,16 +33,18 @@ describe('fetch config', () => {
3833 const args = [ '--directory' , directory ] ;
3934 if ( opts ?. output ) args . push ( '--output' , opts . output ) ;
4035 const cmd = new FetchConfigCommand ( args , oclifConfig ) ;
41- cmd . client = mockCloudClient as unknown as PowerSyncManagementClient ;
36+ cmd . client = managementClientMock as unknown as FetchConfigCommand [ 'client' ] ;
4237 return captureOutput ( ( ) => cmd . run ( ) ) ;
4338 }
4439
4540 beforeEach ( ( ) => {
41+ resetManagementClientMocks ( ) ;
42+
4643 origCwd = process . cwd ( ) ;
4744 tmpDir = mkdtempSync ( join ( tmpdir ( ) , 'fetch-config-test-' ) ) ;
4845 process . chdir ( tmpDir ) ;
49- mockCloudClient . getInstanceConfig . mockReset ( ) ;
50- mockCloudClient . getInstanceConfig . mockRejectedValue ( new Error ( 'network error' ) ) ;
46+ managementClientMock . getInstanceConfig . mockReset ( ) ;
47+ managementClientMock . getInstanceConfig . mockRejectedValue ( new Error ( 'network error' ) ) ;
5148 } ) ;
5249
5350 afterEach ( ( ) => {
@@ -89,7 +86,7 @@ describe('fetch config', () => {
8986 } ) ;
9087
9188 it ( 'default output is yaml and prints fetched object (config + optional syncRules) to stdout' , async ( ) => {
92- mockCloudClient . getInstanceConfig . mockResolvedValueOnce ( { config : MOCK_CONFIG } ) ;
89+ managementClientMock . getInstanceConfig . mockResolvedValueOnce ( { config : MOCK_CONFIG } ) ;
9390 const result = await runFetchConfigDirect ( ) ;
9491 expect ( result . error ) . toBeUndefined ( ) ;
9592 expect ( result . stdout ) . toContain ( 'config:' ) ;
@@ -98,7 +95,7 @@ describe('fetch config', () => {
9895 } ) ;
9996
10097 it ( '--output yaml prints fetched object as YAML to stdout' , async ( ) => {
101- mockCloudClient . getInstanceConfig . mockResolvedValueOnce ( { config : MOCK_CONFIG } ) ;
98+ managementClientMock . getInstanceConfig . mockResolvedValueOnce ( { config : MOCK_CONFIG } ) ;
10299 const result = await runFetchConfigDirect ( { output : 'yaml' } ) ;
103100 expect ( result . error ) . toBeUndefined ( ) ;
104101 expect ( result . stdout ) . toContain ( 'config:' ) ;
@@ -107,7 +104,7 @@ describe('fetch config', () => {
107104 } ) ;
108105
109106 it ( '--output json prints fetched object (config, syncRules) as JSON to stdout' , async ( ) => {
110- mockCloudClient . getInstanceConfig . mockResolvedValueOnce ( { config : MOCK_CONFIG } ) ;
107+ managementClientMock . getInstanceConfig . mockResolvedValueOnce ( { config : MOCK_CONFIG } ) ;
111108 const result = await runFetchConfigDirect ( { output : 'json' } ) ;
112109 expect ( result . error ) . toBeUndefined ( ) ;
113110 const parsed = JSON . parse ( result . stdout ) as { config : typeof MOCK_CONFIG } ;
@@ -116,7 +113,7 @@ describe('fetch config', () => {
116113
117114 it ( '--output json includes syncRules when returned' , async ( ) => {
118115 const syncRules = 'bucket_definitions: []\n' ;
119- mockCloudClient . getInstanceConfig . mockResolvedValueOnce ( {
116+ managementClientMock . getInstanceConfig . mockResolvedValueOnce ( {
120117 config : MOCK_CONFIG ,
121118 sync_rules : syncRules
122119 } as { config : typeof MOCK_CONFIG ; sync_rules : string } ) ;
0 commit comments