Repository navigation
Expand file tree
/
Copy pathcomputer-use-sync-reminder.test.js
More file actions
148 lines (130 loc) · 5.1 KB
/
Copy pathcomputer-use-sync-reminder.test.js
File metadata and controls
148 lines (130 loc) · 5.1 KB
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
"use strict";
const assert = require("node:assert/strict");
const fs = require("node:fs");
const path = require("node:path");
const test = require("node:test");
const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor;
const repositoryRoot = path.resolve(__dirname, "../..");
const workflowPath = path.join(
repositoryRoot,
".github/workflows/computer-use-sync-reminder.yml",
);
const policy = require(path.join(repositoryRoot, ".github/labels.json"));
function workflowScript() {
const lines = fs.readFileSync(workflowPath, "utf8").split("\n");
const marker = lines.findIndex((line) => line.trim() === "script: |");
assert.notEqual(marker, -1, "workflow must contain a github-script block");
return lines
.slice(marker + 1)
.map((line) => line.replace(/^ {12}/, ""))
.join("\n");
}
function harness({
getLabelError = Object.assign(new Error("missing"), { status: 404 }),
legacyIssue = { number: 944 },
} = {}) {
const calls = [];
const issues = {
addLabels: async (options) => calls.push(["addLabels", options]),
create: async (options) => calls.push(["create", options]),
createComment: async (options) => calls.push(["createComment", options]),
createLabel: async (options) => calls.push(["createLabel", options]),
getLabel: async (options) => {
calls.push(["getLabel", options]);
if (getLabelError) throw getLabelError;
},
listForRepo: async (options) => {
calls.push(["listForRepo", options]);
if (options.labels === "computer-use-sync") {
return { data: legacyIssue ? [legacyIssue] : [] };
}
return { data: [] };
},
};
return {
calls,
core: { notice: (message) => calls.push(["notice", message]) },
context: {
payload: { compare: "https://github.com/owner/repository/compare/old...new" },
repo: { owner: "owner", repo: "repository" },
sha: "0123456789abcdef",
},
github: { rest: { issues } },
};
}
async function runWorkflow(harnessValue) {
const originalWorkspace = process.env.GITHUB_WORKSPACE;
process.env.GITHUB_WORKSPACE = repositoryRoot;
const requirePolicy = (requestedPath) => {
assert.equal(requestedPath, path.join(repositoryRoot, ".github/labels.json"));
return policy;
};
try {
const execute = new AsyncFunction("github", "context", "require", "core", workflowScript());
await execute(
harnessValue.github,
harnessValue.context,
requirePolicy,
harnessValue.core,
);
} finally {
if (originalWorkspace === undefined) delete process.env.GITHUB_WORKSPACE;
else process.env.GITHUB_WORKSPACE = originalWorkspace;
}
}
test("sync reminder adopts the legacy open issue during label rollout", async () => {
const value = harness();
await runWorkflow(value);
assert.deepEqual(
value.calls
.filter(([operation]) => operation === "createLabel")
.map(([, options]) => options.name),
["type: maintenance", "area: computer use", "status: ready for work", "sync: computer use"],
);
const addLabels = value.calls.find(([operation]) => operation === "addLabels");
assert.deepEqual(addLabels[1], {
owner: "owner",
repo: "repository",
issue_number: 944,
labels: [
"type: maintenance",
"area: computer use",
"status: ready for work",
"sync: computer use",
],
});
const comment = value.calls.find(([operation]) => operation === "createComment");
assert.equal(comment[1].issue_number, 944);
assert.equal(value.calls.some(([operation]) => operation === "create"), false);
});
test("sync reminder creates new issues with a complete governed classification", async () => {
const value = harness({ legacyIssue: null });
await runWorkflow(value);
assert.deepEqual(value.calls.find(([operation]) => operation === "create")[1].labels, [
"type: maintenance",
"area: computer use",
"status: ready for work",
"sync: computer use",
]);
});
test("sync reminder fails closed when label lookup fails for a non-404 reason", async () => {
const value = harness({ getLabelError: Object.assign(new Error("forbidden"), { status: 403 }) });
await assert.rejects(() => runWorkflow(value), /forbidden/);
assert.equal(value.calls.some(([operation]) => operation === "createLabel"), false);
assert.equal(value.calls.some(([operation]) => operation === "create"), false);
});
test("sync reminder preserves a manual-only item without commenting", async () => {
const value = harness({
legacyIssue: { number: 944, labels: [{ name: "workflow: manual only" }] },
});
await runWorkflow(value);
assert.equal(value.calls.some(([operation]) => operation === "addLabels"), false);
assert.equal(value.calls.some(([operation]) => operation === "createComment"), false);
assert.equal(value.calls.some(([operation]) => operation === "create"), false);
});
test("sync reminder workflow serializes runs and pins third-party actions", () => {
const workflow = fs.readFileSync(workflowPath, "utf8");
assert.match(workflow, /group: computer-use-sync-reminder/);
assert.match(workflow, /cancel-in-progress: false/);
assert.doesNotMatch(workflow, /uses:\s+[^\s]+@v\d/);
});