@@ -15,45 +15,28 @@ if (host === null) {
1515
1616const api = new XataApiClient ( { apiKey : process . env . XATA_API_KEY , host } ) ;
1717const region = process . env . XATA_REGION || 'eu-west-1' ;
18+ const clusterId = process . env . XATA_CLUSTER_ID ?? 'shared-cluster' ;
19+ const hash = Math . random ( ) . toString ( 36 ) . substr ( 2 , 9 ) ;
1820
19- const getWorkspaceName = ( ) => `sdk-integration-api-client-${ Math . random ( ) . toString ( 36 ) . substr ( 2 , 9 ) } ` ;
21+ // For shared-cluster, we create a new workspace with a unique name
22+ // while in dedicated clusters, we use the provided workspace name
23+ const workspaceName = clusterId === 'shared-cluster' ? `sdk-smoke-${ hash } ` : process . env . XATA_WORKSPACE ;
24+ if ( ! workspaceName ) throw new Error ( 'XATA_WORKSPACE environment variable is not set' ) ;
2025
2126describe ( 'API Client Integration Tests' , ( ) => {
2227 test ( 'Create, get and delete workspace with new apiKey' , async ( ) => {
23- const workspaceName = getWorkspaceName ( ) ;
24-
25- const newApiKey = await api . authentication . createUserAPIKey ( { pathParams : { keyName : `${ workspaceName } -key` } } ) ;
28+ const newApiKey = await api . authentication . createUserAPIKey ( { pathParams : { keyName : `smoke-${ hash } -key` } } ) ;
2629
2730 expect ( newApiKey ) . toBeDefined ( ) ;
28- expect ( newApiKey . name ) . toBe ( `${ workspaceName } -key` ) ;
31+ expect ( newApiKey . name ) . toBe ( `smoke- ${ hash } -key` ) ;
2932 expect ( newApiKey . key ) . toBeDefined ( ) ;
3033
34+ const workspace = await getOrCreateWorkspace ( workspaceName ) ;
3135 const newApi = new XataApiClient ( { apiKey : newApiKey . key , host } ) ;
3236
33- const { id : workspace , name } = await newApi . workspaces . createWorkspace ( {
34- body : { name : workspaceName , slug : `${ workspaceName } -slug` }
35- } ) ;
36-
37- await waitForReplication ( newApi , workspace ) ;
38-
39- expect ( workspace ) . toBeDefined ( ) ;
40- expect ( name ) . toBe ( workspaceName ) ;
41-
42- console . log ( 'Created workspace' , workspace ) ;
43-
44- const foo = await newApi . workspaces . getWorkspace ( { pathParams : { workspaceId : workspace } } ) ;
45-
46- expect ( foo . id ) . toBe ( workspace ) ;
47- expect ( foo . slug ) . toBe ( `${ workspaceName } -slug` ) ;
48-
49- const bar = await newApi . workspaces . getWorkspace ( { pathParams : { workspaceId : workspace } } ) ;
50-
51- expect ( bar . id ) . toBe ( workspace ) ;
52- expect ( bar . slug ) . toBe ( `${ workspaceName } -slug` ) ;
53-
5437 const { databaseName : database } = await newApi . databases . createDatabase ( {
55- pathParams : { workspaceId : workspace , dbName : `data-${ workspace } ` } ,
56- body : { region }
38+ pathParams : { workspaceId : workspace , dbName : `data-${ workspace } - ${ hash } ` } ,
39+ body : { region, defaultClusterID : clusterId }
5740 } ) ;
5841
5942 await waitForReplication ( newApi , workspace , database ) ;
@@ -122,14 +105,35 @@ describe('API Client Integration Tests', () => {
122105
123106 console . log ( 'Deleted API key, record is no longer accessible' ) ;
124107
125- await api . workspaces . deleteWorkspace ( { pathParams : { workspaceId : workspace } } ) ;
108+ if ( clusterId === 'shared-cluster' ) {
109+ await api . workspaces . deleteWorkspace ( { pathParams : { workspaceId : workspace } } ) ;
126110
127- await expect ( api . workspaces . getWorkspace ( { pathParams : { workspaceId : workspace } } ) ) . rejects . toHaveProperty (
128- 'message'
129- ) ;
111+ await expect ( api . workspaces . getWorkspace ( { pathParams : { workspaceId : workspace } } ) ) . rejects . toHaveProperty (
112+ 'message'
113+ ) ;
114+ }
130115 } ) ;
131116} ) ;
132117
118+ async function getOrCreateWorkspace ( workspaceName : string ) : Promise < string > {
119+ if ( clusterId === 'shared-cluster' ) {
120+ const { id : workspace , name } = await api . workspaces . createWorkspace ( {
121+ body : { name : workspaceName , slug : `${ workspaceName } -slug` }
122+ } ) ;
123+
124+ await waitForReplication ( api , workspace ) ;
125+
126+ expect ( workspace ) . toBeDefined ( ) ;
127+ expect ( name ) . toBe ( workspaceName ) ;
128+
129+ console . log ( 'Created workspace' , workspace ) ;
130+
131+ return workspace ;
132+ }
133+
134+ return workspaceName ;
135+ }
136+
133137async function waitForReplication ( api : XataApiClient , workspace : string , database ?: string ) : Promise < void > {
134138 try {
135139 if ( database === undefined ) {
0 commit comments