Skip to content

Commit

Permalink
Add snapshot test (#764)
Browse files Browse the repository at this point in the history
* Print checks raw data to get snapshot to write tests

* Add some snapshot tests for filtering logics

* Print checks only in debug mode
  • Loading branch information
kachick committed Apr 14, 2024
1 parent 6735a28 commit b118095
Show file tree
Hide file tree
Showing 5 changed files with 1,089 additions and 1 deletion.
3 changes: 3 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28441,6 +28441,9 @@ async function run() {
}
(0, import_core2.startGroup)(`Polling ${attempts}: ${(/* @__PURE__ */ new Date()).toISOString()}`);
const checks = await fetchChecks(githubToken, trigger);
if ((0, import_core2.isDebug)()) {
(0, import_core2.debug)(JSON.stringify(checks, null, 2));
}
const report = generateReport(
checks,
trigger,
Expand Down
3 changes: 3 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ async function run(): Promise<void> {

startGroup(`Polling ${attempts}: ${(new Date()).toISOString()}`);
const checks = await fetchChecks(githubToken, trigger);
if (isDebug()) {
debug(JSON.stringify(checks, null, 2));
}
const report = generateReport(
checks,
trigger,
Expand Down
155 changes: 155 additions & 0 deletions src/report.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import test from 'node:test';
import assert from 'node:assert';
import { snapshotChecks } from './snapshot.ts';
import { generateReport } from './report.ts';

// https://stackoverflow.com/a/56162151
function pick<T extends object, U extends keyof T>(
obj: T,
paths: Array<U>,
): Pick<T, U> {
const ret = Object.create({});
for (const k of paths) {
ret[k] = obj[k];
}
return ret;
}

test('wait-list', () => {
const report = generateReport(
snapshotChecks,
{
owner: 'kachick',
repo: 'wait-other-jobs',
'runId': 8679817057,
ref: '760074f4f419b55cb864030c29ece58a689a42a2',
jobName: 'wait-list',
},
[
{
'workflowFile': 'lint.yml',
'optional': false,
},
{
'workflowFile': 'merge-bot-pr.yml',
'jobName': 'dependabot',
'optional': true,
},
{
'workflowFile': 'THERE_ARE_NO_FILES_AS_THIS.yml',
'optional': true,
},
],
[],
false,
);

assert.deepStrictEqual({
conclusion: 'acceptable',
progress: 'done',
summaries: [
{
acceptable: true,
checkRunUrl: 'https://github.com/kachick/wait-other-jobs/actions/runs/8679817058/job/23799347237',
checkSuiteConclusion: 'SUCCESS',
checkSuiteStatus: 'COMPLETED',
isSameWorkflow: false,
jobName: 'dprint',
runConclusion: 'SUCCESS',
runDatabaseId: 23799347237,
runStatus: 'COMPLETED',
workflowPath: 'lint.yml',
},
{
acceptable: true,
checkRunUrl: 'https://github.com/kachick/wait-other-jobs/actions/runs/8679817058/job/23799347295',
checkSuiteConclusion: 'SUCCESS',
checkSuiteStatus: 'COMPLETED',
isSameWorkflow: false,
jobName: 'typos',
runConclusion: 'SUCCESS',
runDatabaseId: 23799347295,
runStatus: 'COMPLETED',
workflowPath: 'lint.yml',
},
{
acceptable: true,
checkRunUrl: 'https://github.com/kachick/wait-other-jobs/actions/runs/8679817059/job/23799347394',
checkSuiteConclusion: 'SKIPPED',
checkSuiteStatus: 'COMPLETED',
isSameWorkflow: false,
jobName: 'dependabot',
runConclusion: 'NEUTRAL',
runDatabaseId: 23799347394,
runStatus: 'COMPLETED',
workflowPath: 'merge-bot-pr.yml',
},
],
}, report);
});

test('skip-list', () => {
const report = generateReport(
snapshotChecks,
{
owner: 'kachick',
repo: 'wait-other-jobs',
'runId': 8679817057,
ref: '760074f4f419b55cb864030c29ece58a689a42a2',
jobName: 'skip-list',
},
[],
[
{
'workflowFile': 'itself.yml',
},
{
'workflowFile': 'ci.yml',
},
{
'workflowFile': 'ci-nix.yml',
},
{
'workflowFile': 'merge-bot-pr.yml',
'jobName': 'dependabot',
},
],
false,
);

assert.deepEqual({
conclusion: 'acceptable',
progress: 'done',
summaries: [
{
acceptable: true,
jobName: 'dprint',
workflowPath: 'lint.yml',
},
{
acceptable: true,
jobName: 'typos',
workflowPath: 'lint.yml',
},
{
acceptable: true,
jobName: 'judge-dependabot',
workflowPath: 'merge-bot-pr.yml',
},
{
acceptable: true,
jobName: 'renovate',
workflowPath: 'merge-bot-pr.yml',
},
{
acceptable: true,
jobName: 'selfup-runner',
workflowPath: 'merge-bot-pr.yml',
},
],
}, {
conclusion: 'acceptable',
progress: 'done',
summaries: report.summaries.map((summary) => pick(summary, ['workflowPath', 'jobName', 'acceptable'])),
});
});
2 changes: 1 addition & 1 deletion src/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function summarize(check: Check, trigger: Trigger): Summary {
}

export function generateReport(
checks: Check[],
checks: readonly Check[],
trigger: Trigger,
waitList: WaitFilterConditions,
skipList: SkipFilterConditions,
Expand Down

0 comments on commit b118095

Please sign in to comment.