|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect } from 'vitest'; |
| 4 | +import { SharingConfigSchema } from '@objectstack/spec/ui'; |
| 5 | +import { |
| 6 | + anonymousFormIntakeCandidates, |
| 7 | + anonymousFormIntakeSlug, |
| 8 | + anonymousFormIntakeSlugs, |
| 9 | + publicFormSlug, |
| 10 | +} from './anonymous-form-intake.js'; |
| 11 | + |
| 12 | +const OPEN = { enabled: true, allowAnonymous: true, publicLink: '/forms/contact-us' }; |
| 13 | + |
| 14 | +describe('anonymousFormIntakeSlug — which sharing opens a form to anonymous intake', () => { |
| 15 | + it('both switches on and a publicLink: open, slug normalised', () => { |
| 16 | + expect(anonymousFormIntakeSlug(OPEN)).toBe('contact-us'); |
| 17 | + expect(anonymousFormIntakeSlug({ ...OPEN, publicLink: 'forms/contact-us' })).toBe('contact-us'); |
| 18 | + expect(anonymousFormIntakeSlug({ ...OPEN, publicLink: 'contact-us' })).toBe('contact-us'); |
| 19 | + }); |
| 20 | + |
| 21 | + it.each<[string, Record<string, unknown>]>([ |
| 22 | + ['enabled: false', { ...OPEN, enabled: false }], |
| 23 | + ['enabled absent', { allowAnonymous: true, publicLink: '/forms/contact-us' }], |
| 24 | + ['allowAnonymous: false', { ...OPEN, allowAnonymous: false }], |
| 25 | + ['allowAnonymous absent', { enabled: true, publicLink: '/forms/contact-us' }], |
| 26 | + ['publicLink absent', { enabled: true, allowAnonymous: true }], |
| 27 | + ['publicLink empty', { ...OPEN, publicLink: '' }], |
| 28 | + ['a truthy non-boolean switch', { ...OPEN, enabled: 'true' }], |
| 29 | + ])('%s: closed', (_label, sharing) => { |
| 30 | + expect(anonymousFormIntakeSlug(sharing)).toBeNull(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('a raw body and its parse get the same answer (the schema defaults `enabled` to false)', () => { |
| 34 | + for (const raw of [OPEN, { allowAnonymous: true, publicLink: '/forms/contact-us' }, { ...OPEN, enabled: false }]) { |
| 35 | + expect(anonymousFormIntakeSlug(SharingConfigSchema.parse(raw))).toBe(anonymousFormIntakeSlug(raw)); |
| 36 | + } |
| 37 | + }); |
| 38 | + |
| 39 | + it('not an object: closed', () => { |
| 40 | + expect(anonymousFormIntakeSlug(undefined)).toBeNull(); |
| 41 | + expect(anonymousFormIntakeSlug(null)).toBeNull(); |
| 42 | + expect(anonymousFormIntakeSlug('x')).toBeNull(); |
| 43 | + }); |
| 44 | +}); |
| 45 | + |
| 46 | +describe('anonymousFormIntakeCandidates / anonymousFormIntakeSlugs — the three form shapes of a view', () => { |
| 47 | + const view = (sharing: Record<string, unknown>) => ({ |
| 48 | + name: 'inquiry.contact', |
| 49 | + object: 'inquiry', |
| 50 | + form: { data: { object: 'inquiry' }, sharing: { ...sharing, publicLink: '/forms/nested' } }, |
| 51 | + formViews: { |
| 52 | + a: { sharing: { ...sharing, publicLink: '/forms/a' } }, |
| 53 | + b: { sharing: { ...OPEN, enabled: false, publicLink: '/forms/b' } }, |
| 54 | + }, |
| 55 | + viewKind: 'form', |
| 56 | + config: { sharing: { ...sharing, publicLink: 'forms/flat' } }, |
| 57 | + }); |
| 58 | + |
| 59 | + it('scans the nested form, every formViews entry and the flattened config, open ones only', () => { |
| 60 | + const c = anonymousFormIntakeCandidates(view(OPEN)); |
| 61 | + expect(c.map((x) => [x.key, x.slug])).toEqual([ |
| 62 | + [undefined, 'nested'], |
| 63 | + ['a', 'a'], |
| 64 | + ['inquiry.contact', 'flat'], |
| 65 | + ]); |
| 66 | + expect(anonymousFormIntakeSlugs(view(OPEN))).toEqual(['a', 'flat', 'nested']); |
| 67 | + }); |
| 68 | + |
| 69 | + it('withdrawn through either switch: no candidate on any shape', () => { |
| 70 | + expect(anonymousFormIntakeSlugs(view({ ...OPEN, enabled: false }))).toEqual([]); |
| 71 | + expect(anonymousFormIntakeSlugs(view({ ...OPEN, allowAnonymous: false }))).toEqual([]); |
| 72 | + }); |
| 73 | + |
| 74 | + it('de-duplicates and sorts slugs; tolerates non-object input', () => { |
| 75 | + expect(anonymousFormIntakeSlugs({ formViews: { x: { sharing: OPEN }, y: { sharing: { ...OPEN, publicLink: 'contact-us' } } } })) |
| 76 | + .toEqual(['contact-us']); |
| 77 | + expect(anonymousFormIntakeSlugs(null)).toEqual([]); |
| 78 | + expect(anonymousFormIntakeSlugs({ formViews: { x: null } })).toEqual([]); |
| 79 | + expect(publicFormSlug('//forms/x')).toBe('x'); |
| 80 | + }); |
| 81 | +}); |
0 commit comments