Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 49 additions & 58 deletions .github/actions/draft-segment-schema-pr/gate.test.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { describe, it } from 'node:test';
import { fileURLToPath } from 'node:url';
import { describe, expect, it } from 'vitest';

import {
AGREEMENT_PHRASE,
Expand Down Expand Up @@ -55,32 +54,28 @@ function input(overrides = {}) {

describe('eventSkipReason', () => {
it('skips propose on issue_comment and close unless the PR closed', () => {
assert.equal(
eventSkipReason('propose', 'issue_comment', 'created'),
expect(eventSkipReason('propose', 'issue_comment', 'created')).toBe(
'wrong-event',
);
assert.equal(
eventSkipReason('propose', 'pull_request', 'closed'),
expect(eventSkipReason('propose', 'pull_request', 'closed')).toBe(
'wrong-event',
);
assert.equal(
eventSkipReason('close', 'pull_request', 'synchronize'),
expect(eventSkipReason('close', 'pull_request', 'synchronize')).toBe(
'wrong-event',
);
assert.equal(eventSkipReason('create', 'issue_comment', 'created'), '');
assert.equal(eventSkipReason('propose', 'pull_request', 'synchronize'), '');
assert.equal(eventSkipReason('close', 'pull_request', 'closed'), '');
expect(eventSkipReason('create', 'issue_comment', 'created')).toBe('');
expect(eventSkipReason('propose', 'pull_request', 'synchronize')).toBe('');
expect(eventSkipReason('close', 'pull_request', 'closed')).toBe('');
});
});

describe('isAgreementComment', () => {
it('treats trailing whitespace on the agreement sentence as a match', () => {
assert.equal(
expect(
isAgreementComment(`${AGREEMENT_PHRASE} \nThanks`, 'alice', 'alice'),
true,
);
assert.equal(isAgreementComment(AGREEMENT_PHRASE, 'bob', 'alice'), false);
assert.equal(isAgreementComment('please open it', 'alice', 'alice'), false);
).toBe(true);
expect(isAgreementComment(AGREEMENT_PHRASE, 'bob', 'alice')).toBe(false);
expect(isAgreementComment('please open it', 'alice', 'alice')).toBe(false);
});
});

Expand All @@ -94,9 +89,9 @@ describe('collectTsDiff and hasAnalyticsDiff', () => {
{ filename: 'README.md', patch: '+docs' },
{ filename: 'app/foo.test.ts', patch: '+createEventBuilder(x)' },
]);
assert.deepEqual(diff.files, ['app/Home.ts']);
assert.equal(diff.mentionsAnalytics, true);
assert.equal(hasAnalyticsDiff(diff, 'mobile'), true);
expect(diff.files).toStrictEqual(['app/Home.ts']);
expect(diff.mentionsAnalytics).toBe(true);
expect(hasAnalyticsDiff(diff, 'mobile')).toBe(true);
});

it('treats a catalog-only change as an analytics hit', () => {
Expand All @@ -106,16 +101,16 @@ describe('collectTsDiff and hasAnalyticsDiff', () => {
patch: "+NEW_EVENT = 'New Event'",
},
]);
assert.equal(diff.mentionsAnalytics, false);
assert.equal(hasAnalyticsDiff(diff, 'mobile'), true);
expect(diff.mentionsAnalytics).toBe(false);
expect(hasAnalyticsDiff(diff, 'mobile')).toBe(true);
});

it('does not treat unrelated TypeScript patches as analytics hits', () => {
const diff = collectTsDiff([
{ filename: 'app/Home.ts', patch: '+const x = 1;\n' },
]);
assert.equal(diff.mentionsAnalytics, false);
assert.equal(hasAnalyticsDiff(diff, 'mobile'), false);
expect(diff.mentionsAnalytics).toBe(false);
expect(hasAnalyticsDiff(diff, 'mobile')).toBe(false);
});

it('treats addSensitiveProperties as an analytics hit', () => {
Expand All @@ -125,7 +120,7 @@ describe('collectTsDiff and hasAnalyticsDiff', () => {
patch: '+.addSensitiveProperties({ secret: true })',
},
]);
assert.equal(diff.mentionsAnalytics, true);
expect(diff.mentionsAnalytics).toBe(true);
});

it('counts a renamed TypeScript file once', () => {
Expand All @@ -136,32 +131,30 @@ describe('collectTsDiff and hasAnalyticsDiff', () => {
patch: '+const x = 1;\n',
},
]);
assert.deepEqual(diff.files, ['app/next.ts', 'app/prev.ts']);
assert.equal(diff.recordCount, 1);
expect(diff.files).toStrictEqual(['app/next.ts', 'app/prev.ts']);
expect(diff.recordCount).toBe(1);
});
});

describe('shouldPushSchema', () => {
it('pushes on create when open, and on propose only after a schema PR exists', () => {
assert.equal(shouldPushSchema('create', false, true, true), true);
assert.equal(shouldPushSchema('propose', false, true, true), false);
assert.equal(shouldPushSchema('propose', true, true, true), true);
assert.equal(shouldPushSchema('close', true, false, true), false);
assert.equal(shouldPushSchema('create', false, true, false), false);
expect(shouldPushSchema('create', false, true, true)).toBe(true);
expect(shouldPushSchema('propose', false, true, true)).toBe(false);
expect(shouldPushSchema('propose', true, true, true)).toBe(true);
expect(shouldPushSchema('close', true, false, true)).toBe(false);
expect(shouldPushSchema('create', false, true, false)).toBe(false);
});
});

describe('decideGate', () => {
it('skips forks, opt-out, closed PRs, and non-agreement comments', () => {
assert.equal(
decideGate(input({ pr: { ...PR, isFork: true } })).skipReason,
expect(decideGate(input({ pr: { ...PR, isFork: true } })).skipReason).toBe(
'fork',
);
assert.equal(
expect(
decideGate(input({ pr: { ...PR, hasOptOutLabel: true } })).skipReason,
'opt-out',
);
assert.equal(
).toBe('opt-out');
expect(
decideGate(
input({
mode: 'create',
Expand All @@ -172,18 +165,16 @@ describe('decideGate', () => {
commenterLogin: 'alice',
}),
).skipReason,
'client-pr-closed',
);
assert.equal(
).toBe('client-pr-closed');
expect(
decideGate(
input({
mode: 'create',
eventName: 'issue_comment',
eventAction: 'created',
}),
).skipReason,
'agreement-mismatch',
);
).toBe('agreement-mismatch');
});

it('skips close when no schema PR exists', () => {
Expand All @@ -194,8 +185,8 @@ describe('decideGate', () => {
schemaPrExists: false,
}),
);
assert.equal(result.skip, true);
assert.equal(result.skipReason, 'no-schema-pr');
expect(result.skip).toBe(true);
expect(result.skipReason).toBe('no-schema-pr');
});

it('skips propose when there is no analytics diff, schema PR, or sticky comment', () => {
Expand All @@ -204,7 +195,7 @@ describe('decideGate', () => {
pullFiles: [{ filename: 'app/Home.ts', patch: '+const x = 1;\n' }],
}),
);
assert.equal(result.skipReason, 'no-analytics-diff');
expect(result.skipReason).toBe('no-analytics-diff');
});

it('continues propose so publish can edit a sticky comment when analytics drop off', () => {
Expand All @@ -214,9 +205,9 @@ describe('decideGate', () => {
hasProposalComment: true,
}),
);
assert.equal(result.skip, false);
assert.equal(result.hasAnalyticsDiff, false);
assert.equal(result.shouldPush, false);
expect(result.skip).toBe(false);
expect(result.hasAnalyticsDiff).toBe(false);
expect(result.shouldPush).toBe(false);
});

it('sets tooManyFiles and does not push when the TS file list exceeds the cap', () => {
Expand All @@ -228,9 +219,9 @@ describe('decideGate', () => {
}),
);
const result = decideGate(input({ pullFiles, schemaPrExists: true }));
assert.equal(result.skip, false);
assert.equal(result.tooManyFiles, true);
assert.equal(result.shouldPush, false);
expect(result.skip).toBe(false);
expect(result.tooManyFiles).toBe(true);
expect(result.shouldPush).toBe(false);
});

it('does not treat 76 renamed TypeScript files as over the cap', () => {
Expand All @@ -240,7 +231,7 @@ describe('decideGate', () => {
patch: '+createEventBuilder(MetaMetricsEvents.APP_OPENED)',
}));
const result = decideGate(input({ pullFiles }));
assert.equal(result.tooManyFiles, false);
expect(result.tooManyFiles).toBe(false);
});

it('pushes on create when agreement matches and YAML can be written', () => {
Expand All @@ -253,9 +244,9 @@ describe('decideGate', () => {
commenterLogin: 'alice',
}),
);
assert.equal(result.skip, false);
assert.equal(result.shouldPush, true);
assert.equal(result.branch, botBranchName('mobile', 12));
expect(result.skip).toBe(false);
expect(result.shouldPush).toBe(true);
expect(result.branch).toBe(botBranchName('mobile', 12));
});
});

Expand All @@ -265,8 +256,8 @@ describe('schema pull list client', () => {
join(dirname(fileURLToPath(import.meta.url)), 'action.yml'),
'utf8',
);
assert.match(action, /getOctokit\(process\.env\.SEGMENT_SCHEMA_TOKEN\)/u);
assert.doesNotMatch(action, /const \{\s*getOctokit\s*\}/u);
assert.doesNotMatch(action, /headers:\s*\{\s*authorization:/u);
expect(action).toMatch(/getOctokit\(process\.env\.SEGMENT_SCHEMA_TOKEN\)/u);
expect(action).not.toMatch(/const \{\s*getOctokit\s*\}/u);
expect(action).not.toMatch(/headers:\s*\{\s*authorization:/u);
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import { describe, expect, it } from 'vitest';

import {
allocateBucketSlots,
Expand Down Expand Up @@ -51,11 +50,11 @@ describe('summarizeTestHealth', () => {
},
]);

const test = summary[0];
assert.equal(test.latestClassification, 'passed');
assert.equal(test.historicalBrokenCount, 1);
assert.equal(test.historicalFlakyCount, 1);
assert.equal(test.totalRuns, 3);
const [first] = summary;
expect(first.latestClassification).toBe('passed');
expect(first.historicalBrokenCount).toBe(1);
expect(first.historicalFlakyCount).toBe(1);
expect(first.totalRuns).toBe(3);
});
});

Expand All @@ -73,9 +72,9 @@ describe('partitionSummary', () => {
},
]);

assert.equal(watchItems.length, 1);
assert.equal(flakyItems.length, 0);
assert.equal(brokenItems.length, 0);
expect(watchItems).toHaveLength(1);
expect(flakyItems).toHaveLength(0);
expect(brokenItems).toHaveLength(0);
});
});

Expand All @@ -88,12 +87,11 @@ describe('allocateBucketSlots', () => {
infra: 0,
});

assert.equal(slots.maxBroken, 0);
assert.equal(slots.maxWatch, 13);
assert.equal(
expect(slots.maxBroken).toBe(0);
expect(slots.maxWatch).toBe(13);
expect(
slots.maxBroken + slots.maxFlaky + slots.maxWatch + slots.maxInfra,
13,
);
).toBe(13);
});
});

Expand All @@ -105,54 +103,51 @@ describe('formatWatchHistory', () => {
totalRuns: 8,
});

assert.match(text, /failed 2\/8 runs/);
assert.match(text, /flaky 3\/8 runs/);
expect(text).toMatch(/failed 2\/8 runs/u);
expect(text).toMatch(/flaky 3\/8 runs/u);
});
});

describe('normalizeRepoFilePath', () => {
it('prefixes testDir-relative paths', () => {
assert.equal(
expect(
normalizeRepoFilePath(
'accounts/account-syncing-settings-toggle.spec.ts',
{
prefix: 'tests/smoke-appium',
},
),
).toBe(
'tests/smoke-appium/accounts/account-syncing-settings-toggle.spec.ts',
);
});

it('strips CI absolute checkout paths to repo-relative', () => {
assert.equal(
expect(
normalizeRepoFilePath(
'/Users/runner/work/metamask-mobile/metamask-mobile/tests/framework/config/global.setup.ts',
),
'tests/framework/config/global.setup.ts',
);
).toBe('tests/framework/config/global.setup.ts');
});

it('leaves paths already under tests/ unchanged when prefix is set', () => {
assert.equal(
expect(
normalizeRepoFilePath('tests/smoke-appium/accounts/foo.spec.ts', {
prefix: 'tests/smoke-appium',
}),
'tests/smoke-appium/accounts/foo.spec.ts',
);
).toBe('tests/smoke-appium/accounts/foo.spec.ts');
});

it('does not double-apply an identical prefix', () => {
assert.equal(
expect(
normalizeRepoFilePath('tests/smoke-appium/accounts/foo.spec.ts', {
prefix: 'tests/smoke-appium',
}),
'tests/smoke-appium/accounts/foo.spec.ts',
);
).toBe('tests/smoke-appium/accounts/foo.spec.ts');
});

it('returns relative paths unchanged when no prefix is set', () => {
assert.equal(
normalizeRepoFilePath('accounts/foo.spec.ts'),
expect(normalizeRepoFilePath('accounts/foo.spec.ts')).toBe(
'accounts/foo.spec.ts',
);
});
Expand Down Expand Up @@ -195,16 +190,16 @@ describe('createSlackBlocks path links', () => {
},
);

const link = blocks
.flatMap((block) => block.elements || [])
.flatMap((element) => element.elements || [])
.find(
(element) => element.type === 'link' && element.text === 'toggles sync',
);
const elements = blocks
.flatMap(({ elements: blockElements = [] }) => blockElements)
.flatMap(({ elements: innerElements = [] }) => innerElements);

assert.equal(
link?.url,
'https://github.com/MetaMask/metamask-mobile/blob/main/tests/smoke-appium/accounts/account-syncing-settings-toggle.spec.ts',
expect(elements).toContainEqual(
expect.objectContaining({
type: 'link',
text: 'toggles sync',
url: 'https://github.com/MetaMask/metamask-mobile/blob/main/tests/smoke-appium/accounts/account-syncing-settings-toggle.spec.ts',
}),
);
});
});
Loading
Loading