Skip to content

Black Friday

Annual Pro is $90 for the first year, normally $120. Or start monthly at $6 for two months, then $12.

Ends Dec 4, 11:59 PM PT.

Create an account
← Public packages

@noah/mega-radio

MEGA Radio (the WAKE album) for members of the MEGA.dev Discord: sign in with Kody, verify with Discord, listen with repeat.

FollowForkOpen app

Files

page.test.ts

127 lines · 8.3 KB · TypeScript
import assert from 'node:assert/strict'
import { test } from 'node:test'
import { errorPage, renderPage, shortDate } from '../src/page.ts'
import { PLAYER_JS } from '../src/player.ts'
import { MEMBERSHIP_DAYS, MEMBERSHIP_TTL_MS, SIGNUP_URL } from '../src/config.ts'

test('player script parses', () => {
	assert.doesNotThrow(() => new Function(PLAYER_JS))
})

test('signed-out page offers Kody sign-in and the referral sign-up link', () => {
	const html = renderPage('/mega-radio', { kind: 'signed-out', loginHref: '/-/auth/login?app=mega-radio' })
	assert.ok(html.includes(SIGNUP_URL) && SIGNUP_URL === 'https://kody.codes/signup?ref=noah')
	assert.ok(!html.includes('/files/radio/'), 'no audio URLs before verification')
	assert.match(html, /<a class="pill kody" href="\/-\/auth\/login\?app=mega-radio">Sign in with Kody<\/a>/)
	assert.match(html, /class="pill ghost" href="https:\/\/kody\.codes\/signup\?ref=noah"[^>]*>Create a Free Account<\/a>/)
	assert.ok(html.includes('Sign in to listen') && html.includes("Sign in, then confirm you're in the MEGA.dev Discord."))
})

test('MEGA Radio name appears only in the header (no big h1, not in the sign-in title)', () => {
	for (const s of [
		{ kind: 'signed-out', loginHref: '/l' },
		{ kind: 'unverified', username: 'n', logoutHref: '/x' },
		{ kind: 'member', username: 'n', logoutHref: '/x', discordName: null, expiresAt: '2026-11-27T00:00:00Z' },
	] as const) {
		const html = renderPage('/mega-radio', s)
		assert.ok(!html.includes('<h1'), `${s.kind}: no h1`)
		assert.ok(html.includes('class="home" href="/mega-radio/">MEGA Radio</a>'))
	}
})

test('a Discord check is good for 30 days', () => {
	assert.equal(MEMBERSHIP_DAYS, 30)
	assert.equal(MEMBERSHIP_TTL_MS, 30 * 86_400_000)
	const html = renderPage('/mega-radio', { kind: 'unverified', username: 'n', logoutHref: '/x' })
	assert.ok(html.includes('A check is good for 30 days.'))
	assert.ok(renderPage('/mega-radio', { kind: 'signed-out', loginHref: '/l' }).includes('unlocks the radio for 30 days'))
})

test('member page has players and a repeat icon button, off by default', () => {
	const html = renderPage('/mega-radio', { kind: 'member', username: 'n', logoutHref: '/x', discordName: 'Noah', expiresAt: '2026-11-04T00:00:00Z' })
	assert.ok(html.includes('data-src="/mega-radio/files/radio/01.mp3"') && html.includes('data-src="/mega-radio/files/radio/02.mp3"') && html.includes('data-src="/mega-radio/files/radio/03.mp3"') && html.includes('data-src="/mega-radio/files/radio/04.mp3"') && html.includes('data-src="/mega-radio/files/radio/05.mp3"'))
	assert.ok(!html.includes('/files/radio/06.mp3'))
	assert.match(html, /<button type="button" class="icon-btn repeat-btn" data-repeat data-mode="off" aria-label="Repeat: off\. Press for repeat all\." title="Repeat: off">/)
	assert.equal((html.match(/data-repeat/g) ?? []).length, 1, 'one icon button')
	assert.match(html, /class="repeat-badge" aria-hidden="true">1</)
})

test('unverified page shows the Check with Discord button now that a client ID is set', () => {
	const html = renderPage('/mega-radio', { kind: 'unverified', username: 'n', logoutHref: '/x' })
	assert.ok(html.includes('/mega-radio/discord/start'))
	assert.ok(!html.includes('not set up yet'))
})

test('every step and state uses the same card and pill buttons', () => {
	const pillOnly = (html: string) => {
		assert.ok(html.includes('<section class="signin"'), 'step card')
		assert.ok(!/class="button|class="secondary|class="card"/.test(html), 'no old-style buttons or cards')
	}
	const u = (extra = {}) => renderPage('/mega-radio', { kind: 'unverified', username: 'n', logoutHref: '/x', ...extra })
	const first = u()
	pillOnly(first)
	assert.ok(first.includes('>Confirm your Discord</h2>') && first.includes("We'll check you're in the MEGA.dev Discord."))
	assert.match(first, /<a class="pill kody" href="\/mega-radio\/discord\/start">Check with Discord<\/a>/)
	assert.match(first, /class="muted signin-fine">We ask Discord once/)
	const nm = u({ notMember: { discordName: 'nk' } })
	pillOnly(nm)
	assert.match(nm, /class="pill kody" href="https:\/\/discord\.gg\/megadev"[^>]*>Join the MEGA\.dev Discord<\/a>/)
	assert.match(nm, /class="pill ghost" href="\/mega-radio\/discord\/start">Check again<\/a>/)
	const ex = u({ expired: true })
	pillOnly(ex)
	assert.ok(ex.includes('Check your Discord again') && ex.includes('more than 30 days old'))
	const er = u({ error: 'Discord <b>' })
	pillOnly(er)
	assert.ok(er.includes('Discord &lt;b&gt;') && er.includes('Something went wrong'))
	const mem = renderPage('/mega-radio', { kind: 'member', username: 'n', logoutHref: '/x', discordName: 'nk', expiresAt: '2026-11-27T00:00:00Z' })
	pillOnly(mem)
	assert.ok(!mem.includes('Verified') && !mem.includes('Re-check due') && !mem.includes('re-check due'))
	assert.match(mem, /<button type="button" class="pill danger" data-confirm-open="forget-dialog" aria-haspopup="dialog" aria-controls="forget-dialog">Forget My Discord Check…<\/button>/)
	assert.match(mem, /<dialog id="forget-dialog"/)
	assert.match(mem, /Forget Your Discord Check\?/)
	assert.match(mem, /<button type="submit" class="alert-cancel" autofocus>Cancel<\/button>/)
	assert.match(mem, /<button type="submit" class="alert-forget" form="forget-post">Forget<\/button>/)
	assert.match(mem, /<form id="forget-post" method="post" action="\/mega-radio\/forget"><\/form>/)
	assert.ok(!/<form method="post" action="\/mega-radio\/forget"><button/.test(mem), 'the pill does not POST by itself')
	assert.match(mem, />Sign Out<\/a>/)
	assert.ok(!mem.includes('>sign out<'))
	assert.match(mem, /<img class="track-cover" src="\/mega-radio\/files\/radio\/01\.png"/)
	assert.match(mem, /opens 2026-11-18/)
	assert.match(mem, /\.now-controls \.icon-btn \{[^}]*min-width: 44px[^}]*height: 44px[^}]*min-height: 44px/)
	assert.ok(!/\.now-controls \.icon-btn \{[^}]*height: 36px/.test(mem))
	const err = errorPage('/mega-radio', 'oops')
	pillOnly(err)
	assert.match(err, /class="pill kody" href="\/mega-radio\/">Start again<\/a>/)
	assert.ok(!err.includes('<h1'))
})

test('member intro replaces the plain intro and shows the expiry date', () => {
	const now = new Date('2026-10-28T16:00:00Z')
	const mem = renderPage('/mega-radio', { kind: 'member', username: 'n', logoutHref: '/x', discordName: 'Noah', expiresAt: '2026-11-27T16:00:00Z' }, now)
	assert.ok(mem.includes('<p class="muted intro">The WAKE album from the MEGA.dev course, for members of the <a href="https://discord.gg/megadev">MEGA.dev Discord</a>, which now includes you! You’re validated through Nov. 27.</p>'))
	assert.ok(!mem.includes('MEGA.dev Discord</a>.</p>'), 'plain intro gone for members')
	const un = renderPage('/mega-radio', { kind: 'unverified', username: 'n', logoutHref: '/x' }, now)
	assert.ok(un.includes('MEGA.dev Discord</a>.</p>') && !un.includes('which now includes you'))
})

test('shortDate: Mon. D in New York time, May without a period, year only when not this year', () => {
	const now = new Date('2026-10-28T16:00:00Z')
	assert.equal(shortDate('2026-11-27T16:00:00Z', now), 'Nov. 27')
	assert.equal(shortDate('2026-05-04T16:00:00Z', now), 'May 4')
	assert.equal(shortDate('2026-11-28T03:00:00Z', now), 'Nov. 27', '10pm New York the night before')
	assert.equal(shortDate('2027-01-03T16:00:00Z', now), 'Jan. 3, 2027')
})

test('play counts: after the duration, hover title and tap tooltip, on repeat at 3+', async () => {
	const { minGapMs, playsTip, onRepeat } = await import('../src/stats.ts')
	const html = renderPage('/mega-radio', { kind: 'member', username: 'n', logoutHref: '/x', discordName: 'N', expiresAt: '2026-11-27T00:00:00Z', plays: { 1: { total: 7, mine: 5 }, 2: { total: 1, mine: 0 } } })
	assert.match(html, /0:47 · <span class="plays-wrap"><button type="button" class="plays" data-plays data-total="7" data-mine="5" title="7 plays by MEGA.dev members \(5 are yours\)" aria-label="7 plays by MEGA.dev members \(5 are yours\)" aria-expanded="false" aria-controls="tip-01">▶ 7<\/button><span class="tip" id="tip-01" role="tooltip" hidden>/)
	assert.ok(html.includes('<span class="on-repeat" data-on-repeat> · you’ve got it on repeat (5)</span>'))
	assert.ok(html.includes('title="1 play by MEGA.dev members"'))
	assert.equal(onRepeat(2), '')
	assert.equal(playsTip({ total: 0, mine: 0 }), '0 plays by MEGA.dev members')
	assert.equal(minGapMs('0:47'), 42_000)
	assert.equal(minGapMs('3:20'), 180_000)
	assert.equal(minGapMs('0:20'), 30_000)
	assert.equal(minGapMs(null), 30_000)
})