import { describe, expect, it } from 'vitest' import { escapeLikePattern, escapePostgrestLikePattern, isRecord, isUuid, } from '../src/guards' import { csvCell, parseCsvRows } from '../src/csv' import { formatBytes, formatDurationClockFromMs, formatDurationClockFromSeconds, } from '../src/format' import { createUnifiedSearchExcerpt, includesSearchQuery, mergeUnifiedSearchResults, } from '../src/unified-search' import { ADMIN_ROLE_RANK, hasMinimumAdminRole, normalizeAdminRoleAlias, } from '../src/admin-roles' import { formatTranscriptTimestamp } from '../src/meeting-transcript' import { formatDateFile, formatDateRange, formatTime } from '../src/utils/meeting-markdown' describe('코어 공용 유틸리티 계약 (W5-05~07)', () => { it('isRecord_객체와비객체구분_일반객체만참을반환한다', () => { expect(isRecord({ a: 1 })).toBe(true) expect(isRecord(null)).toBe(false) expect(isRecord(undefined)).toBe(false) expect(isRecord([1, 2])).toBe(false) expect(isRecord('text')).toBe(false) }) it('isUuid_유효한UUID와비정상문자열_표준RFC4122형식만참을반환한다', () => { expect(isUuid('550e8400-e29b-41d4-a716-446655440000')).toBe(true) expect(isUuid('00000000-0000-0000-0000-000000000000')).toBe(false) expect(isUuid('not-a-uuid')).toBe(false) expect(isUuid(123)).toBe(false) }) it('escapeLikePattern_와일드카드포함검색어_퍼센트와언더스코어를이스케이프한다', () => { expect(escapeLikePattern('100%_test\\ok')).toBe('100\\%\\_test\\\\ok') expect(escapePostgrestLikePattern('100%_*ok')).toBe('*100\\%\\_\\*ok*') }) it('csvCell과parseCsvRows_수식주입과줄바꿈포함_안전하게직렬화하고복원한다', () => { expect(csvCell('=CMD()')).toBe('"=CMD()"') expect(csvCell('hello, "world"')).toBe('"hello, ""world"""') const csv = `${csvCell('word')},${csvCell('pronunciation')}\r\n${csvCell('D3RO')},${csvCell('디에로, "보이스"')}\r\n` expect(parseCsvRows(csv)).toEqual([ ['word', 'pronunciation'], ['D3RO', '디에로, "보이스"'], ]) }) it('formatDurationClock_밀리초와초입력_시분초규칙에맞게포맷한다', () => { expect(formatDurationClockFromMs(65_000)).toBe('1:05') expect(formatDurationClockFromMs(5_000, { padMinutes: true })).toBe('00:05') expect(formatDurationClockFromSeconds(3_665, { showHours: true })).toBe('1:01:05') expect(formatDurationClockFromSeconds(125, { showHours: true })).toBe('2:05') expect(formatBytes(1536)).toBe('1.5 KB') }) it('unifiedSearch_발췌및병합_검색어주변문맥을추출하고최신순정렬한다', () => { expect(includesSearchQuery('Hello D3RO World', 'd3ro')).toBe(true) const excerpt = createUnifiedSearchExcerpt( `${'앞문장 '.repeat(30)}핵심키워드 등장 구간${' 뒷문장'.repeat(30)}`, '핵심키워드', 60, ) expect(excerpt).toContain('핵심키워드') expect(excerpt.startsWith('…')).toBe(true) const merged = mergeUnifiedSearchResults( [{ source: 'record', id: 'b', title: 'R', excerpt: 'r', matchedField: 'title', occurredAt: 100 }], [{ source: 'meeting', id: 'a', title: 'M', excerpt: 'm', matchedField: 'title', occurredAt: 200 }], 10, ) expect(merged.map((item) => item.id)).toEqual(['a', 'b']) }) it('adminRoles_권한계층비교_super_admin별칭과최소권한순서를일관되게판정한다', () => { expect(normalizeAdminRoleAlias('super_admin')).toBe('super_admin') expect(normalizeAdminRoleAlias('superadmin')).toBe('super_admin') expect(ADMIN_ROLE_RANK.manager).toBeLessThan(ADMIN_ROLE_RANK.admin) expect(ADMIN_ROLE_RANK.admin).toBeLessThan(ADMIN_ROLE_RANK.super_admin) expect(hasMinimumAdminRole('admin', 'manager')).toBe(true) expect(hasMinimumAdminRole('manager', 'admin')).toBe(false) expect(hasMinimumAdminRole('superadmin', 'super_admin')).toBe(true) }) it('parseCsvRows_단독CR줄바꿈과BOM포함입력_행으로정확히분리한다', () => { expect(parseCsvRows('\uFEFFword,note\rD3RO,"line1\r\nline2"')).toEqual([ ['word', 'note'], ['D3RO', 'line1\r\nline2'], ]) }) it('unifiedSearch_비정상limit와maxLength입력_음수slice나예외없이안전하게동작한다', () => { expect(createUnifiedSearchExcerpt('긴 본문 텍스트', '본문', 0)).toBe('') expect(createUnifiedSearchExcerpt('긴 본문 텍스트', '본문', -5)).toBe('') expect(createUnifiedSearchExcerpt('Hello world', ' ', 5)).toBe('Hell…') const r = [{ source: 'record' as const, id: '1', title: 'A', excerpt: 'a', matchedField: 'title', occurredAt: 100 }] const m = [{ source: 'meeting' as const, id: '2', title: 'B', excerpt: 'b', matchedField: 'title', occurredAt: 200 }] expect(mergeUnifiedSearchResults(r, m, 0)).toEqual([]) expect(mergeUnifiedSearchResults(r, m, -1)).toEqual([]) expect(mergeUnifiedSearchResults(r, m, Number.NaN)).toEqual([]) }) it('시간및날짜포맷터_음수와비유한수입력_NaN이나음수시각을노출하지않는다', () => { expect(formatDurationClockFromSeconds(-5)).toBe('0:00') expect(formatDurationClockFromSeconds(59.9, { rounding: 'round' })).toBe('1:00') expect(formatDurationClockFromMs(Number.NaN)).toBe('0:00') expect(formatTime(-500)).toBe('00:00') expect(formatTime(Number.NaN)).toBe('00:00') expect(formatTranscriptTimestamp(Number.NaN)).toBe('00:00') expect(formatDateFile(Number.NaN)).toBe('') expect(formatDateRange(Number.NaN, 1_700_000_000_000)).toBe('') }) })