import { describe, expect, it } from 'vitest' import { columnsForCounts, countColumns, countsCSV, countsDefinitionParams, countsResourcePath, countsRequestParams, normaliseCountsParams, selectedCountChildren } from '@/lib/counts' import { countsErrorMessage, countsConditionValue, countsNestedIsScalar, countsNestedScalarText } from '@/lib/countsDisplay' import { countsClipboard } from '@/lib/countsClipboard' import { createUII18n, translateUIMessage } from '@/lib/i18n' import { encodeCountState, readCountState, setCountState } from '@/lib/queryState' import { isCountsPath } from '@/lib/routeLocales' import { nameColumns, postDefinition } from '@/test/fixtures/counts' import type { CountsCondition } from '@/lib/countsDefinitions' describe ('Counts URL contract', () => { it ('distinguishes HTTP, network and internal failures without server details', () => { const http = { isAxiosError: true, response: { status: 503, data: '' } } expect (countsErrorMessage (http, 'ja').text).toContain ('HTTP 503') expect (countsErrorMessage (http, 'ja').text).not.toContain ('private SQL') expect (countsErrorMessage ({ isAxiosError: true }, 'ja').text) .toBe ('APIサーバーとの通信に失敗しました。') expect (countsErrorMessage (new Error ('private stack'), 'ja').text) .toBe ('内部処理で問題が発生しました。') }) it ('explains a totals timeout using the server time limit', () => { const failure = { isAxiosError: true, response: { status: 503, data: { error: 'counts_query_timeout', stage: 'totals', timeout_seconds: 30 } } } const text = countsErrorMessage (failure, 'ja').text expect (text).toContain ('集計処理が制限時間を超えました。') expect (text).toContain ('小計・総計の算出が30秒以内に完了しませんでした。') expect (text).toContain ('集計期間や行・列の項目を減らして') expect (countsErrorMessage (failure, 'ain-Kana').language).toBe ('ja') }) it ('uses hyphenated page paths for internal resource identifiers', () => { expect (countsResourcePath ('nico_tags')).toBe ('/counts/nico-tags') expect (countsResourcePath ('tag_descriptions')).toBe ('/counts/tag-descriptions') expect (countsResourcePath ('tag_groups')).toBe ('/counts/tag-groups') expect (countsResourcePath ('posts')).toBe ('/counts/posts') }) it ('defaults to latest summary, limit 100, and no axes', () => { const params = normaliseCountsParams ('posts', new URLSearchParams (), postDefinition) expect (params.get ('definition')).toBe ('latest') expect (params.get ('mode')).toBe ('summary') expect (params.get ('limit')).toBe ('100') expect (params.get ('rows')).toBe ('') expect (params.get ('cols')).toBe ('') expect (params.has ('axis')).toBe (false) expect (normaliseCountsParams ('posts', params, postDefinition).toString ()) .toBe (params.toString ()) }) it.each (['latest', 'history']) ('shares detail defaults for %s', definition => { const params = normaliseCountsParams ('posts', new URLSearchParams ({ definition, mode: 'details' }), postDefinition) expect (params.get ('sort')).toBe ('resource_id:asc version_no:asc') expect ([...selectedCountChildren (params)]).toEqual (['tags.name_ja']) expect (countsNestedIsScalar (nameColumns, 'tags', params)).toBe (true) }) it.each (['20', '50', '100']) ('preserves an explicit limit of %s and empty selections', limit => { const params = normaliseCountsParams ('posts', new URLSearchParams ({ mode: 'details', limit, sort: '', columns: '', children: '', rows: '', cols: '' }), postDefinition) for (const key of ['sort', 'columns', 'children', 'rows', 'cols']) expect (params.get (key)).toBe ('') expect (params.get ('limit')).toBe (limit) }) it ('normalises saved display keys without broadening script-specific predicates', () => { const params = new URLSearchParams ({ mode: 'details', columns: 'tags', children: 'tags.name_ain_kana tags.aliases_ja' }) setCountState (params, 'conditions', encodeCountState ('conditions', [ { field: 'name_ain_kana', operator: 'eq', value: 'recorded' }])) const result = normaliseCountsParams ('posts', params, postDefinition) expect (result.get ('children')).toBe ('tags.name_ain tags.aliases_ja') expect (result.get ('conditions.0.field')).toBe ('name_ain_kana') expect (countsNestedIsScalar (nameColumns, 'tags', result)).toBe (false) }) it ('unifies legacy drill conditions while keeping typed values, grain and execution time', () => { const params = new URLSearchParams ({ execution_at: '2024-03-01T00:00:00Z', timezone: 'Asia/Tokyo' }) const drills = [{ field: 'created_at', grain: 'month', value: '2024-02' }, { field: 'user', value: null }, { field: 'resource_id', value: 7 }, { field: 'flag', value: false }] setCountState (params, 'drill', encodeCountState ('drill', drills)) const result = normaliseCountsParams ('posts', params) expect (readCountState (result, 'conditions', [])) .toEqual (drills.map (entry => ({ ...entry, operator: 'axis_eq' }))) expect ([...result.keys ()].some (key => key === 'drill' || key.startsWith ('drill.'))) .toBe (false) expect (result.get ('execution_at')).toBe (params.get ('execution_at')) expect (result.get ('timezone')).toBe ('Asia/Tokyo') setCountState (result, 'conditions', encodeCountState ('conditions', [])) expect (readCountState (result, 'conditions', [])).toEqual ([]) }) it ('round-trips free text and empty sets without JSON or delimiter splitting', () => { const conditions: CountsCondition[] = [ { field: 'title', operator: 'eq', value: 'a & b = c "quoted"' }, { field: 'category', operator: 'in', input: 'list', values: [] }, { field: 'version_no', operator: 'in', values: [1, 2] }] const params = new URLSearchParams () setCountState (params, 'conditions', encodeCountState ('conditions', conditions)) expect (params.get ('conditions')).toBe ('') expect (params.get ('conditions.0.value')).toBe (conditions[0].value) expect (readCountState (params, 'conditions', [])).toEqual (conditions) setCountState (params, 'conditions', encodeCountState ('conditions', [conditions[0]])) expect ([...params.keys ()].some (key => key.startsWith ('condition_values'))).toBe (false) }) it ('resets definition-dependent state but preserves timezone and row-number preference', () => { const previous = new URLSearchParams ({ tags: 'alpha', rows: 'user', page: '4', at: '2024-01-01', execution_at: '2024-02-01', timezone: 'Asia/Tokyo', row_numbers: '0' }) expect (Object.fromEntries (countsDefinitionParams ('history', previous))).toEqual ({ definition: 'history', mode: 'details', timezone: 'Asia/Tokyo', row_numbers: '0' }) }) it ('excludes presentation settings from API identity but retains extraction conditions', () => { const params = new URLSearchParams ({ definition: 'history', columns: 'title', children: 'tags.name_ja', order: 'title', nested_json: '1', row_numbers: '1', resource_id: '7', limit: '50', sort: 'version_no:desc' }) expect (Object.fromEntries (countsRequestParams (params))).toEqual ({ definition: 'history', resource_id: '7', limit: '50', sort: 'version_no:desc' }) }) }) describe ('Counts logical display and exports', () => { it ('resolves output labels in both supported locales', () => { for (const locale of ['ja', 'ain-Kana']) for (const key of Object.values (countColumns)) { const message = translateUIMessage (key, 'counts', locale, {}, createUII18n (locale)) expect (message.text).not.toBe (key) expect (message.text).not.toContain ('counts:') } expect (columnsForCounts ('posts')).toContain ('event_type') expect (columnsForCounts ('tags')).not.toContain ('aliases') }) it ('distinguishes unresolved, inaccessible and registered condition names', () => { const axis = { field: 'tags' } expect (countsConditionValue ('internal:7', axis, 'ja', 'UTC')).toBe ('…') expect (countsConditionValue ('internal:7', axis, 'ja', 'UTC', { tags: {} })).toBe ('—') expect (countsConditionValue ('internal:7', axis, 'ja', 'UTC', { tags: { 'internal:7': 'recorded tag' } })).toBe ('recorded tag') expect (countsConditionValue (7, { field: 'user' }, 'ja', 'UTC', { user: { '7': 'actor' } })).toBe ('actor') expect (countsConditionValue ('title', { field: 'changed_field' }, 'ja', 'UTC')) .toBe (translateUIMessage ('columns.postTitle', 'counts', 'ja').text) }) it ('formats calendar grain values without changing their timezone or identifier', () => { expect (countsConditionValue ('2026', { field: 'created_at', grain: 'year' }, 'ja', 'Asia/Tokyo')).toContain ('令和8年') expect (countsConditionValue ('2026-10', { field: 'created_at', grain: 'month' }, 'ja', 'America/New_York')).toContain ('令和8年10月') }) it ('keeps all inner rows when choosing one display column', () => { const params = new URLSearchParams ({ children: 'tags.name_ja' }) const rows = [{ name_ja: 'alpha' }, { name_ja: 'beta' }] expect (countsNestedScalarText (rows, nameColumns, 'tags', params, 'ja')).toBe ('alpha beta') expect (rows).toEqual ([{ name_ja: 'alpha' }, { name_ja: 'beta' }]) }) it ('escapes CSV and clipboard formulas, quotes, newlines and HTML', () => { expect (countsCSV ([['日本語', 'a"b', 'a\nb', '=1+1']])) .toBe ('\uFEFF"日本語","a""b","a\nb","\'=1+1"') const copy = countsClipboard ([['', 'first\nsecond', '=1+1']]) expect (copy.text).toBe ('\t"first\nsecond"\t\'=1+1') expect (copy.html).toContain ('<tag>') expect (copy.html).toContain ('first
second') expect (copy.html).toContain (''=1+1') }) it.each (['/counts', '/counts/posts?q=1', '/ja/counts/tags', '/ain-Kana/counts/materials']) ( 'classifies %s as excluded from search and speculative loading', path => { expect (isCountsPath (path.split ('?')[0])).toBe (true) }) }) describe ('Counts safe failure messages', () => { it.each ([ [400, { error: 'too_many_axes', max_axes: 8 }, '最大8個'], [422, { error: 'too_many_columns', max_columns: 73 }, '最大73列'], [503, { error: 'counts_query_timeout' }, '時間制限'], [503, { error: 'export_worker_unavailable' }, 'ワーカー'], [400, {}, '入力条件'], [401, {}, '認証状態'], [403, {}, '権限']]) ( 'distinguishes HTTP %s with a known safe response', (status, data, expected) => { const failure = { isAxiosError: true, response: { status, data } } expect (countsErrorMessage (failure, 'ja').text).toContain (expected) }) it ('never renders arbitrary exception messages or private identifiers', () => { const failure = { isAxiosError: true, response: { status: 500, data: { error: 'SELECT private_title FROM secret_table', message: 'private-user-17' } } } const text = countsErrorMessage (failure, 'ja').text expect (text).not.toMatch (/SELECT|private|secret_table/) expect (countsErrorMessage ({ error: 'worker_stalled' }, 'ja').text).toContain ('停止') }) })