Skip to content

test: add infra stage parser coverage#1868

Open
GhostieMostie wants to merge 2 commits into
CapSoftware:mainfrom
GhostieMostie:maximus-cap-infra-tests
Open

test: add infra stage parser coverage#1868
GhostieMostie wants to merge 2 commits into
CapSoftware:mainfrom
GhostieMostie:maximus-cap-infra-tests

Conversation

@GhostieMostie
Copy link
Copy Markdown

@GhostieMostie GhostieMostie commented May 24, 2026

/claim #54

Summary

  • adds a focused Vitest setup for the @cap/infra workspace
  • extracts SST stage parsing into a small tested helper
  • covers production, staging, git-branch preview stages, and unsupported stages

Validation

  • Direct helper assertion with Node 24 import: staging, production, git-branch parsing, and unsupported-stage throw all passed
  • Updated pnpm-lock.yaml importer for @cap/infra with the existing Vitest 2.1.9 lock entry

Greptile Summary

This PR extracts the inline parsedStage closure in sst.config.ts into a standalone parseStageName helper in stage.ts, and wires up a focused Vitest suite for the @cap/infra workspace.

  • stage.ts: New module exporting ParsedStage discriminated union and parseStageName; logic is a direct lift from the original inline function with no behavioural change.
  • stage.test.ts: Four Vitest cases covering staging, production, a git-branch-* prefix, and an unsupported stage throw; the empty-prefix edge case (\"git-branch-\" → empty branch string) is not currently tested or guarded.
  • package.json / pnpm-lock.yaml: Adds vitest ~2.1.9 as a dev dependency (reuses an existing resolved entry in the lockfile) and a test script, making the workspace participate in pnpm turbo test.

Confidence Score: 4/5

Safe to merge; the only gap is a silent empty-branch path that was already latent before this change.

The refactor is a straightforward extraction with no changed behaviour. The one unaddressed case — parseStageName("git-branch-") returning an empty branch — was already silently possible before this PR via the inline function, and is not made worse here. The tests and helper both look correct for all documented stage names.

infra/stage.ts and infra/stage.test.ts — the empty-branch guard and corresponding test case.

Important Files Changed

Filename Overview
infra/stage.ts New helper extracting stage-parsing logic; clean discriminated-union return type; edge case of empty branch name after the prefix is not guarded.
infra/stage.test.ts New Vitest suite covering the four main cases; missing a test for the "git-branch-" empty-branch edge case.
infra/sst.config.ts Inline parsedStage closure replaced with a call to the new helper; behaviour is identical and no logic was changed.
infra/package.json Adds vitest dev dependency, a test script, and "type": "module"; straightforward and consistent with the rest of the monorepo's ESM setup.
pnpm-lock.yaml Adds the @cap/infra vitest importer entry, reusing an existing resolved version (2.1.9); no new packages introduced.
Prompt To Fix All With AI
Fix the following 2 code review issues. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 2
infra/stage.test.ts:10-15
**Missing edge-case: empty branch after the prefix**

`parseStageName("git-branch-")` currently returns `{ variant: "git-branch", branch: "" }` — an empty string branch. Downstream in `sst.config.ts`, that value is interpolated directly into a Vercel preview URL, yielding a double-dash segment (`…-git--mc-ilroy.vercel.app`) which Vercel won't resolve. A test asserting that `"git-branch-"` either throws or is treated as unsupported would make the acceptable input contract explicit.

### Issue 2 of 2
infra/stage.ts:9-13
Guard against a bare `"git-branch-"` input (empty branch name). An empty `branch` string propagates silently and produces a malformed Vercel preview URL in `sst.config.ts`.

```suggestion
	if (stage.startsWith("git-branch-")) {
		const branch = stage.slice("git-branch-".length);
		if (!branch) throw new Error("Unsupported stage");
		return { variant: "git-branch", branch };
	}
```

Reviews (1): Last reviewed commit: "test: add infra stage parser coverage" | Re-trigger Greptile

Greptile also left 2 inline comments on this PR.

@superagent-security superagent-security Bot added contributor:verified Contributor passed trust analysis. pr:verified PR passed security analysis. labels May 24, 2026
Comment thread infra/stage.test.ts
Comment on lines +10 to +15
it("extracts branch names from git branch preview stages", () => {
expect(parseStageName("git-branch-add-infra-tests")).toEqual({
variant: "git-branch",
branch: "add-infra-tests",
});
});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Missing edge-case: empty branch after the prefix

parseStageName("git-branch-") currently returns { variant: "git-branch", branch: "" } — an empty string branch. Downstream in sst.config.ts, that value is interpolated directly into a Vercel preview URL, yielding a double-dash segment (…-git--mc-ilroy.vercel.app) which Vercel won't resolve. A test asserting that "git-branch-" either throws or is treated as unsupported would make the acceptable input contract explicit.

Prompt To Fix With AI
This is a comment left during a code review.
Path: infra/stage.test.ts
Line: 10-15

Comment:
**Missing edge-case: empty branch after the prefix**

`parseStageName("git-branch-")` currently returns `{ variant: "git-branch", branch: "" }` — an empty string branch. Downstream in `sst.config.ts`, that value is interpolated directly into a Vercel preview URL, yielding a double-dash segment (`…-git--mc-ilroy.vercel.app`) which Vercel won't resolve. A test asserting that `"git-branch-"` either throws or is treated as unsupported would make the acceptable input contract explicit.

How can I resolve this? If you propose a fix, please make it concise.

Comment thread infra/stage.ts Outdated
Comment on lines +9 to +13
if (stage.startsWith("git-branch-"))
return {
variant: "git-branch",
branch: stage.slice("git-branch-".length),
};
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Guard against a bare "git-branch-" input (empty branch name). An empty branch string propagates silently and produces a malformed Vercel preview URL in sst.config.ts.

Suggested change
if (stage.startsWith("git-branch-"))
return {
variant: "git-branch",
branch: stage.slice("git-branch-".length),
};
if (stage.startsWith("git-branch-")) {
const branch = stage.slice("git-branch-".length);
if (!branch) throw new Error("Unsupported stage");
return { variant: "git-branch", branch };
}
Prompt To Fix With AI
This is a comment left during a code review.
Path: infra/stage.ts
Line: 9-13

Comment:
Guard against a bare `"git-branch-"` input (empty branch name). An empty `branch` string propagates silently and produces a malformed Vercel preview URL in `sst.config.ts`.

```suggestion
	if (stage.startsWith("git-branch-")) {
		const branch = stage.slice("git-branch-".length);
		if (!branch) throw new Error("Unsupported stage");
		return { variant: "git-branch", branch };
	}
```

How can I resolve this? If you propose a fix, please make it concise.

@GhostieMostie
Copy link
Copy Markdown
Author

Follow-up after the automated review: I addressed Greptile's edge-case note in f70b8ca by rejecting a bare \git-branch-\ stage and adding coverage for it. The non-Vercel checks are green/neutral; the remaining Vercel status appears to be the authorization URL rather than a code failure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor:verified Contributor passed trust analysis. pr:verified PR passed security analysis.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant