diff --git a/src/lib/provision.ts b/src/lib/provision.ts index 53ac582..baeec5f 100644 --- a/src/lib/provision.ts +++ b/src/lib/provision.ts @@ -198,7 +198,10 @@ function normalizeAccount(raw: unknown): CloudAccount { return account; } if (typeof account.cloud_name === 'string' && account.cloud_name !== '') { - account.product_environments = [account]; + // Copy, not a self-reference: the account must stay JSON-serializable + // (--json output stringifies it). + const { product_environments: _omit, ...env } = account; + account.product_environments = [env]; return account; } throw new ProvisionError( diff --git a/test/provision.test.mjs b/test/provision.test.mjs index acdc715..c4efbf7 100644 --- a/test/provision.test.mjs +++ b/test/provision.test.mjs @@ -164,6 +164,24 @@ test('normalizes the flattened response shape', async () => { ); }); +test('flattened response normalizes without circular references (--json serializes it)', async () => { + await withStub( + (req, res) => { + req.on('data', () => {}); + req.on('end', () => { + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify(FLAT_RESPONSE)); + }); + }, + async host => { + const account = await provisionCloud({}, { apiHost: host }); + const serialized = JSON.parse(JSON.stringify(account)); + assert.equal(serialized.product_environments[0].cloud_name, 'cloud-flat'); + assert.equal(serialized.product_environments[0].product_environments, undefined); + }, + ); +}); + test('unrecognized success shape throws with the raw response preserved', async () => { await withStub( (req, res) => {