| 1 | import { readFileSync } from 'node:fs' |
| 2 | import { createRequire } from 'node:module' |
| 3 | import { join } from 'node:path' |
| 4 | import { describe, expect, it } from 'vitest' |
| 5 | |
| 6 | const require = createRequire(import.meta.url) |
| 7 | const ejs = require('ejs') as { |
| 8 | render: (template: string, data: Record<string, unknown>, options: Record<string, unknown>) => string |
| 9 | } |
| 10 | |
| 11 | const templatePath = join(process.cwd(), 'src/views/channels/auth/select-accounts.ejs') |
| 12 | const template = readFileSync(templatePath, 'utf8') |
| 13 | |
| 14 | function renderSelectAccounts(data: Record<string, unknown> = {}) { |
| 15 | return ejs.render(template, { |
| 16 | locale: 'en-US', |
| 17 | platformDisplayName: 'Facebook', |
| 18 | platformLogoUrl: '', |
| 19 | accounts: [], |
| 20 | ...data, |
| 21 | }, { filename: templatePath }) |
| 22 | } |
| 23 | |
| 24 | describe('channel auth select accounts view', () => { |
| 25 | it('renders configured empty account hint with an action link', () => { |
| 26 | const html = renderSelectAccounts({ |
| 27 | emptyAccountHint: { |
| 28 | title: 'No Facebook Page found', |
| 29 | description: 'Create a Facebook Page first, then authorize again.', |
| 30 | action: { |
| 31 | label: 'Create Facebook Page', |
| 32 | url: 'https://www.facebook.com/pages/create', |
| 33 | }, |
| 34 | }, |
| 35 | }) |
| 36 | |
| 37 | expect(html).toContain('No Facebook Page found') |
| 38 | expect(html).toContain('Create a Facebook Page first, then authorize again.') |
| 39 | expect(html).toContain('href="https://www.facebook.com/pages/create"') |
| 40 | expect(html).toContain('Create Facebook Page') |
| 41 | expect(html).toContain('target="_blank"') |
| 42 | expect(html).toContain('rel="noopener noreferrer"') |
| 43 | }) |
| 44 | |
| 45 | it('falls back to the default empty account copy without a hint', () => { |
| 46 | const html = renderSelectAccounts() |
| 47 | |
| 48 | expect(html).toContain('No accounts available') |
| 49 | expect(html).toContain('This authorization did not return any accounts that can be connected.') |
| 50 | expect(html).not.toContain('class="empty-action"') |
| 51 | }) |
| 52 | |
| 53 | it('renders selectable account identity as checkbox data attributes', () => { |
| 54 | const html = renderSelectAccounts({ |
| 55 | accounts: [{ |
| 56 | platformUid: 'google-user-1', |
| 57 | account: 'channel-1', |
| 58 | displayName: 'Channel One', |
| 59 | }], |
| 60 | }) |
| 61 | |
| 62 | expect(html).toContain('name="selectedAccounts"') |
| 63 | expect(html).toContain('value="google-user-1"') |
| 64 | expect(html).toContain('data-platform-uid="google-user-1"') |
| 65 | expect(html).toContain('data-account="channel-1"') |
| 66 | expect(html).not.toContain('name="accounts"') |
| 67 | expect(html).not.toContain('{"platformUid"') |
| 68 | }) |
| 69 | }) |
| 70 |