Skip to content

test(web): add unit tests for pure helper functions#2454

Merged
Hmbown merged 2 commits into
Hmbown:mainfrom
HUQIANTAO:test/web-frontend-unit-tests
May 31, 2026
Merged

test(web): add unit tests for pure helper functions#2454
Hmbown merged 2 commits into
Hmbown:mainfrom
HUQIANTAO:test/web-frontend-unit-tests

Conversation

@HUQIANTAO
Copy link
Copy Markdown
Contributor

@HUQIANTAO HUQIANTAO commented May 31, 2026

Add the first tests for the web frontend:

  • vitest.config.ts: Test configuration pointing to lib/**/*.test.ts and components/**/*.test.tsx
  • github.test.ts: Tests for pure helper functions:
    • relativeTime: 6 test cases covering just-now, minutes, hours, days, months, years
    • lastPageFromLink: 5 test cases covering null, empty, single/multi-part Link headers, invalid format

Vitest was already in package.json devDependencies but had no config or test files.

Greptile Summary

This PR introduces the first frontend unit tests: a vitest.config.ts and a test file covering the relativeTime and lastPageFromLink helpers from web/lib/github.ts. Several issues prevent the tests from working correctly as written.

  • Both functions are re-implemented inline in the test file rather than imported from github.ts, so the tests cover local copies rather than the production code. relativeTime is already exported and can be imported directly; lastPageFromLink would need to be exported.
  • vitest is not present in package.json devDependencies despite the PR description claiming otherwise, so the tests cannot be executed at all.
  • Two assertions are wrong: the \"30d\" case uses a timestamp exactly 30 days prior (falls through to \"1mo\"), and the \"2y\" case for \"2025-01-01\" is only ~17 months away from the pinned time, yielding \"1y\" instead.

Confidence Score: 3/5

Safe to merge for production code — no runtime behaviour is changed — but the tests themselves do not work: they cannot be installed (missing dependency), do not cover the real functions (inline copies), and contain two incorrect assertions.

The tests re-implement the functions they are supposed to cover, vitest is not in package.json so they cannot run at all, and two of the six relativeTime assertions produce the wrong expected value. Together these problems mean the test suite would neither execute nor catch regressions in the production helpers.

web/lib/github.test.ts needs the most attention: the import strategy, the "30d" boundary case, and the "1y"/"2y" years assertion all need to be corrected. web/package.json also needs vitest added before the tests can run.

Important Files Changed

Filename Overview
web/lib/github.test.ts Adds tests for relativeTime and lastPageFromLink, but both functions are re-implemented inline rather than imported from github.ts; two test assertions are also incorrect (the "30d" boundary and the "1y"→"2y" years case).
web/vitest.config.ts Minimal vitest config pointing at lib//*.test.ts and components//*.test.tsx — structurally fine, but vitest itself is not listed in package.json devDependencies so the config cannot be resolved.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[vitest.config.ts] --> B[github.test.ts]
    B --> C{Inline copy of relativeTime}
    B --> D{Inline copy of lastPageFromLink}
    E[github.ts export relativeTime] -.->|should import| C
    F[github.ts private lastPageFromLink] -.->|should export and import| D
    C --> G[6 test cases - 2 wrong assertions]
    D --> H[5 test cases - all correct]
    I[package.json - vitest missing] -.->|blocks execution| A
Loading

Fix All in Codex Fix All in Claude Code Fix All in Cursor

Reviews (1): Last reviewed commit: "test(web): add unit tests for pure helpe..." | Re-trigger Greptile

Greptile also left 3 inline comments on this PR.

Add vitest configuration and tests for:
- relativeTime: time formatting (just now, minutes, hours, days, months, years)
- lastPageFromLink: GitHub Link header pagination parsing

These are the first tests for the web frontend. The test framework
(vitest) was already in package.json but had no config or test files.
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

Comment thread web/lib/github.test.ts Outdated
Comment on lines +7 to +21

function relativeTime(iso: string): string {
const diff = Date.now() - +new Date(iso);
const mins = Math.round(diff / 60000);
if (mins < 1) return "just now";
if (mins < 60) return `${mins}m`;
const hrs = Math.round(mins / 60);
if (hrs < 24) return `${hrs}h`;
const days = Math.round(hrs / 24);
if (days < 30) return `${days}d`;
const months = Math.round(days / 30);
if (months < 12) return `${months}mo`;
return `${Math.round(months / 12)}y`;
}

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.

P1 Tests copy implementations instead of importing them

Both relativeTime and lastPageFromLink are re-implemented verbatim in this file rather than imported from github.ts. This means the tests exercise local copies, not the production code — if the real implementations change, these tests will still pass, providing false confidence. relativeTime is already exported from github.ts and can be imported directly. lastPageFromLink is private; exporting it (or moving both helpers to a shared lib/github-helpers.ts module) would let the tests cover the real code.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Fix in Codex Fix in Claude Code Fix in Cursor

Comment thread web/lib/github.test.ts Outdated

it("returns days for < 30 days", () => {
expect(relativeTime("2026-05-25T12:00:00Z")).toBe("7d");
expect(relativeTime("2026-05-02T12:00:00Z")).toBe("30d");
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.

P1 "30d" boundary assertion is wrong

"2026-05-02T12:00:00Z" is exactly 30 days before the pinned time. The implementation computes days = Math.round(30) = 30, and since 30 < 30 is false, it falls through to the months branch: months = Math.round(30/30) = 1, returning "1mo", not "30d". The test case needs a date that is strictly fewer than 30 days away — for example 29 days — or the assertion needs to match "1mo".

Suggested change
expect(relativeTime("2026-05-02T12:00:00Z")).toBe("30d");
expect(relativeTime("2026-05-03T12:00:00Z")).toBe("29d");

Fix in Codex Fix in Claude Code Fix in Cursor

Comment thread web/lib/github.test.ts Outdated

it("returns years for >= 12 months", () => {
expect(relativeTime("2024-06-01T12:00:00Z")).toBe("2y");
expect(relativeTime("2025-01-01T00:00:00Z")).toBe("2y");
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.

P1 "2y" assertion for 2025-01-01 is wrong

From 2025-01-01T00:00:00Z to 2026-06-01T12:00:00Z is ~516.5 days → months = Math.round(516.5/30) ≈ 17Math.round(17/12) = Math.round(1.42) = 1, so the function returns "1y", not "2y". Choose a date that is clearly ≥ 18 months before the pinned time or update the expectation to "1y".

Suggested change
expect(relativeTime("2025-01-01T00:00:00Z")).toBe("2y");
expect(relativeTime("2025-01-01T00:00:00Z")).toBe("1y");

Fix in Codex Fix in Claude Code Fix in Cursor

Copy link
Copy Markdown
Contributor

@greptile-apps greptile-apps Bot left a comment

Choose a reason for hiding this comment

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

HUQIANTAO has reached the 50-review limit for trial accounts. To continue receiving code reviews, upgrade your plan.

@Hmbown
Copy link
Copy Markdown
Owner

Hmbown commented May 31, 2026

Thanks for getting the first web helper tests going here. I pushed a small maintainer follow-up so these tests import the real production helpers instead of copying their implementations into the test file, and wired up npm test with Vitest so this is runnable locally/CI-ready. Local checks passed: npm test, npm run lint (existing warning only), and git diff --check.

@Hmbown Hmbown merged commit dfeedca into Hmbown:main May 31, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants