diff options
| author | Elizabeth Hunt <me@liz.coffee> | 2025-12-14 22:43:24 -0800 |
|---|---|---|
| committer | Elizabeth Hunt <me@liz.coffee> | 2025-12-14 22:43:24 -0800 |
| commit | cdb1a57614068fcfefa145bc6df45c9def7ccc6a (patch) | |
| tree | 92cadbecda8658c143b7625d5925e3411976a892 /test/types.test.ts | |
| parent | 6d318665a08c0d4564d8de23cc39425d2c0bac49 (diff) | |
| download | posthook-cdb1a57614068fcfefa145bc6df45c9def7ccc6a.tar.gz posthook-cdb1a57614068fcfefa145bc6df45c9def7ccc6a.zip | |
Updates
Diffstat (limited to 'test/types.test.ts')
| -rw-r--r-- | test/types.test.ts | 73 |
1 files changed, 73 insertions, 0 deletions
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); + }); +}); |
