-
Notifications
You must be signed in to change notification settings - Fork 424
[jsweep] Clean validate_lockdown_requirements_templates.cjs #40730
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
b827104
6f6b008
d37b6a9
5e5ccdd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| // @ts-check | ||
| import { describe, it, expect } from "vitest"; | ||
| import { createRequire } from "module"; | ||
|
|
||
| const req = createRequire(import.meta.url); | ||
| const { renderLockdownTokenErrorMessage, renderPublicStrictModeErrorMessage, renderPullRequestTargetErrorMessage } = req("./validate_lockdown_requirements_templates.cjs"); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [/tdd] Long single-line destructuring — split across lines for readability and diff clarity. 💡 Suggested formattingconst {
renderLockdownTokenErrorMessage,
renderPublicStrictModeErrorMessage,
renderPullRequestTargetErrorMessage,
} = req("./validate_lockdown_requirements_templates.cjs");The current line exceeds typical column limits and is harder to scan in diffs. |
||
|
|
||
| describe("validate_lockdown_requirements_templates", () => { | ||
| describe("renderLockdownTokenErrorMessage", () => { | ||
| it("returns a non-empty string", () => { | ||
| expect(typeof renderLockdownTokenErrorMessage()).toBe("string"); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [/tdd] Two issues in one assertion: the function is invoked twice and 💡 Suggested refactorit("returns a non-empty string", () => {
const message = renderLockdownTokenErrorMessage(); // arrange + act once
expect(message).toBeTypeOf("string");
expect(message.length).toBeGreaterThan(0);
});Same pattern applies to the equivalent |
||
| expect(renderLockdownTokenErrorMessage().length).toBeGreaterThan(0); | ||
| }); | ||
|
|
||
| it("includes the auth documentation URL", () => { | ||
| const message = renderLockdownTokenErrorMessage(); | ||
| expect(message).toContain("https://github.com/github/gh-aw/blob/main/docs/src/content/docs/reference/auth.mdx"); | ||
| }); | ||
|
|
||
| it("includes GH_AW_GITHUB_TOKEN recommendation", () => { | ||
| const message = renderLockdownTokenErrorMessage(); | ||
| expect(message).toContain("GH_AW_GITHUB_TOKEN (recommended)"); | ||
| }); | ||
|
|
||
| it("includes GH_AW_GITHUB_MCP_SERVER_TOKEN as alternative", () => { | ||
| const message = renderLockdownTokenErrorMessage(); | ||
| expect(message).toContain("GH_AW_GITHUB_MCP_SERVER_TOKEN (alternative)"); | ||
| }); | ||
|
|
||
| it("includes the gh aw secrets set command", () => { | ||
| const message = renderLockdownTokenErrorMessage(); | ||
| expect(message).toContain("gh aw secrets set GH_AW_GITHUB_TOKEN"); | ||
| }); | ||
|
|
||
| it("mentions lockdown mode is enabled", () => { | ||
| const message = renderLockdownTokenErrorMessage(); | ||
| expect(message).toContain("Lockdown mode is enabled"); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [/tdd] This and similar tests ("includes GH_AW_GITHUB_TOKEN recommendation", "mentions public repository context", etc.) assert exact static phrases from the templates rather than substituted tokens. Any intentional reword (e.g. 💡 RecommendationThe placeholder-substitution tests — Consider one of:
|
||
| }); | ||
|
|
||
| it("does not contain unreplaced {auth_docs_url} placeholder", () => { | ||
| const message = renderLockdownTokenErrorMessage(); | ||
| expect(message).not.toContain("{auth_docs_url}"); | ||
| }); | ||
|
|
||
| it("does not contain unreplaced {security_docs_url} placeholder", () => { | ||
| const message = renderLockdownTokenErrorMessage(); | ||
| expect(message).not.toContain("{security_docs_url}"); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Trivially-passing assertion: 💡 Suggested fixRemove this test case. The meaningful guard— |
||
| }); | ||
|
|
||
| it("returns the same value on repeated calls", () => { | ||
| expect(renderLockdownTokenErrorMessage()).toBe(renderLockdownTokenErrorMessage()); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [/tdd] Idempotency tests for a pure, stateless function add minimal regression value — the function has no side effects and no mutable state, so same-call equality is guaranteed by design rather than tested behaviour. The test slot would be better used for a boundary/edge case (e.g. verifying that the exported module surface exposes exactly the three expected function names). 💡 ContextPer |
||
| }); | ||
| }); | ||
|
|
||
| describe("renderPublicStrictModeErrorMessage", () => { | ||
| it("returns a non-empty string", () => { | ||
| expect(typeof renderPublicStrictModeErrorMessage()).toBe("string"); | ||
| expect(renderPublicStrictModeErrorMessage().length).toBeGreaterThan(0); | ||
| }); | ||
|
|
||
| it("includes the security documentation URL", () => { | ||
| const message = renderPublicStrictModeErrorMessage(); | ||
| expect(message).toContain("https://github.com/github/gh-aw/blob/main/docs/src/content/docs/reference/security.mdx"); | ||
| }); | ||
|
|
||
| it("includes the strict compile command", () => { | ||
| const message = renderPublicStrictModeErrorMessage(); | ||
| expect(message).toContain("gh aw compile --strict"); | ||
| }); | ||
|
|
||
| it("mentions public repository context", () => { | ||
| const message = renderPublicStrictModeErrorMessage(); | ||
| expect(message).toContain("public repository"); | ||
| }); | ||
|
|
||
| it("mentions strict mode", () => { | ||
| const message = renderPublicStrictModeErrorMessage(); | ||
| expect(message).toContain("strict mode"); | ||
| }); | ||
|
|
||
| it("does not contain unreplaced {strict_compile_command} placeholder", () => { | ||
| const message = renderPublicStrictModeErrorMessage(); | ||
| expect(message).not.toContain("{strict_compile_command}"); | ||
| }); | ||
|
|
||
| it("does not contain unreplaced {security_docs_url} placeholder", () => { | ||
| const message = renderPublicStrictModeErrorMessage(); | ||
| expect(message).not.toContain("{security_docs_url}"); | ||
| }); | ||
|
|
||
| it("returns the same value on repeated calls", () => { | ||
| expect(renderPublicStrictModeErrorMessage()).toBe(renderPublicStrictModeErrorMessage()); | ||
| }); | ||
| }); | ||
|
|
||
| describe("renderPullRequestTargetErrorMessage", () => { | ||
| it("returns a non-empty string", () => { | ||
| expect(typeof renderPullRequestTargetErrorMessage()).toBe("string"); | ||
| expect(renderPullRequestTargetErrorMessage().length).toBeGreaterThan(0); | ||
| }); | ||
|
|
||
| it("includes the security documentation URL", () => { | ||
| const message = renderPullRequestTargetErrorMessage(); | ||
| expect(message).toContain("https://github.com/github/gh-aw/blob/main/docs/src/content/docs/reference/security.mdx"); | ||
| }); | ||
|
|
||
| it("mentions the pull_request_target event", () => { | ||
| const message = renderPullRequestTargetErrorMessage(); | ||
| expect(message).toContain("pull_request_target"); | ||
| }); | ||
|
|
||
| it("mentions pwn request security risk", () => { | ||
| const message = renderPullRequestTargetErrorMessage(); | ||
| expect(message).toContain("pwn request"); | ||
| }); | ||
|
|
||
| it("mentions public repositories", () => { | ||
| const message = renderPullRequestTargetErrorMessage(); | ||
| expect(message).toContain("public repositories"); | ||
| }); | ||
|
|
||
| it("suggests using the pull_request event instead", () => { | ||
| const message = renderPullRequestTargetErrorMessage(); | ||
| expect(message).toContain("pull_request event instead"); | ||
| }); | ||
|
|
||
| it("does not contain unreplaced {security_docs_url} placeholder", () => { | ||
| const message = renderPullRequestTargetErrorMessage(); | ||
| expect(message).not.toContain("{security_docs_url}"); | ||
| }); | ||
|
|
||
| it("returns the same value on repeated calls", () => { | ||
| expect(renderPullRequestTargetErrorMessage()).toBe(renderPullRequestTargetErrorMessage()); | ||
| }); | ||
| }); | ||
|
|
||
| describe("cross-function checks", () => { | ||
| it("each render function returns a distinct message", () => { | ||
| const lockdown = renderLockdownTokenErrorMessage(); | ||
| const strictMode = renderPublicStrictModeErrorMessage(); | ||
| const prTarget = renderPullRequestTargetErrorMessage(); | ||
|
|
||
| expect(lockdown).not.toBe(strictMode); | ||
| expect(lockdown).not.toBe(prTarget); | ||
| expect(strictMode).not.toBe(prTarget); | ||
| }); | ||
|
|
||
| it("lockdown message does not contain strict mode content", () => { | ||
| const message = renderLockdownTokenErrorMessage(); | ||
| expect(message).not.toContain("gh aw compile --strict"); | ||
| }); | ||
|
|
||
| it("strict mode message does not contain lockdown token content", () => { | ||
| const message = renderPublicStrictModeErrorMessage(); | ||
| expect(message).not.toContain("GH_AW_GITHUB_TOKEN"); | ||
| }); | ||
|
|
||
| it("no message contains unreplaced placeholders", () => { | ||
| const messages = [renderLockdownTokenErrorMessage(), renderPublicStrictModeErrorMessage(), renderPullRequestTargetErrorMessage()]; | ||
| for (const message of messages) { | ||
| expect(message).not.toMatch(/\{[a-z_]+\}/); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Narrow placeholder guard: 💡 Suggested fixBroaden the character class to cover all typical identifier conventions: expect(message).not.toMatch(/\{[A-Za-z][A-Za-z0-9_]*\}/);All current placeholder names ( |
||
| } | ||
| }); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Module system contradiction:
createRequire(import.meta.url)is an ESM idiom used in.mjsfiles to accessrequire(); using it here—alongside top-levelimportstatements—in a.cjsfile means the file fails withSyntaxError: Cannot use import statement in a modulewhen executed directly by Node.js. Tests pass only because Vitest transforms the file before execution; the.cjsextension actively promises semantics the file does not deliver.💡 Suggested fix
If the file must stay
.cjs, replace ESM imports with CommonJS equivalents—nocreateRequireneeded sincerequire()is already in scope:Alternatively, rename to
.test.mjsand keep the ESM imports as-is. Either way, the file extension and the module syntax must agree.