aboutsummaryrefslogtreecommitdiff
path: root/test/types.test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'test/types.test.ts')
-rw-r--r--test/types.test.ts73
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);
+ });
+});