|
| 1 | +import { readFileSync } from 'node:fs' |
| 2 | +import { runInNewContext } from 'node:vm' |
| 3 | +import { expect, it } from 'vitest' |
| 4 | +import { parse } from 'yaml' |
| 5 | +import { compileAggregateExpression } from './pr-aggregate-gate-context.mjs' |
| 6 | + |
| 7 | +const workflow = parse(readFileSync('.github/workflows/node-server-tests.yml', 'utf8')) |
| 8 | +const persistence = workflow.jobs.persistence |
| 9 | +const shouldPersist = compileAggregateExpression(persistence.if, ['needs', 'github']) |
| 10 | + |
| 11 | +function context({ qualification, producer = 'success', event = 'pull_request', draft = false }) { |
| 12 | + return { |
| 13 | + github: { event_name: event, event: { pull_request: { draft } } }, |
| 14 | + needs: { |
| 15 | + changes: { outputs: { qualification } }, |
| 16 | + mobile_web_bundle: { result: producer } |
| 17 | + } |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +it.each([ |
| 22 | + { qualification: 'true', producer: 'success', expected: true }, |
| 23 | + { qualification: '', producer: 'success', expected: true }, |
| 24 | + { qualification: 'true', producer: 'failure', expected: false }, |
| 25 | + { qualification: 'true', producer: 'cancelled', expected: false }, |
| 26 | + { qualification: 'true', producer: 'skipped', expected: false }, |
| 27 | + { qualification: '', producer: 'skipped', expected: false }, |
| 28 | + { qualification: 'false', producer: 'skipped', expected: true }, |
| 29 | + { qualification: 'false', producer: 'skipped', draft: true, expected: false }, |
| 30 | + { producer: 'success', event: 'workflow_dispatch', expected: true }, |
| 31 | + { producer: 'success', event: 'schedule', expected: true } |
| 32 | +])('preserves mobile producer failure and test-only behavior: %j', (scenario) => { |
| 33 | + const ctx = context(scenario) |
| 34 | + expect(persistence.needs).toEqual(['changes', 'mobile_web_bundle']) |
| 35 | + expect(shouldPersist(ctx).coerceString()).toBe(String(scenario.expected)) |
| 36 | + expect(shouldPersist(ctx, true).coerceString()).toBe('false') |
| 37 | +}) |
| 38 | + |
| 39 | +it('uses the shared page for qualification and builds locally only when its producer is skipped', () => { |
| 40 | + const download = persistence.steps.find((step) => step.with?.name === 'orcad-mobile-web-bundle') |
| 41 | + const build = persistence.steps.find((step) => step.uses?.includes('build-mobile-web-bundle')) |
| 42 | + const serverBuild = persistence.steps.find((step) => step.run === 'pnpm build:orcad') |
| 43 | + expect(download.uses).toBe('actions/download-artifact@v8') |
| 44 | + expect(download.with.path).toBe('out/mobile-web') |
| 45 | + expect(persistence.steps.indexOf(download)).toBeLessThan(persistence.steps.indexOf(serverBuild)) |
| 46 | + expect(persistence.steps.indexOf(build)).toBeLessThan(persistence.steps.indexOf(serverBuild)) |
| 47 | + for (const qualification of ['true', '', undefined, 'false']) { |
| 48 | + const ctx = context({ qualification }) |
| 49 | + expect(compileAggregateExpression(download.if)(ctx).coerceString()).toBe( |
| 50 | + String(qualification !== 'false') |
| 51 | + ) |
| 52 | + expect(compileAggregateExpression(build.if)(ctx).coerceString()).toBe( |
| 53 | + String(qualification === 'false') |
| 54 | + ) |
| 55 | + } |
| 56 | + expect(serverBuild['continue-on-error']).toBeUndefined() |
| 57 | +}) |
| 58 | + |
| 59 | +it('starts independent Linux qualification without waiting for another platform', () => { |
| 60 | + for (const name of ['linux_glibc_floor', 'linux_glibc217_compat', 'linux_musl']) { |
| 61 | + const job = workflow.jobs[name] |
| 62 | + expect([job.needs].flat()).not.toContain('persistence') |
| 63 | + expect(job.if).not.toContain('needs.persistence') |
| 64 | + const evaluate = compileAggregateExpression(job.if, ['needs', 'github', 'inputs']) |
| 65 | + const ctx = { ...context({ qualification: 'true' }), inputs: { build_template: true } } |
| 66 | + expect(evaluate(ctx).coerceString()).toBe('true') |
| 67 | + expect(evaluate(ctx, true).coerceString()).toBe('false') |
| 68 | + const draft = { |
| 69 | + ...context({ qualification: '', draft: true, producer: 'skipped' }), |
| 70 | + inputs: { build_template: false } |
| 71 | + } |
| 72 | + expect(evaluate(draft).coerceString()).toBe('false') |
| 73 | + } |
| 74 | +}) |
| 75 | + |
| 76 | +it('requires every platform and compatibility result before publishing a desktop template', () => { |
| 77 | + const template = workflow.jobs.desktop_template |
| 78 | + const required = [ |
| 79 | + 'persistence', |
| 80 | + 'mobile_web_bundle', |
| 81 | + 'linux_glibc_floor', |
| 82 | + 'linux_glibc217_compat', |
| 83 | + 'linux_musl' |
| 84 | + ] |
| 85 | + expect(template.needs).toEqual(required) |
| 86 | + const evaluate = compileAggregateExpression(template.if, ['needs', 'inputs']) |
| 87 | + const needs = Object.fromEntries(required.map((name) => [name, { result: 'success' }])) |
| 88 | + const ctx = { needs, inputs: { build_template: true } } |
| 89 | + expect(evaluate(ctx).coerceString()).toBe('true') |
| 90 | + expect(evaluate(ctx, true).coerceString()).toBe('false') |
| 91 | + for (const name of required) { |
| 92 | + for (const result of ['failure', 'cancelled', 'skipped']) { |
| 93 | + expect(evaluate({ ...ctx, needs: { ...needs, [name]: { result } } }).coerceString()).toBe( |
| 94 | + 'false' |
| 95 | + ) |
| 96 | + } |
| 97 | + } |
| 98 | +}) |
| 99 | + |
| 100 | +it('avoids template download-cache transfers while retaining native and verification caches', () => { |
| 101 | + const install = persistence.steps.find((step) => step.id === 'dependencies') |
| 102 | + expect(install.with['native-runtime']).toContain("runner.os == 'Windows' && 'node'") |
| 103 | + expect(install.with['cache-pnpm-verification']).toBeUndefined() |
| 104 | + expect(install.with['persist-native-cache']).toBeUndefined() |
| 105 | + const source = install.with['cache-pnpm-store'].slice(3, -2) |
| 106 | + for (const os of ['Linux', 'macOS', 'Windows']) { |
| 107 | + for (const buildTemplate of [true, false]) { |
| 108 | + expect( |
| 109 | + runInNewContext(source, { inputs: { build_template: buildTemplate }, runner: { os } }) |
| 110 | + ).toBe(!buildTemplate && os !== 'Windows') |
| 111 | + } |
| 112 | + } |
| 113 | +}) |
0 commit comments