From 216c7e1060b42837cc2c04b147a4023e32a589e7 Mon Sep 17 00:00:00 2001 From: Josh Mandel Date: Sun, 22 Nov 2026 15:36:31 -0600 Subject: [PATCH] Count the choices the wrong-display message lists The "Valid display is one of N choices: ..." message took N from Designations.displayCount(), which counts only the best kind of language match (exact, then language-region, then language), while the list comes from present(), which matches on language alone. For displayLanguage=en an en-GB display was listed but not counted ("one of 4 choices" followed by five); for en-GB the one-choice wording was used in front of every English display. present()'s selection is now in _presented(), and presentCount() returns its size; both validate.js sites take the number, and the _one/_other choice, from it. displayCount() still decides whether any valid display exists. Co-Authored-By: Claude --- tests/tx/designations.test.js | 14 ++ tests/tx/snomed-dialect-display.test.js | 9 ++ .../tx/validate-display-choice-count.test.js | 120 ++++++++++++++++++ tx/library/designations.js | 53 +++++--- tx/workers/validate.js | 18 ++- 5 files changed, 183 insertions(+), 31 deletions(-) create mode 100644 tests/tx/validate-display-choice-count.test.js diff --git a/tests/tx/designations.test.js b/tests/tx/designations.test.js index a12fc231..8d21620c 100644 --- a/tests/tx/designations.test.js +++ b/tests/tx/designations.test.js @@ -274,6 +274,20 @@ describe('Designations', () => { expect(presentation).toContain("'French term'"); expect(presentation).toContain("'German term'"); }); + + test('presentCount counts what present lists, across regions of the language', () => { + // displayCount stops at the best kind of match: for 'en' that is the plain 'en' + // designation, while present lists the 'en-US' one too + const langList = Languages.fromAcceptLanguage('en', languageDefinitions); + expect(designations.displayCount(langList, null, false)).toBe(1); + expect(designations.present(langList, null, false)).toBe("'English base' (en) or 'US English display' (en-US)"); + expect(designations.presentCount(langList, null, false)).toBe(2); + }); + + test('presentCount counts the fallback list when no language matches', () => { + const langList = Languages.fromAcceptLanguage('zh', languageDefinitions); + expect(designations.presentCount(langList, null, false)).toBe(4); + }); }); describe('Complex language preference scenarios', () => { diff --git a/tests/tx/snomed-dialect-display.test.js b/tests/tx/snomed-dialect-display.test.js index c815eeae..b726d84b 100644 --- a/tests/tx/snomed-dialect-display.test.js +++ b/tests/tx/snomed-dialect-display.test.js @@ -214,5 +214,14 @@ describe('SNOMED CT display by English dialect', () => { } } }); + + test('a wrong display is told the number of choices listed, GB synonym included', async () => { + const list = "'Hematoma' (en), 'Haematoma' (en-GB), 'Hematoma, NOS' (en), 'Haematoma, NOS' (en) or 'Hematoma (morphologic abnormality)' (en)"; + for (const lang of ['en', 'en-US', 'en-GB']) { + const r = await validate(US_FIRST, lang, 'Wrong'); + expect(r.result).toBe(false); + expect(r.message).toContain(`Valid display is one of 5 choices: ${list} (for the language(s) '${lang}')`); + } + }); }); }); diff --git a/tests/tx/validate-display-choice-count.test.js b/tests/tx/validate-display-choice-count.test.js new file mode 100644 index 00000000..f7507171 --- /dev/null +++ b/tests/tx/validate-display-choice-count.test.js @@ -0,0 +1,120 @@ +/** + * The wrong-display message "Valid display is one of N choices: ..." says how many + * choices it lists. The count used to come from Designations.displayCount(), which counts + * only the best kind of language match, while the list comes from present(), which matches + * by language alone: for 'en', a concept with displays in 'en' and 'en-GB' was reported as + * "one of 2 choices" followed by three, and for 'en-GB' the single-choice wording was used + * in front of a list of three. + */ + +const { ValidateWorker } = require('../../tx/workers/validate'); +const { CodeSystem } = require('../../tx/library/codesystem'); +const { FhirCodeSystemProvider } = require('../../tx/cs/cs-cs'); +const ValueSet = require('../../tx/library/valueset'); +const { Languages } = require('../../library/languages'); +const { OperationContext } = require('../../tx/operation-context'); +const { TestUtilities } = require('../test-utilities'); +const { TxParameters } = require('../../tx/params'); + +const mockLog = { debug: jest.fn(), info: jest.fn(), warn: jest.fn(), error: jest.fn() }; +const URL = 'http://example.org/devices'; + +function devicesCS() { + return new CodeSystem({ + resourceType: 'CodeSystem', url: URL, version: '1.0.0', name: 'Devices', status: 'active', + language: 'en', caseSensitive: true, content: 'complete', + concept: [ + { + code: 'analyzer', display: 'Oxygen analyzer', + designation: [ + { language: 'en', value: 'O2 analyzer' }, + { language: 'en-GB', value: 'Oxygen analyser' } + ] + }, + { + code: 'monitor', display: 'Monitor', + designation: [{ language: 'en-GB', value: 'Display unit' }] + }, + { code: 'pump', display: 'Pump' } + ] + }); +} + +function provider() { + return { + getCodeSystem: (c, url) => url === URL ? devicesCS().jsonObj : null, + getCodeSystemProvider: (c, url, version, supps) => + url === URL ? new FhirCodeSystemProvider(c, devicesCS(), supps || []) : null, + getCodeSystemById: () => null, + findValueSet: () => null, + getValueSetById: () => null, + loadSupplements: () => [] + }; +} + +function message(result) { + return (result.parameter.find(p => p.name === 'message') || {}).valueString; +} + +describe('wrong-display message counts the choices it lists', () => { + let worker, opContext, txp; + + beforeEach(async () => { + jest.clearAllMocks(); + const langDefs = await TestUtilities.loadLanguageDefinitions(); + const i18n = await TestUtilities.loadTranslations(langDefs); + opContext = new OperationContext(new Languages(), i18n); + worker = new ValidateWorker(opContext, mockLog, provider(), langDefs, i18n); + // validating = true, as $validate-code sets it: without it an implicit "*" is added to + // the working languages, every designation matches fully, and the bug does not show + txp = new TxParameters(opContext.i18n.languageDefinitions, opContext.i18n, true); + }); + + function language(lang) { + txp.readParams({ + resourceType: 'Parameters', + parameter: [{ name: '__Accept-Language', valueCode: lang }] + }); + } + + async function validateCS(code) { + const coded = { coding: [{ system: URL, code, display: 'Wrong' }] }; + const csp = new FhirCodeSystemProvider(opContext, devicesCS(), []); + return message(await worker.doValidationCS(coded, csp, txp, { issuePath: 'Coding', mode: 'coding' })); + } + + async function validateVS(code) { + const coded = { coding: [{ system: URL, code, display: 'Wrong' }] }; + const valueSet = new ValueSet({ + resourceType: 'ValueSet', url: 'http://example.org/ValueSet/devices', + compose: { include: [{ system: URL }] } + }); + return message(await worker.doValidationVS(coded, valueSet, txp, 'coded', 'Coding')); + } + + for (const [name, validate] of [['CodeSystem', validateCS], ['ValueSet', validateVS]]) { + test(`${name}: two 'en' displays and one 'en-GB' are three choices`, async () => { + language('en'); + const m = await validate('analyzer'); + expect(m).toContain("one of 3 choices: 'Oxygen analyzer' (en), 'O2 analyzer' (en) or 'Oxygen analyser' (en-GB)"); + }); + + test(`${name}: one 'en' display and one 'en-GB' are two choices`, async () => { + language('en'); + const m = await validate('monitor'); + expect(m).toContain("one of 2 choices: 'Monitor' (en) or 'Display unit' (en-GB)"); + }); + + test(`${name}: for 'en-GB', three listed choices are not worded as one`, async () => { + language('en-GB'); + const m = await validate('analyzer'); + expect(m).toContain("one of 3 choices: 'Oxygen analyzer' (en), 'O2 analyzer' (en) or 'Oxygen analyser' (en-GB)"); + }); + + test(`${name}: a single display is not counted`, async () => { + language('en'); + const m = await validate('pump'); + expect(m).toContain("Valid display is 'Pump' (en) (for the language(s) 'en')"); + }); + } +}); diff --git a/tx/library/designations.js b/tx/library/designations.js index 4a2c53a7..0f9542e3 100644 --- a/tx/library/designations.js +++ b/tx/library/designations.js @@ -482,45 +482,56 @@ class Designations { } /** - * Present all matching designations as a formatted string + * The designations present() lists: those in the requested languages (matched by + * language alone), or every designation when none of them is */ - present(langList, defLang, displayOnly) { - validateOptionalParameter(langList, 'langList', Languages, true); // Allow null - validateOptionalParameter(defLang, 'defLang', Language); - validateParameter(displayOnly, 'displayOnly', Boolean); - + _presented(langList, displayOnly) { const results = []; - let count = 0; // Collect matching designations for (const cd of this.designations) { if ((!displayOnly || this.isDisplay(cd)) && this._langsMatch(langList, cd.language, LangMatchType.LANG, null) && cd.value) { - count++; - if (cd.language) { - results.push(`'${cd.display}' (${cd.language.code})`); - } else { - results.push(`'${cd.display}'`); - } + results.push(cd); } } // If no language-specific matches, get all - if (count === 0) { + if (results.length === 0) { for (const cd of this.designations) { if ((!displayOnly || this.isDisplay(cd)) && cd.value) { - count++; - if (cd.language) { - results.push(`'${cd.display}' (${cd.language.code})`); - } else { - results.push(`'${cd.display}'`); - } + results.push(cd); } } } - return this._joinWithOr(results); + return results; + } + + /** + * Present all matching designations as a formatted string + */ + present(langList, defLang, displayOnly) { + validateOptionalParameter(langList, 'langList', Languages, true); // Allow null + validateOptionalParameter(defLang, 'defLang', Language); + validateParameter(displayOnly, 'displayOnly', Boolean); + + return this._joinWithOr(this._presented(langList, displayOnly).map(cd => + cd.language ? `'${cd.display}' (${cd.language.code})` : `'${cd.display}'`)); + } + + /** + * How many designations present() lists for the same arguments. This can be more than + * displayCount(), which counts only the best kind of language match: for 'en' it counts + * the 'en' designations and not an 'en-GB' one, where present() lists both. + */ + presentCount(langList, defLang, displayOnly) { + validateOptionalParameter(langList, 'langList', Languages, true); // Allow null + validateOptionalParameter(defLang, 'defLang', Language); + validateParameter(displayOnly, 'displayOnly', Boolean); + + return this._presented(langList, displayOnly).length; } /** diff --git a/tx/workers/validate.js b/tx/workers/validate.js index 208890d7..7853b2ee 100644 --- a/tx/workers/validate.js +++ b/tx/workers/validate.js @@ -1355,12 +1355,11 @@ class ValueSetChecker { baseMsg = 'NO_VALID_DISPLAY_AT_ALL'; m = this.worker.i18n.translate('NO_VALID_DISPLAY_AT_ALL', this.params.HTTPLanguages, [c.display, prov.system(), c.code, this.params.langSummary()]); - } else if (dc === 1) { - m = this.worker.i18n.translate(baseMsg + '_one', this.params.HTTPLanguages, - ['', prov.system(), c.code, list.present(this.params.workingLanguages(), defLang.value, true), c.display, this.params.langSummary()]); } else { - m = this.worker.i18n.translate(baseMsg + '_other', this.params.HTTPLanguages, - [dc.toString(), prov.system(), c.code, list.present(this.params.workingLanguages(), defLang.value, true), c.display, this.params.langSummary()]); + // the count in the message is the number of choices the message lists + const shown = list.presentCount(this.params.workingLanguages(), defLang.value, true); + m = this.worker.i18n.translate(baseMsg + (shown === 1 ? '_one' : '_other'), this.params.HTTPLanguages, + [shown === 1 ? '' : shown.toString(), prov.system(), c.code, list.present(this.params.workingLanguages(), defLang.value, true), c.display, this.params.langSummary()]); } msg(m); op.addIssue(new Issue(severity, 'invalid', addToPath(path, 'display'), baseMsg, m, 'invalid-display')); @@ -1672,12 +1671,11 @@ class ValueSetChecker { } } } - } else if (dc === 1) { - m = this.worker.i18n.translate(baseMsg + '_one', this.params.HTTPLanguages, - ['', c.system, c.code, list.present(this.params.workingLanguages(), defLang.value, dc > 0), c.display, this.params.langSummary()]); } else { - m = this.worker.i18n.translate(baseMsg + '_other', this.params.HTTPLanguages, - [dc.toString(), c.system, c.code, list.present(this.params.workingLanguages(), defLang.value, dc > 0), c.display, this.params.langSummary()]); + // the count in the message is the number of choices the message lists + const shown = list.presentCount(this.params.workingLanguages(), defLang.value, true); + m = this.worker.i18n.translate(baseMsg + (shown === 1 ? '_one' : '_other'), this.params.HTTPLanguages, + [shown === 1 ? '' : shown.toString(), c.system, c.code, list.present(this.params.workingLanguages(), defLang.value, true), c.display, this.params.langSummary()]); } msg(m); op.addIssue(new Issue(severity, 'invalid', addToPath(path, 'display'), mid, m, 'invalid-display'));