Skip to content

Commit 22ffb04

Browse files
fix: add finally block to refresh access token (#1671)
1 parent 73e6544 commit 22ffb04

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/client/metadataApiDeploy.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,10 @@ const compileAABComponents = async (connection: Connection, aabComponents: Sourc
531531
];
532532
}
533533
throw error;
534+
} finally {
535+
// regardless of success or failure, we don't need the named user jwt access token anymore
536+
delete connection.accessToken;
537+
await connection.refreshAuth();
534538
}
535539
})
536540
);
@@ -546,11 +550,6 @@ const compileAABComponents = async (connection: Connection, aabComponents: Sourc
546550
message: `${EOL}${errors.join(EOL)}`,
547551
name: 'AgentCompilationError',
548552
});
549-
} else {
550-
// everything successfully compiled
551-
// stop using named user jwt access token
552-
delete connection.accessToken;
553-
await connection.refreshAuth();
554553
}
555554
};
556555

test/client/metadataApiDeploy.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,6 +1445,7 @@ describe('MetadataApiDeploy', () => {
14451445
const connection = await testOrg.getConnection();
14461446

14471447
const readFileStub = $$.SANDBOX.stub(fs.promises, 'readFile').resolves(agentContent);
1448+
const refreshAuthStub = $$.SANDBOX.stub(Connection.prototype, 'refreshAuth').resolves();
14481449

14491450
$$.SANDBOX.stub(connection, 'getConnectionOptions').returns({
14501451
accessToken: 'test-access-token',
@@ -1488,6 +1489,8 @@ describe('MetadataApiDeploy', () => {
14881489
expect(callCount).to.be.at.least(2);
14891490
// Verify URL uses production endpoint when SF_TEST_API is not set
14901491
expect(compileUrl).to.equal('https://api.salesforce.com/einstein/ai-agent/v1.1/authoring/scripts');
1492+
// Regardless of success or failure, compileAABComponents should refresh auth.
1493+
expect(refreshAuthStub.calledOnce).to.be.true;
14911494
});
14921495

14931496
it('should not throw error when compilation succeeds', async () => {
@@ -1496,6 +1499,7 @@ describe('MetadataApiDeploy', () => {
14961499

14971500
// Stub retrieveMaxApiVersion on prototype before getting connection
14981501
$$.SANDBOX.stub(Connection.prototype, 'retrieveMaxApiVersion').resolves('60.0');
1502+
const refreshAuthStub = $$.SANDBOX.stub(Connection.prototype, 'refreshAuth').resolves();
14991503
const connection = await testOrg.getConnection();
15001504

15011505
const readFileStub = $$.SANDBOX.stub(fs.promises, 'readFile').resolves(agentContent);
@@ -1530,6 +1534,8 @@ describe('MetadataApiDeploy', () => {
15301534
expect(callCount).to.be.at.least(2);
15311535
// Verify URL uses production endpoint when SF_TEST_API is not set
15321536
expect(compileUrl).to.equal('https://api.salesforce.com/einstein/ai-agent/v1.1/authoring/scripts');
1537+
// Regardless of success or failure, compileAABComponents should refresh auth.
1538+
expect(refreshAuthStub.calledOnce).to.be.true;
15331539
});
15341540

15351541
it('should not compile when no AABs present in component set', async () => {
@@ -1669,6 +1675,7 @@ describe('MetadataApiDeploy', () => {
16691675

16701676
// Stub retrieveMaxApiVersion on prototype before getting connection
16711677
$$.SANDBOX.stub(Connection.prototype, 'retrieveMaxApiVersion').resolves('60.0');
1678+
const refreshAuthStub = $$.SANDBOX.stub(Connection.prototype, 'refreshAuth').resolves();
16721679
const connection = await testOrg.getConnection();
16731680

16741681
const readFileStub = $$.SANDBOX.stub(fs.promises, 'readFile').resolves(agentContent);
@@ -1705,6 +1712,8 @@ describe('MetadataApiDeploy', () => {
17051712
expect((e as Error).name).to.equal('ERROR_HTTP_404');
17061713
expect((e as SfError).actions!.length).to.equal(2);
17071714
}
1715+
// Regardless of success or failure, compileAABComponents should refresh auth.
1716+
expect(refreshAuthStub.calledOnce).to.be.true;
17081717
});
17091718

17101719
it('should not compile when SF_AAB_COMPILATION=false', async () => {

0 commit comments

Comments
 (0)