From 127ae428ece08a8fa9da0fdbf0f337f3be13aaa2 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 31 Aug 2026 11:15:53 -0700 Subject: [PATCH] test(tooling): reuse Periphery workflow code fixtures (#134350) --- .../ios-periphery-comment-workflow.test.ts | 39 ++++++------------- .../scripts/periphery-scope-workflows.test.ts | 38 ++++++++++-------- 2 files changed, 33 insertions(+), 44 deletions(-) diff --git a/test/scripts/ios-periphery-comment-workflow.test.ts b/test/scripts/ios-periphery-comment-workflow.test.ts index 8163edba49a6..e2fb8ba315fe 100644 --- a/test/scripts/ios-periphery-comment-workflow.test.ts +++ b/test/scripts/ios-periphery-comment-workflow.test.ts @@ -2,7 +2,7 @@ import { Buffer } from "node:buffer"; import { readFileSync } from "node:fs"; import { createRequire } from "node:module"; import { compileFunction } from "node:vm"; -import { deflateRawSync } from "node:zlib"; +import { crc32, deflateRawSync } from "node:zlib"; import { describe, expect, it } from "vitest"; import { parse } from "yaml"; import { markdownToIR } from "../../packages/markdown-core/src/ir.js"; @@ -76,7 +76,7 @@ type WorkflowRun = { workflow_id: number; }; -function commenterScript(): string { +function readCommenterScript(): string { const workflow = parse(readFileSync(WORKFLOW_PATH, "utf8")) as Workflow; const step = workflow.jobs?.comment?.steps?.find( (candidate) => candidate.name === "Upsert Periphery PR comment", @@ -88,6 +88,14 @@ function commenterScript(): string { return script; } +const commenterScript = readCommenterScript(); +const executeCommenter = compileFunction(`return (async () => {\n${commenterScript}\n})();`, [ + "require", + "context", + "core", + "github", +]) as (require: NodeJS.Require, context: unknown, core: unknown, github: unknown) => Promise; + async function runCommenter( artifact: Artifact, archiveData: Buffer, @@ -103,7 +111,6 @@ async function runCommenter( workflowRuns?: WorkflowRun[]; } = {}, ) { - const script = commenterScript(); const core = { infos: [] as string[], warnings: [] as string[], @@ -225,19 +232,8 @@ async function runCommenter( repo: "openclaw", }, }; - const execute = compileFunction(`return (async () => {\n${script}\n})();`, [ - "require", - "context", - "core", - "github", - ]) as ( - require: NodeJS.Require, - context: unknown, - core: unknown, - github: unknown, - ) => Promise; - await execute(createRequire(import.meta.url), context, core, github); + await executeCommenter(createRequire(import.meta.url), context, core, github); return { artifactListCount, @@ -255,17 +251,6 @@ function expectUnavailableComment(bodies: string[]): void { expect(bodies[0]).toContain("Periphery did not complete or its report could not be safely read."); } -function crc32(input: Buffer): number { - let crc = 0xffffffff; - for (const byte of input) { - crc ^= byte; - for (let bit = 0; bit < 8; bit += 1) { - crc = (crc >>> 1) ^ (0xedb88320 & -(crc & 1)); - } - } - return (crc ^ 0xffffffff) >>> 0; -} - function u16(value: number): Buffer { const buffer = Buffer.alloc(2); buffer.writeUInt16LE(value); @@ -375,7 +360,7 @@ function setFirstEntryUncompressedSize(archive: Buffer, size: number): Buffer { describe("iOS Periphery comment workflow", () => { it("parses the workflow YAML and embedded github-script JavaScript", () => { - const script = commenterScript(); + const script = commenterScript; expect(script).not.toContain("node:child_process"); expect(script).not.toContain("execFileSync"); expect(() => diff --git a/test/scripts/periphery-scope-workflows.test.ts b/test/scripts/periphery-scope-workflows.test.ts index d4295185897b..aff852660b35 100644 --- a/test/scripts/periphery-scope-workflows.test.ts +++ b/test/scripts/periphery-scope-workflows.test.ts @@ -64,16 +64,28 @@ function readWorkflow(workflowPath: string): ScopeWorkflow { return parse(readFileSync(workflowPath, "utf8")) as ScopeWorkflow; } -function scopeScript(workflowPath: string): string { - const step = readWorkflow(workflowPath).jobs?.scope?.steps?.find( - (candidate) => candidate.id === "scope", - ); - if (!step?.with?.script) { +function compileScopeWorkflow(workflowPath: string) { + const workflow = readWorkflow(workflowPath); + const step = workflow.jobs?.scope?.steps?.find((candidate) => candidate.id === "scope"); + const script = step?.with?.script; + if (!script) { throw new Error(`missing Periphery scope script in ${workflowPath}`); } - return step.with.script; + const execute = compileFunction(`return (async () => {\n${script}\n})();`, [ + "context", + "core", + "exec", + ]) as (context: unknown, core: unknown, exec: unknown) => Promise; + return { workflow, script, execute }; } +const scopeWorkflows = new Map>( + WORKFLOW_CASES.map(({ path: workflowPath }) => [ + workflowPath, + compileScopeWorkflow(workflowPath), + ]), +); + async function runScope(workflowPath: string, options: ScopeOptions): Promise { const outputs = new Map(); const context = { @@ -102,11 +114,7 @@ async function runScope(workflowPath: string, options: ScopeOptions): Promise {\n${scopeScript(workflowPath)}\n})();`, [ - "context", - "core", - "exec", - ]) as (context: unknown, core: unknown, exec: unknown) => Promise; + const { execute } = scopeWorkflows.get(workflowPath)!; await execute( context, @@ -134,10 +142,9 @@ describe("Periphery scope workflows", () => { it.each(WORKFLOW_CASES)( "uses the synthetic merge parent for $name scope", ({ path: workflowPath }) => { - const workflow = readWorkflow(workflowPath); + const { workflow, script } = scopeWorkflows.get(workflowPath)!; const steps = workflow.jobs?.scope?.steps ?? []; const checkout = steps.find((step) => step.name === "Checkout"); - const script = scopeScript(workflowPath); expect(workflow.on?.pull_request?.types).toContain("converted_to_draft"); expect(workflow.on?.pull_request?.paths).toBeUndefined(); @@ -210,10 +217,7 @@ describe("Periphery scope workflows", () => { expect(oldDiff.status).toBe(1); const outputs = new Map(); - const execute = compileFunction( - `return (async () => {\n${scopeScript(".github/workflows/shared-openclawkit-periphery.yml")}\n})();`, - ["context", "core", "exec"], - ) as (context: unknown, core: unknown, exec: unknown) => Promise; + const { execute } = scopeWorkflows.get(".github/workflows/shared-openclawkit-periphery.yml")!; await execute( { eventName: "pull_request", payload: { pull_request: { draft: false, number: 123 } } }, { setOutput: (name: string, value: string) => outputs.set(name, value) },