From cdb1a57614068fcfefa145bc6df45c9def7ccc6a Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Sun, 14 Dec 2025 22:43:24 -0800 Subject: Updates --- test/types.test.ts | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 test/types.test.ts (limited to 'test/types.test.ts') diff --git a/test/types.test.ts b/test/types.test.ts new file mode 100644 index 0000000..1823c15 --- /dev/null +++ b/test/types.test.ts @@ -0,0 +1,73 @@ +import { describe, expect, it } from 'vitest'; + +import { ContentType, isRouteConfig, isSafeRouteName } from '../src/types/index.js'; + +describe('isSafeRouteName', () => { + it('accepts typical route names', () => { + expect(isSafeRouteName('route1')).toBe(true); + expect(isSafeRouteName('Route_1')).toBe(true); + expect(isSafeRouteName('route-1')).toBe(true); + }); + + it('enforces length and character rules', () => { + expect(isSafeRouteName('a'.repeat(64))).toBe(true); + expect(isSafeRouteName('a'.repeat(65))).toBe(false); + expect(isSafeRouteName('bad!name')).toBe(false); + }); + + it('rejects unsafe or ambiguous names', () => { + expect(isSafeRouteName('')).toBe(false); + expect(isSafeRouteName(' route')).toBe(false); + expect(isSafeRouteName('route ')).toBe(false); + expect(isSafeRouteName('.')).toBe(false); + expect(isSafeRouteName('..')).toBe(false); + expect(isSafeRouteName('foo/bar')).toBe(false); + expect(isSafeRouteName('foo\\bar')).toBe(false); + }); +}); + +describe('isRouteConfig', () => { + it('accepts a valid configuration', () => { + const config = { + name: 'route1', + contentType: ContentType.JSON, + hcaptchaProtected: false, + ntfy: { enabled: false }, + requireToken: true, + }; + + expect(isRouteConfig(config)).toBe(true); + }); + + it('requires an hCaptcha secret when protected', () => { + const config = { + name: 'route1', + contentType: ContentType.JSON, + hcaptchaProtected: true, + }; + + expect(isRouteConfig(config)).toBe(false); + }); + + it('validates ntfy config when enabled', () => { + const config = { + name: 'route1', + contentType: ContentType.JSON, + hcaptchaProtected: false, + ntfy: { enabled: true }, + }; + + expect(isRouteConfig(config)).toBe(false); + }); + + it('validates requireToken type when present', () => { + const config = { + name: 'route1', + contentType: ContentType.JSON, + hcaptchaProtected: false, + requireToken: 'yes', + }; + + expect(isRouteConfig(config)).toBe(false); + }); +}); -- cgit v1.2.3-70-g09d2