service-badges.test.ts82 lines · main
| 1 | import { describe, expect, test } from 'bun:test'; |
| 2 | |
| 3 | import { |
| 4 | isMintableServiceBadgeProduct, |
| 5 | isServiceBadgeProduct, |
| 6 | isServiceBadgeRole, |
| 7 | looksLikeServiceBadge, |
| 8 | serviceBadgeAllowedOnRoute, |
| 9 | SERVICE_BADGE_PREFIX, |
| 10 | } from './service-badges.js'; |
| 11 | |
| 12 | describe('service badge product helpers', () => { |
| 13 | test('accepts known products', () => { |
| 14 | expect(isServiceBadgeProduct('db')).toBe(true); |
| 15 | expect(isServiceBadgeProduct('s3')).toBe(true); |
| 16 | expect(isServiceBadgeProduct('auth')).toBe(true); |
| 17 | expect(isServiceBadgeProduct('pay')).toBe(true); |
| 18 | }); |
| 19 | |
| 20 | test('rejects unknown products', () => { |
| 21 | expect(isServiceBadgeProduct('mcp')).toBe(false); |
| 22 | expect(isServiceBadgeProduct('')).toBe(false); |
| 23 | }); |
| 24 | |
| 25 | test('pay is reserved — not mintable yet', () => { |
| 26 | expect(isMintableServiceBadgeProduct('db')).toBe(true); |
| 27 | expect(isMintableServiceBadgeProduct('s3')).toBe(true); |
| 28 | expect(isMintableServiceBadgeProduct('auth')).toBe(true); |
| 29 | expect(isMintableServiceBadgeProduct('pay')).toBe(false); |
| 30 | }); |
| 31 | |
| 32 | test('roles are viewer | developer | admin', () => { |
| 33 | expect(isServiceBadgeRole('viewer')).toBe(true); |
| 34 | expect(isServiceBadgeRole('developer')).toBe(true); |
| 35 | expect(isServiceBadgeRole('admin')).toBe(true); |
| 36 | expect(isServiceBadgeRole('owner')).toBe(false); |
| 37 | }); |
| 38 | }); |
| 39 | |
| 40 | describe('looksLikeServiceBadge', () => { |
| 41 | test('matches product prefixes', () => { |
| 42 | expect(looksLikeServiceBadge(`${SERVICE_BADGE_PREFIX.db}abc`)).toBe(true); |
| 43 | expect(looksLikeServiceBadge(`${SERVICE_BADGE_PREFIX.s3}abc`)).toBe(true); |
| 44 | expect(looksLikeServiceBadge(`${SERVICE_BADGE_PREFIX.auth}abc`)).toBe(true); |
| 45 | }); |
| 46 | |
| 47 | test('rejects other key shapes', () => { |
| 48 | expect(looksLikeServiceBadge('brk_abc')).toBe(false); |
| 49 | expect(looksLikeServiceBadge('pk_briven_mcp_abc')).toBe(false); |
| 50 | expect(looksLikeServiceBadge('m2m_abc')).toBe(false); |
| 51 | }); |
| 52 | }); |
| 53 | |
| 54 | describe('serviceBadgeAllowedOnRoute — product walls', () => { |
| 55 | test('session / brk_ (no badge product) may enter any wall', () => { |
| 56 | expect(serviceBadgeAllowedOnRoute(null, 'db')).toBe(true); |
| 57 | expect(serviceBadgeAllowedOnRoute(null, 's3')).toBe(true); |
| 58 | expect(serviceBadgeAllowedOnRoute(null, 'auth')).toBe(true); |
| 59 | expect(serviceBadgeAllowedOnRoute(undefined, 'db')).toBe(true); |
| 60 | }); |
| 61 | |
| 62 | test('db badge only opens Doltgres wall', () => { |
| 63 | expect(serviceBadgeAllowedOnRoute('db', 'db')).toBe(true); |
| 64 | expect(serviceBadgeAllowedOnRoute('db', 's3')).toBe(false); |
| 65 | expect(serviceBadgeAllowedOnRoute('db', 'auth')).toBe(false); |
| 66 | }); |
| 67 | |
| 68 | test('s3 badge only opens S3 wall', () => { |
| 69 | expect(serviceBadgeAllowedOnRoute('s3', 's3')).toBe(true); |
| 70 | expect(serviceBadgeAllowedOnRoute('s3', 'db')).toBe(false); |
| 71 | }); |
| 72 | |
| 73 | test('auth badge only opens Auth wall', () => { |
| 74 | expect(serviceBadgeAllowedOnRoute('auth', 'auth')).toBe(true); |
| 75 | expect(serviceBadgeAllowedOnRoute('auth', 'db')).toBe(false); |
| 76 | }); |
| 77 | |
| 78 | test('no badge may open routeProduct=any (everything)', () => { |
| 79 | expect(serviceBadgeAllowedOnRoute('db', 'any')).toBe(false); |
| 80 | expect(serviceBadgeAllowedOnRoute(null, 'any')).toBe(true); |
| 81 | }); |
| 82 | }); |