|
| 1 | +/* |
| 2 | + * Copyright 2026, Salesforce, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +import * as fs from 'node:fs'; |
| 17 | +import * as path from 'node:path'; |
| 18 | +import { expect } from 'chai'; |
| 19 | +import { execCmd, TestSession } from '@salesforce/cli-plugins-testkit'; |
| 20 | +import { Org, SfProject } from '@salesforce/core'; |
| 21 | +import { Duration } from '@salesforce/kit'; |
| 22 | +import { PackageVersionCreateRequestResult } from '@salesforce/packaging'; |
| 23 | +import { FileDownloadEntry } from '../../../src/commands/package/version/retrieve.js'; |
| 24 | + |
| 25 | +let packageName: string; |
| 26 | +let devHubOrg: Org; |
| 27 | + |
| 28 | +describe('package:version:create with managed package (--generate-pkg-zip)', () => { |
| 29 | + let session: TestSession; |
| 30 | + let managedPackageId: string; |
| 31 | + |
| 32 | + before('setup managed package', async function () { |
| 33 | + this.timeout(Duration.minutes(5).milliseconds); |
| 34 | + |
| 35 | + session = await TestSession.create({ |
| 36 | + devhubAuthStrategy: 'AUTO', |
| 37 | + project: { name: 'managed-pkg-test' }, |
| 38 | + }); |
| 39 | + |
| 40 | + devHubOrg = await Org.create({ aliasOrUsername: session.hubOrg.username }); |
| 41 | + |
| 42 | + // Query for existing managed package |
| 43 | + const existingPkgQuery = ` |
| 44 | + SELECT Id, Name, NamespacePrefix |
| 45 | + FROM Package2 |
| 46 | + WHERE ContainerOptions = 'Managed' |
| 47 | + AND IsDeprecated = false |
| 48 | + AND Name = 'pnhmanaged' |
| 49 | + LIMIT 1 |
| 50 | + `; |
| 51 | + const existingPkgRecords = ( |
| 52 | + await devHubOrg |
| 53 | + .getConnection() |
| 54 | + .tooling.query<{ Id: string; Name: string; NamespacePrefix: string }>(existingPkgQuery) |
| 55 | + ).records; |
| 56 | + |
| 57 | + managedPackageId = existingPkgRecords[0].Id; |
| 58 | + |
| 59 | + // Create minimal source structure for a custom object |
| 60 | + const objectName = 'TestObject__c'; |
| 61 | + const objectDir = path.join(session.project.dir, 'force-app', 'main', 'default', 'objects', objectName); |
| 62 | + fs.mkdirSync(objectDir, { recursive: true }); |
| 63 | + |
| 64 | + fs.writeFileSync( |
| 65 | + path.join(objectDir, `${objectName}.object-meta.xml`), |
| 66 | + `<?xml version="1.0" encoding="UTF-8"?> |
| 67 | +<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata"> |
| 68 | + <deploymentStatus>Deployed</deploymentStatus> |
| 69 | + <label>Test Object</label> |
| 70 | + <nameField> |
| 71 | + <label>Test Object Name</label> |
| 72 | + <type>Text</type> |
| 73 | + </nameField> |
| 74 | + <pluralLabel>Test Objects</pluralLabel> |
| 75 | + <sharingModel>ReadWrite</sharingModel> |
| 76 | +</CustomObject>` |
| 77 | + ); |
| 78 | + |
| 79 | + // Configure the project for the managed package |
| 80 | + const project = await SfProject.resolve(session.project.dir); |
| 81 | + const projectJson = project.getSfProjectJson(); |
| 82 | + const namespacePrefix = existingPkgRecords[0].NamespacePrefix; |
| 83 | + packageName = existingPkgRecords[0].Name; |
| 84 | + |
| 85 | + const packageDirectory = { |
| 86 | + path: 'force-app', |
| 87 | + package: existingPkgRecords[0].Name, |
| 88 | + versionNumber: '1.0.0.NEXT', |
| 89 | + ancestorVersion: 'NONE', |
| 90 | + versionName: 'v1', |
| 91 | + default: true, |
| 92 | + }; |
| 93 | + |
| 94 | + projectJson.set('packageDirectories', [packageDirectory]); |
| 95 | + projectJson.set('packageAliases', { [packageName]: managedPackageId }); |
| 96 | + projectJson.set('namespace', namespacePrefix); |
| 97 | + projectJson.writeSync(); |
| 98 | + }); |
| 99 | + |
| 100 | + after(async () => { |
| 101 | + await session?.clean(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should create a managed package version with --generate-pkg-zip flag and retrieve the metadata', async function () { |
| 105 | + this.timeout(Duration.minutes(25).milliseconds); |
| 106 | + |
| 107 | + const createResult = execCmd<PackageVersionCreateRequestResult>( |
| 108 | + `package:version:create --package ${packageName} --wait 20 -x --generate-pkg-zip --version-description "Test pkg zip" --json --skip-ancestor-check`, |
| 109 | + { ensureExitCode: 0, timeout: Duration.minutes(20).milliseconds } |
| 110 | + ).jsonOutput?.result; |
| 111 | + |
| 112 | + // Debug: verify the flag was actually set |
| 113 | + const pvcRequestId = createResult?.Id; |
| 114 | + const verifyQuery = `SELECT Id, IsDevUsePkgZipRequested FROM Package2VersionCreateRequest WHERE Id = '${pvcRequestId}'`; |
| 115 | + const verifyResult = await devHubOrg |
| 116 | + .getConnection() |
| 117 | + .tooling.query<{ Id: string; IsDevUsePkgZipRequested: boolean }>(verifyQuery); |
| 118 | + // eslint-disable-next-line no-console |
| 119 | + console.log('IsDevUsePkgZipRequested:', verifyResult.records[0]?.IsDevUsePkgZipRequested); |
| 120 | + |
| 121 | + expect(createResult?.Status).to.equal('Success'); |
| 122 | + const subscriberPkgVersionId = createResult?.SubscriberPackageVersionId; |
| 123 | + expect(subscriberPkgVersionId).to.match(/04t.{15}/); |
| 124 | + |
| 125 | + const retrieveOutputDir = 'pkg-zip-test-output'; |
| 126 | + const retrieveResult = execCmd<FileDownloadEntry[]>( |
| 127 | + `package:version:retrieve --package ${subscriberPkgVersionId} --output-dir ${retrieveOutputDir} --json`, |
| 128 | + { ensureExitCode: 0 } |
| 129 | + ).jsonOutput?.result; |
| 130 | + |
| 131 | + expect(retrieveResult).to.be.an('array'); |
| 132 | + expect(retrieveResult?.length).to.be.greaterThan(0); |
| 133 | + }); |
| 134 | +}); |
0 commit comments