|
17 | 17 | import { join } from 'node:path'; |
18 | 18 | import { readFileSync, writeFileSync } from 'node:fs'; |
19 | 19 | import { TestSession } from '@salesforce/cli-plugins-testkit'; |
20 | | -import { Connection, Org, User, UserFields } from '@salesforce/core'; |
| 20 | +import { Connection, Org, User, UserFields, StateAggregator } from '@salesforce/core'; |
21 | 21 | import { ComponentSetBuilder } from '@salesforce/source-deploy-retrieve'; |
22 | 22 | import { genUniqueString } from '@salesforce/cli-plugins-testkit'; |
23 | 23 | import { expect } from 'chai'; |
@@ -84,15 +84,30 @@ export async function initializeSharedContext(): Promise<SharedTestContext> { |
84 | 84 | } |
85 | 85 | console.log(`Testing with username: ${username}`); |
86 | 86 | } else { |
87 | | - // Get devhub org from session |
88 | | - const hubOrgInfo = session.orgs.get('hub'); |
89 | | - if (!hubOrgInfo?.username) { |
90 | | - throw new Error('Failed to get devhub username from TestSession. Please ensure a devhub is authenticated.'); |
| 87 | + // Get devhub org from StateAggregator (config) |
| 88 | + try { |
| 89 | + const stateAggregator = await StateAggregator.getInstance(); |
| 90 | + const orgs = stateAggregator.orgs.getAll(); |
| 91 | + |
| 92 | + // Find the devhub org (has isDevHub: true) |
| 93 | + const devHubOrg = orgs.find((org) => org.isDevHub === true); |
| 94 | + |
| 95 | + if (!devHubOrg?.username) { |
| 96 | + throw new Error('No devhub org found in config.'); |
| 97 | + } |
| 98 | + |
| 99 | + username = devHubOrg.username; |
| 100 | + defaultOrg = await Org.create({ aliasOrUsername: username }); |
| 101 | + connection = defaultOrg.getConnection(); |
| 102 | + console.log(`Using devhub for testing. Username: ${username}`); |
| 103 | + } catch (error) { |
| 104 | + throw new Error( |
| 105 | + 'Failed to get authenticated devhub org. ' + |
| 106 | + 'Please ensure a devhub is authenticated by running: sf org login web --alias <alias> --instance-url <url> --set-default-dev-hub ' + |
| 107 | + 'or set TESTKIT_ORG_USERNAME environment variable to use a specific org.\n' + |
| 108 | + `Original error: ${error instanceof Error ? error.message : String(error)}` |
| 109 | + ); |
91 | 110 | } |
92 | | - username = hubOrgInfo.username; |
93 | | - defaultOrg = await Org.create({ aliasOrUsername: username }); |
94 | | - connection = defaultOrg.getConnection(); |
95 | | - console.log(`Using devhub for testing. Username: ${username}`); |
96 | 111 | } |
97 | 112 |
|
98 | 113 | // assign the EinsteinGPTPromptTemplateManager to the scratch org admin user |
|
0 commit comments