42 lines
2.0 KiB
JavaScript
42 lines
2.0 KiB
JavaScript
import test from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import { formatAnalyticsCount, formatRealtimeDuration, getActivePageHref, getActivePageTitle, getRealtimePages } from '../lib/analytics-display.js'
|
|
|
|
test('통계 수치는 천 단위 쉼표와 빈 값 보정을 제공한다', () => {
|
|
assert.equal(formatAnalyticsCount(10413), '10,413')
|
|
assert.equal(formatAnalyticsCount('1234567'), '1,234,567')
|
|
assert.equal(formatAnalyticsCount(null), '0')
|
|
assert.equal(formatAnalyticsCount(NaN), '0')
|
|
})
|
|
|
|
test('실시간 페이지는 최근 확인순으로 같은 경로를 중복 제거한다', () => {
|
|
const pages = getRealtimePages({ pages: [
|
|
{ path: '/post/a', postTitle: '이전 제목', lastSeenAt: '2026-10-21T00:00:00Z' },
|
|
{ path: '/post/b', postTitle: '두 번째', lastSeenAt: '2026-10-21T01:00:00Z' },
|
|
{ path: '/post/a', postTitle: '최신 제목', lastSeenAt: '2026-10-21T02:00:00Z' }
|
|
] })
|
|
assert.equal(pages.length, 2)
|
|
assert.equal(pages[0].postTitle, '최신 제목')
|
|
assert.equal(getActivePageTitle({ path: '/' }), '홈')
|
|
assert.equal(getRealtimePages({ path: '/', pages: [] }).length, 1)
|
|
})
|
|
|
|
test('긴 접속 시간은 시간·일 단위로 짧게 표시한다', () => {
|
|
assert.equal(formatRealtimeDuration(59), '59초')
|
|
assert.equal(formatRealtimeDuration(1861), '31분 1초')
|
|
assert.equal(formatRealtimeDuration(3600), '1시간')
|
|
assert.equal(formatRealtimeDuration(4920), '1시간 22분')
|
|
assert.equal(formatRealtimeDuration(172800), '2일')
|
|
assert.equal(formatRealtimeDuration(176400), '2일 1시간')
|
|
assert.equal(formatRealtimeDuration(-1), '0초')
|
|
assert.equal(formatRealtimeDuration(Infinity), '0초')
|
|
})
|
|
|
|
test('실시간 바로가기는 공개 내부 경로만 사용한다', () => {
|
|
assert.equal(getActivePageHref({ postSlug: '한글 제목' }), '/post/%ED%95%9C%EA%B8%80%20%EC%A0%9C%EB%AA%A9')
|
|
assert.equal(getActivePageHref({ path: '/' }), '/')
|
|
for (const path of ['//example.com', '/\\example.com', '/admin', '/admin/settings', 'https://example.com']) {
|
|
assert.equal(getActivePageHref({ path }), '')
|
|
}
|
|
})
|