Skip to content

test: add unit tests for tier helpers#149

Merged
surpradhan merged 7 commits into
surpradhan:mainfrom
Ayushmann13479:test-tiers-unit-tests
Jun 25, 2026
Merged

test: add unit tests for tier helpers#149
surpradhan merged 7 commits into
surpradhan:mainfrom
Ayushmann13479:test-tiers-unit-tests

Conversation

@Ayushmann13479

Copy link
Copy Markdown
Contributor

🎯 Description

Adds a new unit test file for src/tiers.js to improve coverage of subscription tier helper functions and environment-variable override behavior.

🤔 Why?

Issue #114 requested unit tests for the pure functions in src/tiers.js. These functions are a good target for testing because they have no database or network dependencies and contain important tier policy logic.

📝 Changes

  • Added tests/unit/tiers.test.js
  • Added tests for isValidTier()
  • Added tests for TIER_NAMES and DEFAULT_TIER
  • Added tests for getTierDefinitions()
  • Added tests for environment variable overrides
  • Added tests for "unlimited" value handling
  • Added tests for invalid/empty environment values
  • Added tests for getTierPolicy() fallback behavior

🧪 How to Test?

  1. Install dependencies:

    npm install

@surpradhan surpradhan left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks for taking this on, @Ayushmann13479! Solid first contribution. The framework and style match the project well, and the acceptance criteria from issue #114 are covered. Two things to address:

Must fix

parseLimit("0") boundary is untested. The guard in parseLimit is n < 0, so TIER_FREE_EVENT_QUOTA=0 returns 0, not the built-in default. This means an operator who sets the env var to "0" accidentally hard-blocks all event ingest for the free tier. Please add a test that documents this behaviour:

test("zero is returned as-is (not treated as fallback)", () => {
  process.env.TIER_FREE_EVENT_QUOTA = "0";
  assert.equal(getTierDefinitions().free.event_quota, 0);
});

Nits (optional but appreciated)

  • Missing trailing newline at end of file. ESLint won't catch it in this project, but editors and git diff flag it. Just add a newline after the last });.
  • isValidTier(null) and isValidTier(undefined) both safely return false but are untested. A quick two-liner in the existing isValidTier describe block would close that gap.

@Ayushmann13479

Copy link
Copy Markdown
Contributor Author

Thanks for the review
I've addressed the requested changes:

  • Added coverage for TIER_FREE_EVENT_QUOTA="0" to verify that zero is returned as-is and not treated as a fallback.
  • Added tests for isValidTier(null) and isValidTier(undefined).
  • Added the missing trailing newline at the end of the file.

Please take another look.

@surpradhan surpradhan left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks for tackling this -- the structure is solid and all the edge cases from the issue are covered. Two things to address before we merge:

1. Indentation in the "zero is returned as-is" test (lines ~85-90)
The body of that test block is 2-space indented while the rest of the file uses 4-space. There is also a trailing space after the closing });. Quick fix -- just re-indent to match the surrounding tests.

2. process.env replacement pattern
Replacing the whole process.env object works, but is fragile (snapshot is taken at module-load time, not test-run time). The project convention is to mutate specific keys and delete them in afterEach. Something like:

const TIER_KEYS = [
  "TIER_FREE_EVENT_QUOTA",       "TIER_FREE_RETENTION_DAYS",
  "TIER_TEAM_EVENT_QUOTA",       "TIER_TEAM_RETENTION_DAYS",
  "TIER_ENTERPRISE_EVENT_QUOTA", "TIER_ENTERPRISE_RETENTION_DAYS",
];
afterEach(() => { for (const k of TIER_KEYS) delete process.env[k]; });

Then each test just sets the vars it needs directly -- no beforeEach needed at all.

Optional but nice-to-have: add isValidTier("") to the "returns false" test -- empty string is a realistic bad input from a malformed project row.

@Ayushmann13479

Ayushmann13479 commented Jun 25, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review! I've addressed the requested changes:

  1. Removed the beforeEach pattern and switched to cleaning only the tier-related environment variables in afterEach.
  2. Fixed the indentation and trailing whitespace in the zero-value test.
  3. Added the isValidTier("") test.

Please let me know if there's anything else you'd like me to adjust.

@surpradhan surpradhan left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Good fixes -- the process.env replacement is gone and isValidTier("") is in. All the functional feedback is addressed.

One remaining nit: the closing }); on the "zero is returned as-is" test is still at column 0 instead of column 2. Also TIER_KEYS and afterEach use 4-space indentation while the describe/test blocks use 2-space -- worth making consistent. Both are cosmetic only.

Happy to merge once those are tidied up.

@Ayushmann13479

Copy link
Copy Markdown
Contributor Author

Thanks! I've cleaned up the indentation to match the rest of the file.

@surpradhan surpradhan merged commit 01dd872 into surpradhan:main Jun 25, 2026
14 checks passed
@surpradhan

Copy link
Copy Markdown
Owner

Thank you for this contribution, @Ayushmann13479! The tier helper unit tests are a great addition to the test suite — clean coverage of isValidTier(), getTierDefinitions(), env-var overrides, and getTierPolicy() fallback behavior. This closes issue #114.

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