From 4c1dccf648a04b2a2b71a86d4d7591c689bc05ec Mon Sep 17 00:00:00 2001 From: Evie Gauthier Date: Fri, 20 Mar 2026 02:17:21 -0400 Subject: [PATCH] fix: case-sensitive abbreviation matching with word boundaries MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - buildAbbreviationsMap now stores exact-case keys (no .toLowerCase()) - splitByAbbreviations regex uses 'g' flag instead of 'gi' — user-defined casing is matched exactly (FOSS won't match 'foss' in text) - Word boundaries (\b) still prevent matching inside longer words - RoomAbbreviations settings page duplicate check updated to exact match - Add 19 unit tests covering edge cases: empty map, no-match, start/mid/end splits, case sensitivity, word boundaries, longest-match priority --- .../abbreviations/RoomAbbreviations.tsx | 2 +- src/app/utils/abbreviations.test.ts | 145 ++++++++++++++++++ src/app/utils/abbreviations.ts | 12 +- 3 files changed, 152 insertions(+), 7 deletions(-) create mode 100644 src/app/utils/abbreviations.test.ts diff --git a/src/app/features/room-settings/abbreviations/RoomAbbreviations.tsx b/src/app/features/room-settings/abbreviations/RoomAbbreviations.tsx index a9e7386f..daa76a0f 100644 --- a/src/app/features/room-settings/abbreviations/RoomAbbreviations.tsx +++ b/src/app/features/room-settings/abbreviations/RoomAbbreviations.tsx @@ -56,7 +56,7 @@ export function RoomAbbreviations({ requestClose }: AbbreviationsProps) { const definition = definitionInput.value.trim(); if (!term || !definition) return; - const alreadyExists = entries.some((e) => e.term.toLowerCase() === term.toLowerCase()); + const alreadyExists = entries.some((e) => e.term === term); if (alreadyExists) { termInput.setCustomValidity('This term already exists.'); termInput.reportValidity(); diff --git a/src/app/utils/abbreviations.test.ts b/src/app/utils/abbreviations.test.ts new file mode 100644 index 00000000..79b59519 --- /dev/null +++ b/src/app/utils/abbreviations.test.ts @@ -0,0 +1,145 @@ +import { describe, expect, it } from 'vitest'; + +import { buildAbbreviationsMap, splitByAbbreviations } from './abbreviations'; + +describe('buildAbbreviationsMap', () => { + it('returns an empty map for empty entries', () => { + expect(buildAbbreviationsMap([])).toEqual(new Map()); + }); + + it('stores the term with its original casing as the key', () => { + const map = buildAbbreviationsMap([{ term: 'FOSS', definition: 'Free and Open Source Software' }]); + expect(map.get('FOSS')).toBe('Free and Open Source Software'); + expect(map.get('foss')).toBeUndefined(); + }); + + it('trims surrounding whitespace from terms', () => { + const map = buildAbbreviationsMap([{ term: ' FOSS ', definition: 'Free and Open Source Software' }]); + expect(map.get('FOSS')).toBe('Free and Open Source Software'); + expect(map.size).toBe(1); + }); + + it('skips empty and whitespace-only terms', () => { + const map = buildAbbreviationsMap([ + { term: '', definition: 'ignored' }, + { term: ' ', definition: 'also ignored' }, + ]); + expect(map.size).toBe(0); + }); + + it('treats the same term with different casing as separate entries', () => { + const map = buildAbbreviationsMap([ + { term: 'OSS', definition: 'Open Source Software' }, + { term: 'oss', definition: 'open source software' }, + ]); + expect(map.size).toBe(2); + expect(map.get('OSS')).toBe('Open Source Software'); + expect(map.get('oss')).toBe('open source software'); + }); + + it('stores multiple distinct entries', () => { + const map = buildAbbreviationsMap([ + { term: 'FOSS', definition: 'Free and Open Source Software' }, + { term: 'RTFM', definition: 'Read The Fine Manual' }, + ]); + expect(map.size).toBe(2); + expect(map.get('FOSS')).toBe('Free and Open Source Software'); + expect(map.get('RTFM')).toBe('Read The Fine Manual'); + }); +}); + +describe('splitByAbbreviations', () => { + const map = buildAbbreviationsMap([ + { term: 'FOSS', definition: 'Free and Open Source Software' }, + { term: 'RTFM', definition: 'Read The Fine Manual' }, + { term: 'OSS', definition: 'Open Source Software' }, + ]); + + it('returns a single plain segment when the map is empty', () => { + expect(splitByAbbreviations('hello FOSS world', new Map())).toEqual([{ text: 'hello FOSS world' }]); + }); + + it('returns a single plain segment when there are no matches', () => { + expect(splitByAbbreviations('hello world', map)).toEqual([{ text: 'hello world' }]); + }); + + it('returns a single plain segment for an empty string', () => { + expect(splitByAbbreviations('', map)).toEqual([{ text: '' }]); + }); + + it('splits a term match in the middle of a string', () => { + expect(splitByAbbreviations('Use FOSS software', map)).toEqual([ + { text: 'Use ' }, + { text: 'FOSS', termKey: 'FOSS' }, + { text: ' software' }, + ]); + }); + + it('splits a term match at the start of a string', () => { + expect(splitByAbbreviations('FOSS is great', map)).toEqual([ + { text: 'FOSS', termKey: 'FOSS' }, + { text: ' is great' }, + ]); + }); + + it('splits a term match at the end of a string', () => { + expect(splitByAbbreviations('I love FOSS', map)).toEqual([ + { text: 'I love ' }, + { text: 'FOSS', termKey: 'FOSS' }, + ]); + }); + + it('handles multiple terms in the same string', () => { + expect(splitByAbbreviations('FOSS and RTFM', map)).toEqual([ + { text: 'FOSS', termKey: 'FOSS' }, + { text: ' and ' }, + { text: 'RTFM', termKey: 'RTFM' }, + ]); + }); + + it('is case-sensitive and does not match the wrong casing', () => { + expect(splitByAbbreviations('I like foss and Foss', map)).toEqual([ + { text: 'I like foss and Foss' }, + ]); + }); + + it('does not match a term that is a prefix of a longer word (word boundary)', () => { + // OSS is in the map, but should not match inside FOSS because the F provides no boundary + const ossOnlyMap = buildAbbreviationsMap([{ term: 'OSS', definition: 'Open Source Software' }]); + expect(splitByAbbreviations('FOSS rocks', ossOnlyMap)).toEqual([{ text: 'FOSS rocks' }]); + }); + + it('does not match a term embedded inside a longer word', () => { + // OSS should not match inside CROSS or GLOSS + const ossOnlyMap = buildAbbreviationsMap([{ term: 'OSS', definition: 'Open Source Software' }]); + expect(splitByAbbreviations('CROSS the GLOSS', ossOnlyMap)).toEqual([{ text: 'CROSS the GLOSS' }]); + }); + + it('matches a shorter term standalone when the longer overlapping term is also defined', () => { + // OSS is a suffix of FOSS; when standalone it should still match + expect(splitByAbbreviations('OSS is related to FOSS', map)).toEqual([ + { text: 'OSS', termKey: 'OSS' }, + { text: ' is related to ' }, + { text: 'FOSS', termKey: 'FOSS' }, + ]); + }); + + it('prefers the longer term when a shorter one is a suffix of it', () => { + // FOSS contains OSS; FOSS should win and OSS should not be matched separately + expect(splitByAbbreviations('Use FOSS today', map)).toEqual([ + { text: 'Use ' }, + { text: 'FOSS', termKey: 'FOSS' }, + { text: ' today' }, + ]); + }); + + it('preserves plain text between two consecutive matches', () => { + expect(splitByAbbreviations('FOSS, RTFM, OSS', map)).toEqual([ + { text: 'FOSS', termKey: 'FOSS' }, + { text: ', ' }, + { text: 'RTFM', termKey: 'RTFM' }, + { text: ', ' }, + { text: 'OSS', termKey: 'OSS' }, + ]); + }); +}); diff --git a/src/app/utils/abbreviations.ts b/src/app/utils/abbreviations.ts index 0c9a1437..87341191 100644 --- a/src/app/utils/abbreviations.ts +++ b/src/app/utils/abbreviations.ts @@ -7,12 +7,12 @@ export type RoomAbbreviationsContent = { entries: AbbreviationEntry[]; }; -/** Build a map of lowercase term → definition for O(1) lookup. */ +/** Build a map of exact-case term → definition for O(1) lookup. */ export const buildAbbreviationsMap = (entries: AbbreviationEntry[]): Map => { const map = new Map(); entries.forEach(({ term, definition }) => { const t = term.trim(); - if (t) map.set(t.toLowerCase(), definition); + if (t) map.set(t, definition); }); return map; }; @@ -20,10 +20,10 @@ export const buildAbbreviationsMap = (entries: AbbreviationEntry[]): Map) // Sort longest first so "HTTP/2" matches before "HTTP". const terms = [...abbrMap.keys()].sort((a, b) => b.length - a.length); const escaped = terms.map((t) => t.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')); - const pattern = new RegExp(`\\b(${escaped.join('|')})\\b`, 'gi'); + const pattern = new RegExp(`\\b(${escaped.join('|')})\\b`, 'g'); const segments: TextSegment[] = []; let lastIndex = 0; @@ -49,7 +49,7 @@ export const splitByAbbreviations = (text: string, abbrMap: Map) if (match.index > lastIndex) { segments.push({ text: text.slice(lastIndex, match.index) }); } - segments.push({ text: match[0], termKey: match[0].toLowerCase() }); + segments.push({ text: match[0], termKey: match[0] }); lastIndex = match.index + match[0].length; }