Files
bookmarkmagic/tests/unit/scripts/triage.test.ts
T
poli0981andClaude Opus 5.5 eabffbe652 feat(repo): maintenance mode, with triage that closes out-of-policy requests
MAINTENANCE.md is the policy: dependency updates on a schedule, high and
critical advisories fixed as soon as a fix exists, major platform upgrades,
bug/UI/UX/accessibility/translation fixes, and features only when they are
on docs/15's roadmap or genuinely needed.

- The feature form asks one required question — on the roadmap, needed for
  a fix, or something new — and links the policy and Discussions › Ideas.
- scripts/triage.mjs decides what happens, as a pure, unit-tested function;
  .github/workflows/triage.yml carries it out with `gh` alone. Something new
  is labelled maintenance-mode, commented and closed as not planned; the two
  in-policy answers get needs-triage; an outside `feat…` pull request is
  closed the same way. Owner, members, collaborators, bots and `roadmap` are
  exempt.
- pull_request_target checks out the base branch only, runs nothing from the
  pull request, and never interpolates a title or body into the shell.
- README gains a status badge and a "Project status" section; CONTRIBUTING,
  the PR template and the issue chooser say what is accepted.
- docs: 00 status, 11 inventory, 12 §2.6 and §5 (labels), 13 §6, 15 (a
  pointer to the policy, not a copy, and the decision-log rows); CLAUDE.md
  Phase 10.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 21:03:47 +07:00

218 lines
6.9 KiB
TypeScript

import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { describe, expect, it } from 'vitest';
import {
decide,
IDEAS_URL,
ISSUE_COMMENT,
KIND,
KIND_QUESTION,
kindOf,
LABEL,
POLICY_URL,
PR_COMMENT,
} from '../../../scripts/triage.mjs';
/**
* scripts/triage.mjs — what the maintenance-mode triage workflow does with an
* issue or a pull request (MAINTENANCE.md, docs/12 §2.6).
*/
const read = (path: string): string => readFileSync(join(process.cwd(), path), 'utf8');
/** An issue body as GitHub renders the feature form. */
function formBody(kind: string): string {
return [
`### ${KIND_QUESTION}`,
'',
kind,
'',
'### What are you trying to do?',
'',
'Something.',
].join('\n');
}
function issueEvent(
overrides: {
action?: string;
label?: string;
labels?: string[];
body?: unknown;
association?: string;
state?: string;
} = {},
): unknown {
return {
action: overrides.action ?? 'opened',
...(overrides.label !== undefined && { label: { name: overrides.label } }),
issue: {
number: 7,
state: overrides.state ?? 'open',
author_association: overrides.association ?? 'NONE',
labels: (overrides.labels ?? [LABEL.feature]).map((name) => ({ name })),
body: overrides.body ?? formBody(KIND.fresh),
},
};
}
function prEvent(
overrides: {
action?: string;
title?: string;
association?: string;
userType?: string;
labels?: string[];
} = {},
): unknown {
return {
action: overrides.action ?? 'opened',
pull_request: {
number: 12,
state: 'open',
title: overrides.title ?? 'feat(export): add a PDF format',
author_association: overrides.association ?? 'FIRST_TIME_CONTRIBUTOR',
user: { type: overrides.userType ?? 'User' },
labels: (overrides.labels ?? []).map((name) => ({ name })),
},
};
}
describe('kindOf — the feature form dropdown', () => {
it.each(Object.values(KIND))('reads %j', (kind) => {
expect(kindOf(formBody(kind))).toBe(kind);
});
it('reads a CRLF body', () => {
expect(kindOf(formBody(KIND.needed).replaceAll('\n', '\r\n'))).toBe(KIND.needed);
});
it('is undefined without the question, with an empty answer, or for a non-string', () => {
expect(kindOf('### What happened?\n\nIt broke.')).toBeUndefined();
expect(kindOf(`### ${KIND_QUESTION}\n\n### Next\n\nx`)).toBeUndefined();
expect(kindOf(null)).toBeUndefined();
});
});
describe('decide — issues', () => {
it('closes a request for something new, with the policy comment', () => {
expect(decide('issues', issueEvent())).toEqual({
action: 'close',
reason: 'a new feature outside the roadmap',
kind: 'issue',
number: 7,
label: LABEL.closed,
comment: ISSUE_COMMENT,
});
});
it.each([KIND.roadmap, KIND.needed])('keeps %j open for the maintainer', (kind) => {
expect(decide('issues', issueEvent({ body: formBody(kind) }))).toMatchObject({
action: 'label',
label: LABEL.triage,
number: 7,
});
});
it('closes an issue the maintainer labelled enhancement by hand', () => {
const event = issueEvent({
action: 'labeled',
label: LABEL.feature,
body: '### What happened?\n\nx',
});
expect(decide('issues', event)).toMatchObject({
action: 'close',
reason: 'labelled enhancement by hand',
});
});
it.each([
['another label was added', issueEvent({ action: 'labeled', label: 'bug' })],
['not a feature request', issueEvent({ labels: ['bug'] })],
['the roadmap exemption', issueEvent({ labels: [LABEL.feature, LABEL.exempt] })],
['already closed by the bot', issueEvent({ labels: [LABEL.feature, LABEL.closed] })],
['already sent to triage', issueEvent({ labels: [LABEL.feature, LABEL.triage] })],
["the owner's own issue", issueEvent({ association: 'OWNER' })],
['a closed issue', issueEvent({ state: 'closed' })],
['an edit', issueEvent({ action: 'edited' })],
])('does nothing for %s', (_case, event) => {
expect(decide('issues', event).action).toBe('none');
});
it('does nothing without a usable issue number', () => {
expect(decide('issues', { action: 'opened', issue: { number: '7; rm -rf /' } }).action).toBe(
'none',
);
expect(decide('issues', null).action).toBe('none');
});
});
describe('decide — pull requests', () => {
it.each([
'feat: add a PDF export',
'feat(export): add a PDF export',
'feat!: drop CSV',
'FEAT(edit)!: new panel',
])('closes %j from outside the project', (title) => {
expect(decide('pull_request_target', prEvent({ title }))).toEqual({
action: 'close',
reason: 'a feature pull request from outside the project',
kind: 'pr',
number: 12,
label: LABEL.closed,
comment: PR_COMMENT,
});
});
it.each([
'fix(import): read a BOM',
'feature: something',
'docs: feat is not a prefix here',
'chore(deps): bump vite',
])('leaves %j alone', (title) => {
expect(decide('pull_request_target', prEvent({ title })).action).toBe('none');
});
it.each([
['the owner', prEvent({ association: 'OWNER' })],
['a collaborator', prEvent({ association: 'COLLABORATOR' })],
['a member', prEvent({ association: 'MEMBER' })],
['Dependabot', prEvent({ userType: 'Bot' })],
['a roadmap pull request', prEvent({ labels: [LABEL.exempt] })],
['a reopened pull request', prEvent({ action: 'reopened' })],
])('exempts %s', (_case, event) => {
expect(decide('pull_request_target', event).action).toBe('none');
});
});
describe('decide — other events', () => {
it('ignores events it does not triage', () => {
expect(decide('push', {}).action).toBe('none');
expect(decide(undefined, {}).action).toBe('none');
});
});
describe('the pieces the script shares with other files', () => {
it('matches the feature form word for word', () => {
const form = read('.github/ISSUE_TEMPLATE/feature_request.yml');
expect(form).toContain(`label: ${KIND_QUESTION}`);
for (const kind of Object.values(KIND)) expect(form).toContain(`- ${kind}`);
expect(form).toContain(`labels: [${LABEL.feature}]`);
});
it('links the policy and the ideas board, which exist', () => {
for (const comment of [ISSUE_COMMENT, PR_COMMENT]) expect(comment).toContain(POLICY_URL);
expect(ISSUE_COMMENT).toContain(IDEAS_URL);
expect(read('MAINTENANCE.md')).toContain(`\`${LABEL.closed}\``);
});
it('is what the workflow runs, with the labels the workflow re-checks', () => {
const workflow = read('.github/workflows/triage.yml');
expect(workflow).toContain('node scripts/triage.mjs');
for (const label of [LABEL.closed, LABEL.triage, LABEL.exempt])
expect(workflow).toContain(` ${label} `);
// Nothing from the event payload is interpolated into a shell line.
expect(workflow).not.toMatch(/\$\{\{\s*github\.event\.(issue|pull_request)\.(title|body)/);
});
});