-
-
Notifications
You must be signed in to change notification settings - Fork 10
feat(auth): expose sign.cap + remaining on /auth/config (beta seat counter) #3761
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| /** | ||
| * @desc Compute the public beta-capacity view for the auth config endpoint: | ||
| * the configured cap and how many seats remain. Skips the (collection-wide) | ||
| * count entirely when uncapped or when cap β€ 0 (remaining is deterministically 0). | ||
| * @param {number|string|null|undefined} rawCap - config.sign.cap (null/undefined/non-numeric/blank = uncapped; 0 = fully closed, 0 seats) | ||
| * @param {() => Promise<number>} countFn - async total-account counter (UserService.count) | ||
| * @returns {Promise<{cap: number|null, remaining: number|null}>} | ||
| */ | ||
| export const computeSignupCapacity = async (rawCap, countFn) => { | ||
| const cap = rawCap != null && String(rawCap).trim() !== '' ? Number(rawCap) : null; | ||
| if (cap == null || !Number.isFinite(cap)) return { cap: null, remaining: null }; | ||
| if (cap <= 0) return { cap, remaining: 0 }; | ||
| const remaining = Math.max(0, cap - (await countFn())); | ||
| return { cap, remaining }; | ||
|
PierreBrisorgueil marked this conversation as resolved.
|
||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { jest } from '@jest/globals'; | ||
| import { computeSignupCapacity } from '../services/auth.signupCapacity.js'; | ||
|
|
||
| describe('computeSignupCapacity', () => { | ||
| test('uncapped (null) β {cap:null, remaining:null} and count() not called', async () => { | ||
| const countFn = jest.fn(); | ||
| await expect(computeSignupCapacity(null, countFn)).resolves.toEqual({ cap: null, remaining: null }); | ||
| expect(countFn).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('uncapped (undefined) β {cap:null, remaining:null}', async () => { | ||
| const countFn = jest.fn(); | ||
| await expect(computeSignupCapacity(undefined, countFn)).resolves.toEqual({ cap: null, remaining: null }); | ||
| expect(countFn).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('capped β remaining = cap - count', async () => { | ||
| const countFn = jest.fn().mockResolvedValue(10); | ||
| await expect(computeSignupCapacity(50, countFn)).resolves.toEqual({ cap: 50, remaining: 40 }); | ||
| }); | ||
|
|
||
| test('at/over cap β remaining floored at 0', async () => { | ||
| const countFn = jest.fn().mockResolvedValue(60); | ||
| await expect(computeSignupCapacity(50, countFn)).resolves.toEqual({ cap: 50, remaining: 0 }); | ||
| }); | ||
|
|
||
| test('non-numeric cap β treated as uncapped', async () => { | ||
| const countFn = jest.fn(); | ||
| await expect(computeSignupCapacity('abc', countFn)).resolves.toEqual({ cap: null, remaining: null }); | ||
| expect(countFn).not.toHaveBeenCalled(); | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| test('empty string cap β treated as uncapped (not coerced to 0)', async () => { | ||
| const countFn = jest.fn(); | ||
| await expect(computeSignupCapacity('', countFn)).resolves.toEqual({ cap: null, remaining: null }); | ||
| expect(countFn).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('whitespace-only cap β treated as uncapped (not coerced to 0)', async () => { | ||
| const countFn = jest.fn(); | ||
| await expect(computeSignupCapacity(' ', countFn)).resolves.toEqual({ cap: null, remaining: null }); | ||
| expect(countFn).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('cap = 0 β fully closed: {cap:0, remaining:0} and count is NOT called (short-circuit)', async () => { | ||
| const countFn = jest.fn(); | ||
| await expect(computeSignupCapacity(0, countFn)).resolves.toEqual({ cap: 0, remaining: 0 }); | ||
| expect(countFn).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('numeric string cap ("42") β coerced: {cap:42, remaining:40}', async () => { | ||
| const countFn = jest.fn().mockResolvedValue(2); | ||
| await expect(computeSignupCapacity('42', countFn)).resolves.toEqual({ cap: 42, remaining: 40 }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.