Barazo AppView backend barazo.forum
at main 118 lines 4.4 kB view raw
1import { describe, it, expect } from 'vitest' 2import { 3 resolveMaxMaturity, 4 maturityAllows, 5 allowedRatings, 6} from '../../../src/lib/content-filter.js' 7import type { MaturityUser } from '../../../src/lib/content-filter.js' 8import type { MaturityRating } from '../../../src/lib/maturity.js' 9 10// --------------------------------------------------------------------------- 11// resolveMaxMaturity 12// --------------------------------------------------------------------------- 13 14describe('resolveMaxMaturity', () => { 15 it("returns 'safe' for unauthenticated user", () => { 16 expect(resolveMaxMaturity(undefined)).toBe('safe') 17 }) 18 19 it("returns 'safe' when declaredAge is null", () => { 20 const user: MaturityUser = { declaredAge: null, maturityPref: 'mature' } 21 expect(resolveMaxMaturity(user)).toBe('safe') 22 }) 23 24 it("returns 'safe' when declaredAge is 0 (rather not say)", () => { 25 const user: MaturityUser = { declaredAge: 0, maturityPref: 'mature' } 26 expect(resolveMaxMaturity(user)).toBe('safe') 27 }) 28 29 it('returns maturityPref when declaredAge meets default threshold (16)', () => { 30 const user: MaturityUser = { declaredAge: 16, maturityPref: 'mature' } 31 expect(resolveMaxMaturity(user, 16)).toBe('mature') 32 }) 33 34 it("returns 'safe' when declaredAge below community threshold", () => { 35 const user: MaturityUser = { declaredAge: 14, maturityPref: 'mature' } 36 expect(resolveMaxMaturity(user, 16)).toBe('safe') 37 }) 38 39 it('returns maturityPref when declaredAge meets lower threshold (13)', () => { 40 const user: MaturityUser = { declaredAge: 13, maturityPref: 'mature' } 41 expect(resolveMaxMaturity(user, 13)).toBe('mature') 42 }) 43 44 it("returns 'safe' when declaredAge is 13 and threshold is 14", () => { 45 const user: MaturityUser = { declaredAge: 13, maturityPref: 'mature' } 46 expect(resolveMaxMaturity(user, 14)).toBe('safe') 47 }) 48 49 it('defaults threshold to 16 when not provided', () => { 50 const user: MaturityUser = { declaredAge: 16, maturityPref: 'mature' } 51 expect(resolveMaxMaturity(user)).toBe('mature') 52 }) 53 54 it("returns 'safe' when declaredAge is undefined", () => { 55 const user: MaturityUser = { declaredAge: undefined, maturityPref: 'adult' } 56 expect(resolveMaxMaturity(user)).toBe('safe') 57 }) 58 59 it("returns 'adult' when declaredAge meets threshold and pref is adult", () => { 60 const user: MaturityUser = { declaredAge: 18, maturityPref: 'adult' } 61 expect(resolveMaxMaturity(user, 16)).toBe('adult') 62 }) 63 64 it("returns 'safe' when declaredAge meets threshold but pref is safe", () => { 65 const user: MaturityUser = { declaredAge: 18, maturityPref: 'safe' } 66 expect(resolveMaxMaturity(user, 16)).toBe('safe') 67 }) 68}) 69 70// --------------------------------------------------------------------------- 71// maturityAllows 72// --------------------------------------------------------------------------- 73 74describe('maturityAllows', () => { 75 const cases: Array<[MaturityRating, MaturityRating, boolean]> = [ 76 // [maxAllowed, contentRating, expected] 77 ['safe', 'safe', true], 78 ['safe', 'mature', false], 79 ['safe', 'adult', false], 80 ['mature', 'safe', true], 81 ['mature', 'mature', true], 82 ['mature', 'adult', false], 83 ['adult', 'safe', true], 84 ['adult', 'mature', true], 85 ['adult', 'adult', true], 86 ] 87 88 for (const [maxAllowed, contentRating, expected] of cases) { 89 it(`maxAllowed=${maxAllowed}, content=${contentRating} -> ${String(expected)}`, () => { 90 expect(maturityAllows(maxAllowed, contentRating)).toBe(expected) 91 }) 92 } 93}) 94 95// --------------------------------------------------------------------------- 96// allowedRatings 97// --------------------------------------------------------------------------- 98 99describe('allowedRatings', () => { 100 it("returns only 'safe' for safe max level", () => { 101 expect(allowedRatings('safe')).toEqual(['safe']) 102 }) 103 104 it("returns 'safe' and 'mature' for mature max level", () => { 105 const result = allowedRatings('mature') 106 expect(result).toHaveLength(2) 107 expect(result).toContain('safe') 108 expect(result).toContain('mature') 109 }) 110 111 it('returns all ratings for adult max level', () => { 112 const result = allowedRatings('adult') 113 expect(result).toHaveLength(3) 114 expect(result).toContain('safe') 115 expect(result).toContain('mature') 116 expect(result).toContain('adult') 117 }) 118})