From be6f014f444d9e1fad2ee0d9cff7ae050f864f72 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Tue, 25 Aug 2026 17:21:41 +0530 Subject: [PATCH] fix(auth): stop the users limit card silently blocking Update The user-limit policy reserves `total: 0` for "no limit", but the card treated 0 as a value you could type. On a project with no limit, selecting Limited and entering 0 left `policy.total === newLimit`, so Update stayed disabled with nothing explaining why. The same check had three more holes: entering 0 on an already-limited project saved `total: 0` and silently turned the limit off while the UI still read "Limited 0"; clearing the field bound `undefined` and made the SDK throw `Missing required parameter: "total"`; and with no `min` on the input a negative value reached the API as a 400. Move the range and changed-vs-saved checks into usersLimit.ts, put min/max/step on the input, and surface a rejected value as an inline error instead of a dead button. 0 can no longer be submitted as a limit, so the card cannot drift out of sync with the saved policy. Also drop `maxUsersInputField` and its focus effect: nothing ever bound it, so it was always null and never ran. Wiring it up would have auto-focused and scrolled to the card on every page load. --- .../auth/security/updateUsersLimit.svelte | 33 ++++----- .../auth/security/usersLimit.test.ts | 70 +++++++++++++++++++ .../auth/security/usersLimit.ts | 17 +++++ 3 files changed, 104 insertions(+), 16 deletions(-) create mode 100644 src/routes/(console)/project-[region]-[project]/auth/security/usersLimit.test.ts create mode 100644 src/routes/(console)/project-[region]-[project]/auth/security/usersLimit.ts diff --git a/src/routes/(console)/project-[region]-[project]/auth/security/updateUsersLimit.svelte b/src/routes/(console)/project-[region]-[project]/auth/security/updateUsersLimit.svelte index ee522ea8b..c70905ba5 100644 --- a/src/routes/(console)/project-[region]-[project]/auth/security/updateUsersLimit.svelte +++ b/src/routes/(console)/project-[region]-[project]/auth/security/updateUsersLimit.svelte @@ -7,8 +7,14 @@ import { addNotification } from '$lib/stores/notifications'; import { sdk } from '$lib/stores/sdk'; import { Layout, Selector, Input, Badge } from '@appwrite.io/pink-svelte'; - import { tick } from 'svelte'; import type { Models } from '@appwrite.io/console'; + import { + USERS_LIMIT_MAX, + USERS_LIMIT_MIN, + usersLimitChanged, + usersLimitError, + type UsersLimitMode + } from './usersLimit'; let { project, @@ -18,15 +24,12 @@ policy: Models.PolicyUserLimit; } = $props(); - let maxUsersInputField: HTMLInputElement | null = $state(null); - - let value = $state(policy.total !== 0 ? 'limited' : 'unlimited'); + let value = $state(policy.total !== 0 ? 'limited' : 'unlimited'); let newLimit = $state(policy.total !== 0 ? policy.total : 100); const isLimited = $derived(value === 'limited'); - const btnDisabled = $derived.by(() => { - return (!isLimited && policy.total === 0) || (isLimited && policy.total === newLimit); - }); + const limitError = $derived(isLimited ? usersLimitError(newLimit) : null); + const btnDisabled = $derived(!!limitError || !usersLimitChanged(value, newLimit, policy.total)); async function updateLimit() { try { @@ -47,14 +50,6 @@ trackError(error, Submit.AuthLimitUpdate); } } - - $effect(() => { - if (isLimited && maxUsersInputField) { - tick().then(() => { - maxUsersInputField.focus(); - }); - } - }); @@ -83,10 +78,16 @@ name="limit" id="limit" class="input-text" - max="10000" + min={USERS_LIMIT_MIN} + max={USERS_LIMIT_MAX} + step={1} + state={limitError ? 'error' : 'default'} disabled={!isLimited} bind:value={newLimit} /> + {#if limitError} + {limitError} + {/if} diff --git a/src/routes/(console)/project-[region]-[project]/auth/security/usersLimit.test.ts b/src/routes/(console)/project-[region]-[project]/auth/security/usersLimit.test.ts new file mode 100644 index 000000000..ce4cc5886 --- /dev/null +++ b/src/routes/(console)/project-[region]-[project]/auth/security/usersLimit.test.ts @@ -0,0 +1,70 @@ +import { describe, expect, it } from 'vitest'; +import { + USERS_LIMIT_MAX, + USERS_LIMIT_MIN, + usersLimitChanged, + usersLimitError, + type UsersLimitMode +} from './usersLimit'; + +describe('usersLimitError', () => { + it('accepts whole numbers within range', () => { + expect(usersLimitError(USERS_LIMIT_MIN)).toBeNull(); + expect(usersLimitError(100)).toBeNull(); + expect(usersLimitError(USERS_LIMIT_MAX)).toBeNull(); + }); + + it('rejects zero, which the API reads as no limit', () => { + expect(usersLimitError(0)).not.toBeNull(); + }); + + it('rejects a cleared input', () => { + expect(usersLimitError(undefined)).not.toBeNull(); + expect(usersLimitError(NaN)).not.toBeNull(); + }); + + it('rejects values outside the accepted range', () => { + expect(usersLimitError(-1)).not.toBeNull(); + expect(usersLimitError(USERS_LIMIT_MAX + 1)).not.toBeNull(); + expect(usersLimitError(1.5)).not.toBeNull(); + }); +}); + +describe('usersLimitChanged', () => { + it('detects a new limit on a limited project', () => { + expect(usersLimitChanged('limited', 200, 100)).toBe(true); + expect(usersLimitChanged('limited', 100, 100)).toBe(false); + }); + + it('detects switching between limited and unlimited', () => { + expect(usersLimitChanged('limited', 100, 0)).toBe(true); + expect(usersLimitChanged('unlimited', 100, 100)).toBe(true); + expect(usersLimitChanged('unlimited', 100, 0)).toBe(false); + }); +}); + +describe('update button state', () => { + const isDisabled = (mode: UsersLimitMode, limit: number, total: number) => + (mode === 'limited' && usersLimitError(limit) !== null) || + !usersLimitChanged(mode, limit, total); + + it('explains a limit of 0 instead of silently blocking the update', () => { + expect(isDisabled('limited', 0, 0)).toBe(true); + expect(usersLimitError(0)).toContain('Unlimited'); + }); + + it('never submits a limit of 0 as a limit', () => { + expect(isDisabled('limited', 0, 100)).toBe(true); + }); + + it('enables the update for a valid change', () => { + expect(isDisabled('limited', 100, 0)).toBe(false); + expect(isDisabled('limited', 1, 100)).toBe(false); + expect(isDisabled('unlimited', 100, 100)).toBe(false); + }); + + it('ignores the limit field while unlimited is selected', () => { + expect(isDisabled('unlimited', 0, 100)).toBe(false); + expect(isDisabled('unlimited', 0, 0)).toBe(true); + }); +}); diff --git a/src/routes/(console)/project-[region]-[project]/auth/security/usersLimit.ts b/src/routes/(console)/project-[region]-[project]/auth/security/usersLimit.ts new file mode 100644 index 000000000..93a5aafd7 --- /dev/null +++ b/src/routes/(console)/project-[region]-[project]/auth/security/usersLimit.ts @@ -0,0 +1,17 @@ +export const USERS_LIMIT_MIN = 1; +export const USERS_LIMIT_MAX = 10000; + +export type UsersLimitMode = 'limited' | 'unlimited'; + +// The API reserves `total: 0` for "no limit", so `limited` cannot represent zero. +export function usersLimitError(limit: number): string | null { + if (!Number.isInteger(limit) || limit < USERS_LIMIT_MIN || limit > USERS_LIMIT_MAX) { + return `Enter a whole number between ${USERS_LIMIT_MIN} and ${USERS_LIMIT_MAX}, or select "Unlimited" to allow any number of users.`; + } + + return null; +} + +export function usersLimitChanged(mode: UsersLimitMode, limit: number, total: number): boolean { + return mode === 'limited' ? limit !== total : total !== 0; +}