File size: 13,748 Bytes
7a1ad33 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 | /**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { vi, describe, it, expect, beforeEach, afterEach } from 'vitest';
import { execSync } from 'node:child_process';
import { join } from 'node:path';
/**
* Helper function to run the patch-create-comment script with given parameters
*/
function runPatchCreateComment(args, env = {}) {
const scriptPath = join(
process.cwd(),
'scripts/releasing/patch-create-comment.js',
);
const fullEnv = {
...process.env,
LC_ALL: 'en_US.UTF-8',
LANG: 'en_US.UTF-8',
...env,
};
try {
const result = execSync(`node ${scriptPath} ${args}`, {
encoding: 'utf8',
env: fullEnv,
});
return { stdout: result, stderr: '', success: true };
} catch (error) {
return {
stdout: error.stdout || '',
stderr: error.stderr || '',
success: false,
code: error.status,
};
}
}
describe('patch-create-comment', () => {
beforeEach(() => {
vi.stubEnv();
// Always run in test mode to avoid GitHub API calls
vi.stubEnv('TEST_MODE', 'true');
});
afterEach(() => {
vi.clearAllMocks();
vi.unstubAllEnvs();
});
describe('Environment flag', () => {
it('can be overridden with a flag', () => {
vi.stubEnv('ENVIRONMENT', 'dev');
const result = runPatchCreateComment(
'--original-pr 8655 --exit-code 0 --environment prod --commit abc1234 --channel preview --repository google-gemini/gemini-cli --test',
);
expect(result.success).toBe(true);
expect(result.stdout).toContain('π **[Step 2/4] Patch PR Created!**');
expect(result.stdout).toContain('Environment**: `prod`');
});
it('reads from the ENVIRONMENT env variable', () => {
vi.stubEnv('ENVIRONMENT', 'dev');
const result = runPatchCreateComment(
'--original-pr 8655 --exit-code 0 --commit abc1234 --channel preview --repository google-gemini/gemini-cli --test',
);
expect(result.success).toBe(true);
expect(result.stdout).toContain('π **[Step 2/4] Patch PR Created!**');
expect(result.stdout).toContain('Environment**: `dev`');
});
it('fails if the ENVIRONMENT is bogus', () => {
vi.stubEnv('ENVIRONMENT', 'totally-bogus');
const result = runPatchCreateComment(
'--original-pr 8655 --exit-code 0 --commit abc1234 --channel preview --repository google-gemini/gemini-cli --test',
);
expect(result.success).toBe(false);
expect(result.stderr).toContain(
'Argument: environment, Given: "totally-bogus", Choices: "prod", "dev"',
);
});
it('defaults to prod if not specified', () => {
const result = runPatchCreateComment(
'--original-pr 8655 --exit-code 0 --commit abc1234 --channel preview --repository google-gemini/gemini-cli --test',
);
expect(result.success).toBe(true);
expect(result.stdout).toContain('π **[Step 2/4] Patch PR Created!**');
expect(result.stdout).toContain('Environment**: `prod`');
});
});
describe('Environment Variable vs File Reading', () => {
it('should prefer LOG_CONTENT environment variable over file', () => {
const result = runPatchCreateComment(
'--original-pr 8655 --exit-code 0 --commit abc1234 --channel preview --repository google-gemini/gemini-cli --test',
{
LOG_CONTENT:
'Creating hotfix branch hotfix/v0.5.3/preview/cherry-pick-abc1234 from release/v0.5.3',
},
);
expect(result.success).toBe(true);
expect(result.stdout).toContain('π **[Step 2/4] Patch PR Created!**');
expect(result.stdout).toContain('Channel**: `preview`');
expect(result.stdout).toContain('Commit**: `abc1234`');
});
it('should use empty log content when LOG_CONTENT is not set', () => {
const result = runPatchCreateComment(
'--original-pr 8655 --exit-code 1 --commit abc1234 --channel stable --repository google-gemini/gemini-cli --test',
{}, // No LOG_CONTENT env var
);
expect(result.success).toBe(true);
expect(result.stdout).toContain(
'β **[Step 2/4] Patch creation failed!**',
);
expect(result.stdout).toContain(
'There was an error creating the patch release',
);
});
});
describe('Log Content Parsing - Success Scenarios', () => {
it('should generate success comment for clean cherry-pick', () => {
const result = runPatchCreateComment(
'--original-pr 8655 --exit-code 0 --commit abc1234 --channel stable --repository google-gemini/gemini-cli --test',
{
LOG_CONTENT:
'Creating hotfix branch hotfix/v0.4.1/stable/cherry-pick-abc1234 from release/v0.4.1\nβ
Cherry-pick successful - no conflicts detected',
},
);
expect(result.success).toBe(true);
expect(result.stdout).toContain('π **[Step 2/4] Patch PR Created!**');
expect(result.stdout).toContain('Channel**: `stable`');
expect(result.stdout).toContain('Commit**: `abc1234`');
expect(result.stdout).not.toContain('β οΈ Status');
});
it('should generate conflict comment for cherry-pick with conflicts', () => {
const result = runPatchCreateComment(
'--original-pr 8655 --exit-code 0 --commit def5678 --channel preview --repository google-gemini/gemini-cli --test',
{
LOG_CONTENT:
'Creating hotfix branch hotfix/v0.5.0-preview.2/preview/cherry-pick-def5678 from release/v0.5.0-preview.2\nCherry-pick has conflicts in 2 file(s):\nCONFLICT (content): Merge conflict in package.json',
},
);
expect(result.success).toBe(true);
expect(result.stdout).toContain('π **[Step 2/4] Patch PR Created!**');
expect(result.stdout).toContain(
'β οΈ Status**: Cherry-pick conflicts detected',
);
expect(result.stdout).toContain(
'β οΈ **Resolve conflicts** in the hotfix PR first',
);
expect(result.stdout).toContain('Channel**: `preview`');
});
});
describe('Log Content Parsing - Existing PR Scenarios', () => {
it('should detect existing PR and generate appropriate comment', () => {
const result = runPatchCreateComment(
'--original-pr 8655 --exit-code 0 --commit ghi9012 --channel stable --repository google-gemini/gemini-cli --test',
{
LOG_CONTENT:
'Hotfix branch hotfix/v0.4.1/stable/cherry-pick-ghi9012 already has an open PR.\nFound existing PR #8700: https://github.com/google-gemini/gemini-cli/pull/8700',
},
);
expect(result.success).toBe(true);
expect(result.stdout).toContain(
'βΉοΈ **[Step 2/4] Patch PR already exists!**',
);
expect(result.stdout).toContain(
'A patch PR for this change already exists: [#8700](https://github.com/google-gemini/gemini-cli/pull/8700)',
);
expect(result.stdout).toContain(
'Review and approve the existing patch PR',
);
});
it('should detect branch exists but no PR scenario', () => {
const result = runPatchCreateComment(
'--original-pr 8655 --exit-code 0 --commit jkl3456 --channel preview --repository google-gemini/gemini-cli --test',
{
LOG_CONTENT:
'Hotfix branch hotfix/v0.5.0-preview.2/preview/cherry-pick-jkl3456 exists but has no open PR.\nHotfix branch hotfix/v0.5.0-preview.2/preview/cherry-pick-jkl3456 already exists.',
},
);
expect(result.success).toBe(true);
expect(result.stdout).toContain(
'βΉοΈ **[Step 2/4] Patch branch exists but no PR found!**',
);
expect(result.stdout).toContain(
'Delete the branch: `git branch -D hotfix/v0.5.0-preview.2/preview/cherry-pick-jkl3456`',
);
expect(result.stdout).toContain('Run the patch command again');
});
});
describe('Log Content Parsing - Failure Scenarios', () => {
it('should generate failure comment when exit code is non-zero', () => {
const result = runPatchCreateComment(
'--original-pr 8655 --exit-code 1 --commit mno7890 --channel stable --repository google-gemini/gemini-cli --run-id 12345 --test',
{
LOG_CONTENT: 'Error: Failed to create patch',
},
);
expect(result.success).toBe(true);
expect(result.stdout).toContain(
'β **[Step 2/4] Patch creation failed!**',
);
expect(result.stdout).toContain(
'There was an error creating the patch release',
);
expect(result.stdout).toContain(
'View workflow run](https://github.com/google-gemini/gemini-cli/actions/runs/12345)',
);
});
it('should generate fallback failure comment when no output is generated', () => {
const result = runPatchCreateComment(
'--original-pr 8655 --exit-code 1 --commit pqr4567 --channel preview --repository google-gemini/gemini-cli --run-id 67890 --test',
{
LOG_CONTENT: '',
},
);
expect(result.success).toBe(true);
expect(result.stdout).toContain(
'β **[Step 2/4] Patch creation failed!**',
);
expect(result.stdout).toContain(
'There was an error creating the patch release',
);
});
});
describe('Channel and NPM Tag Detection', () => {
it('should correctly map stable channel to latest npm tag', () => {
const result = runPatchCreateComment(
'--original-pr 8655 --exit-code 0 --commit stu8901 --channel stable --repository google-gemini/gemini-cli --test',
{
LOG_CONTENT:
'Creating hotfix branch hotfix/v0.4.1/stable/cherry-pick-stu8901 from release/v0.4.1',
},
);
expect(result.success).toBe(true);
expect(result.stdout).toContain('will publish to npm tag `latest`');
});
it('should correctly map preview channel to preview npm tag', () => {
const result = runPatchCreateComment(
'--original-pr 8655 --exit-code 0 --commit vwx2345 --channel preview --repository google-gemini/gemini-cli --test',
{
LOG_CONTENT:
'Creating hotfix branch hotfix/v0.5.0-preview.2/preview/cherry-pick-vwx2345 from release/v0.5.0-preview.2',
},
);
expect(result.success).toBe(true);
expect(result.stdout).toContain('will publish to npm tag `preview`');
});
});
describe('No Original PR Scenario', () => {
it('should skip comment when no original PR is specified', () => {
const result = runPatchCreateComment(
'--original-pr 0 --exit-code 0 --commit yza6789 --channel stable --repository google-gemini/gemini-cli --test',
{
LOG_CONTENT:
'Creating hotfix branch hotfix/v0.4.1/stable/cherry-pick-yza6789 from release/v0.4.1',
ORIGINAL_PR: '', // Override with empty PR
},
);
expect(result.success).toBe(true);
expect(result.stdout).toContain(
'No original PR specified, skipping comment',
);
});
});
describe('Error Handling', () => {
it('should handle empty LOG_CONTENT gracefully', () => {
const result = runPatchCreateComment(
'--original-pr 8655 --exit-code 1 --commit bcd0123 --channel stable --repository google-gemini/gemini-cli --test',
{ LOG_CONTENT: '' }, // Empty log content
);
expect(result.success).toBe(true);
expect(result.stdout).toContain(
'β **[Step 2/4] Patch creation failed!**',
);
expect(result.stdout).toContain(
'There was an error creating the patch release',
);
});
});
describe('GitHub App Permission Scenarios', () => {
it('should parse manual commands with clipboard emoji correctly', () => {
const result = runPatchCreateComment(
'--original-pr 8655 --exit-code 1 --commit abc1234 --channel stable --repository google-gemini/gemini-cli --test',
{
LOG_CONTENT: `β Failed to create release branch due to insufficient GitHub App permissions.
π Please run these commands manually to create the branch:
\`\`\`bash
git checkout -b hotfix/v0.4.1/stable/cherry-pick-abc1234 v0.4.1
git push origin hotfix/v0.4.1/stable/cherry-pick-abc1234
\`\`\``,
},
);
expect(result.success).toBe(true);
expect(result.stdout).toContain(
'π **[Step 2/4] GitHub App Permission Issue**',
);
expect(result.stdout).toContain(
'Please run these commands manually to create the release branch:',
);
expect(result.stdout).toContain(
'git checkout -b hotfix/v0.4.1/stable/cherry-pick-abc1234 v0.4.1',
);
expect(result.stdout).toContain(
'git push origin hotfix/v0.4.1/stable/cherry-pick-abc1234',
);
});
});
describe('Test Mode Flag', () => {
it('should generate mock content in test mode for success', () => {
const result = runPatchCreateComment(
'--original-pr 8655 --exit-code 0 --commit efg4567 --channel preview --repository google-gemini/gemini-cli --test',
);
expect(result.success).toBe(true);
expect(result.stdout).toContain(
'π§ͺ TEST MODE - No API calls will be made',
);
expect(result.stdout).toContain('π **[Step 2/4] Patch PR Created!**');
});
it('should generate mock content in test mode for failure', () => {
const result = runPatchCreateComment(
'--original-pr 8655 --exit-code 1 --commit hij8901 --channel stable --repository google-gemini/gemini-cli --test',
);
expect(result.success).toBe(true);
expect(result.stdout).toContain(
'β **[Step 2/4] Patch creation failed!**',
);
});
});
});
|