diff --git a/projects/ngx-coding-components/src/lib/dialogs/select-code-rule-reference-dialog.component.spec.ts b/projects/ngx-coding-components/src/lib/dialogs/select-code-rule-reference-dialog.component.spec.ts index ca564347..c40b5c1b 100644 --- a/projects/ngx-coding-components/src/lib/dialogs/select-code-rule-reference-dialog.component.spec.ts +++ b/projects/ngx-coding-components/src/lib/dialogs/select-code-rule-reference-dialog.component.spec.ts @@ -35,13 +35,28 @@ describe('SelectCodeRuleReferenceDialogComponent', () => { expect(component.newValue).toBe(0); }); - it('should default selection to ANY when value is ANY (or falsy)', () => { + it('should select ANY when it is explicitly stored', () => { const { component } = createComponent({ isFragmentMode: false, value: 'ANY' }); expect(component.newSelection).toEqual(['ANY']); expect(component.newValue).toBe(0); }); + [undefined, -1].forEach(value => { + it(`preserves the legacy array reference ${value} when saving unchanged`, () => { + const { component, dialogRef } = createComponent({ isFragmentMode: false, value }); + expect(component.newSelection).toEqual(['legacy']); + component.okButtonClick(); + expect(dialogRef.close).toHaveBeenCalledWith(false); + }); + }); + + it('keeps the existing ANY default for fragment references', () => { + const { component } = createComponent({ isFragmentMode: true }); + expect(component.newSelection).toEqual(['ANY']); + expect(component.hasLegacyReference).toBeFalse(); + }); + it('okButtonClick should close with newValue - 1 when specific is selected', () => { const { component, dialogRef } = createComponent({ isFragmentMode: true, value: 1 }); @@ -96,6 +111,25 @@ describe('SelectCodeRuleReferenceDialogComponent UI', () => { const getHelp = (): HTMLElement | null => fixture.nativeElement.querySelector('#array-reference-help'); const save = () => fixture.nativeElement.querySelector('button[color="primary"]').click(); + it('shows the existing evaluation instead of silently selecting ANY for an omitted reference', async () => { + dialogData.value = undefined; + const list = await renderDialog(); + expect((await list.getItems()).length).toBe(6); + await expectAsync((await list.getItems({ selected: true }))[0].getText()) + .toBeResolvedTo('Bisherige Auswertung beibehalten'); + expect(getHelp()?.textContent).toContain('Bei UND dürfen die Regeln auf unterschiedliche Werte zutreffen'); + save(); + expect(dialogRef.close).toHaveBeenCalledWith(false); + }); + + it('changes a legacy reference to ANY only after explicitly selecting it', async () => { + dialogData.value = undefined; + const list = await renderDialog(); + await list.selectItems({ text: 'Alle vorhandenen Werte müssen passen' }); + save(); + expect(dialogRef.close).toHaveBeenCalledWith('ANY'); + }); + it('explains why an additional non-matching value prevents ANY from matching', async () => { const list = await renderDialog(); diff --git a/projects/ngx-coding-components/src/lib/dialogs/select-code-rule-reference-dialog.component.ts b/projects/ngx-coding-components/src/lib/dialogs/select-code-rule-reference-dialog.component.ts index 4712d50b..c9ead98e 100644 --- a/projects/ngx-coding-components/src/lib/dialogs/select-code-rule-reference-dialog.component.ts +++ b/projects/ngx-coding-components/src/lib/dialogs/select-code-rule-reference-dialog.component.ts @@ -14,7 +14,7 @@ import { MatSelectionList, MatListOption } from '@angular/material/list'; export interface SelectCodeRuleReferenceDialogData { isFragmentMode: boolean; - value: number | 'ANY' | 'ANY_OPEN' | 'SUM' | 'LENGTH' ; + value?: number | 'ANY' | 'ANY_OPEN' | 'SUM' | 'LENGTH' ; } @Component({ @@ -24,7 +24,13 @@ export interface SelectCodeRuleReferenceDialogData {
{{(refData.isFragmentMode ? 'rule' : 'rule-set') + '.reference.prompt' | translate}}
+ (newSelection[0] === 'ANY' || newSelection[0] === 'ANY_OPEN' || newSelection[0] === 'legacy') ? + 'array-reference-help' : null"> + @if (hasLegacyReference) { + + {{'rule-set.reference.legacy' | translate}} + + } {{(refData.isFragmentMode ? 'rule' : 'rule-set') + '.reference.any' | translate}} @@ -47,10 +53,12 @@ export interface SelectCodeRuleReferenceDialogData { - @if (!refData.isFragmentMode && (newSelection[0] === 'ANY' || newSelection[0] === 'ANY_OPEN')) { + @if (!refData.isFragmentMode && + (newSelection[0] === 'ANY' || newSelection[0] === 'ANY_OPEN' || newSelection[0] === 'legacy')) {

- {{(newSelection[0] === 'ANY' ? 'rule-set.reference.any-help' : - 'rule-set.reference.any-open-help') | translate}} + {{(newSelection[0] === 'legacy' ? 'rule-set.reference.legacy-help' : + newSelection[0] === 'ANY' ? 'rule-set.reference.any-help' : + 'rule-set.reference.any-open-help') | translate}}

} @@ -86,12 +94,18 @@ export interface SelectCodeRuleReferenceDialogData { export class SelectCodeRuleReferenceDialogComponent { newValue: number; newSelection: string[]; + readonly hasLegacyReference: boolean; constructor( @Inject(MAT_DIALOG_DATA) public refData: SelectCodeRuleReferenceDialogData, public dialogRef: MatDialogRef ) { - if (typeof this.refData.value === 'number') { + this.hasLegacyReference = !refData.isFragmentMode && + (refData.value === undefined || (typeof refData.value === 'number' && refData.value < 0)); + if (this.hasLegacyReference) { + this.newValue = 0; + this.newSelection = ['legacy']; + } else if (typeof this.refData.value === 'number') { this.newValue = this.refData.value + 1; this.newSelection = ['specific']; } else { @@ -101,6 +115,11 @@ export class SelectCodeRuleReferenceDialogComponent { } okButtonClick(): void { + // Keep the original absent/legacy reference; false leaves the caller's rule set unchanged. + if (this.newSelection[0] === 'legacy') { + this.dialogRef.close(false); + return; + } const result = this.newSelection[0] === 'specific' ? this.newValue - 1 : this.newSelection[0]; this.dialogRef.close(result); } diff --git a/projects/ngx-coding-components/src/lib/services/schemer-code-ops.ts b/projects/ngx-coding-components/src/lib/services/schemer-code-ops.ts index cbf47795..5bb24221 100644 --- a/projects/ngx-coding-components/src/lib/services/schemer-code-ops.ts +++ b/projects/ngx-coding-components/src/lib/services/schemer-code-ops.ts @@ -105,11 +105,18 @@ export const getPasteSingleCodeWarningKeys = ( return Array.from(warningKeys); }; +export const createDefaultRuleSet = (multiple = false): RuleSet => ({ + ...(multiple ? { valueArrayPos: 'ANY' as const } : {}), + ruleOperatorAnd: false, + rules: [{ method: 'MATCH', parameters: [''] }] +}); + export const addCode = ( codeList: CodeData[], codeType: CodeType, userRole: UserRoleType, - orderOfCodeTypes: CodeType[] + orderOfCodeTypes: CodeType[], + multiple = false ): CodeData | string => { if (!canEdit(userRole)) return 'code.error-message.no-access'; @@ -195,17 +202,7 @@ export const addCode = ( label: '', score: codeType === 'FULL_CREDIT' ? 1 : 0, ruleSetOperatorAnd: true, - ruleSets: [ - { - ruleOperatorAnd: false, - rules: [ - { - method: 'MATCH', - parameters: [''] - } - ] - } - ], + ruleSets: [createDefaultRuleSet(multiple)], manualInstruction: '' }; diff --git a/projects/ngx-coding-components/src/lib/services/schemer.service.spec.ts b/projects/ngx-coding-components/src/lib/services/schemer.service.spec.ts index fb683d3a..ec5538c7 100644 --- a/projects/ngx-coding-components/src/lib/services/schemer.service.spec.ts +++ b/projects/ngx-coding-components/src/lib/services/schemer.service.spec.ts @@ -22,6 +22,22 @@ describe('SchemerService', () => { }); describe('addCode', () => { + it('stores ANY for a new multiple-value code without changing existing codes', () => { + service.setUserRole('RW_MAXIMAL'); + const existing: CodeData = { id: 1, ruleSets: [{ rules: [{ method: 'IS_EMPTY' }] }] }; + const before = JSON.stringify(existing); + const created = service.addCode([existing], 'NO_CREDIT', true) as CodeData; + expect(created.ruleSets?.[0].valueArrayPos).toBe('ANY'); + expect(JSON.stringify(existing)).toBe(before); + }); + + it('keeps scalar codes free of array references', () => { + service.setUserRole('RW_MAXIMAL'); + const created = service.addCode([], 'FULL_CREDIT', false) as CodeData; + expect(created.ruleSets?.[0].valueArrayPos).toBeUndefined(); + expect(Object.hasOwn(created.ruleSets?.[0] || {}, 'valueArrayPos')).toBeFalse(); + }); + it('should block adding a residual type if a residual or intended incomplete already exists', () => { service.setUserRole('RW_MAXIMAL'); const codeList: CodeData[] = [ diff --git a/projects/ngx-coding-components/src/lib/services/schemer.service.ts b/projects/ngx-coding-components/src/lib/services/schemer.service.ts index 53027c9c..95bf1e9f 100644 --- a/projects/ngx-coding-components/src/lib/services/schemer.service.ts +++ b/projects/ngx-coding-components/src/lib/services/schemer.service.ts @@ -292,8 +292,8 @@ export class SchemerService { } } - addCode(codeList: CodeData[], codeType: CodeType): CodeData | string { - return addCodeOp(codeList, codeType, this.userRole, this.orderOfCodeTypes); + addCode(codeList: CodeData[], codeType: CodeType, multiple = false): CodeData | string { + return addCodeOp(codeList, codeType, this.userRole, this.orderOfCodeTypes, multiple); } deleteCode(codeList: CodeData[], codeIndex: number): boolean { diff --git a/projects/ngx-coding-components/src/lib/var-coding/code-rules/code-rules.component.html b/projects/ngx-coding-components/src/lib/var-coding/code-rules/code-rules.component.html index 0c0fd4d8..6de62038 100644 --- a/projects/ngx-coding-components/src/lib/var-coding/code-rules/code-rules.component.html +++ b/projects/ngx-coding-components/src/lib/var-coding/code-rules/code-rules.component.html @@ -37,7 +37,8 @@

{{ ruleSetIndex + 1 }}

(click)="editArrayReference(ruleSet)" [matTooltip]="'rule-set.reference.title' | translate" > - A {{ ruleSet.valueArrayPos | ruleReference }} + A {{ ruleSet.valueArrayPos === undefined ? ('rule-set.reference.legacy-short' | translate) : + (ruleSet.valueArrayPos | ruleReference) }} } diff --git a/projects/ngx-coding-components/src/lib/var-coding/code-rules/code-rules.component.spec.ts b/projects/ngx-coding-components/src/lib/var-coding/code-rules/code-rules.component.spec.ts index 57b4f03e..eac0eda4 100644 --- a/projects/ngx-coding-components/src/lib/var-coding/code-rules/code-rules.component.spec.ts +++ b/projects/ngx-coding-components/src/lib/var-coding/code-rules/code-rules.component.spec.ts @@ -4,7 +4,10 @@ import { TranslateFakeLoader, TranslateLoader, TranslateModule } from '@ngx-tran import { MatDialog } from '@angular/material/dialog'; import { of } from 'rxjs'; -import { CodeData } from '@iqbspecs/coding-scheme/coding-scheme.interface'; +import { CodeData, RuleSet } from '@iqbspecs/coding-scheme/coding-scheme.interface'; +import { CodingFactory } from '@iqb/responses'; +import { SelectCodeRuleReferenceDialogComponent } + from '../../dialogs/select-code-rule-reference-dialog.component'; import { CodeRulesComponent } from './code-rules.component'; @@ -77,6 +80,45 @@ describe('CodeRulesComponent', () => { expect(emitSpy).not.toHaveBeenCalled(); }); + it('new multiple-value rule sets require every marking to satisfy a single rule', () => { + component.code = { id: 41, ruleSets: [] }; + component.varInfo = { id: 'v1', multiple: true } as typeof component.varInfo; + component.addRuleSet(); + const ruleSet = component.code.ruleSets![0]; + expect(ruleSet.valueArrayPos).toBe('ANY'); + ruleSet.rules = [{ method: 'NUMERIC_LESS_THAN', parameters: ['74'], fragment: 0 }]; + const coding = CodingFactory.createCodingVariable('v1'); + coding.fragmenting = '^(\\d+)-(\\d+)-#'; + coding.codes = [component.code]; + const response = { id: 'v1', status: 'VALUE_CHANGED' as const, value: ['69-70-#f9f871', '78-79-#f9f871'] }; + expect(CodingFactory.code(response, coding).status).toBe('CODING_INCOMPLETE'); + expect(CodingFactory.code({ ...response, value: ['69-70-#f9f871'] }, coding).code).toBe(41); + }); + + it('opening and saving a legacy array reference preserves serialized coding and its result', () => { + const coding = CodingFactory.createCodingVariable('v1'); + const ruleSet: RuleSet = { + ruleOperatorAnd: true, + rules: [{ method: 'MATCH', parameters: ['A'] }, { method: 'MATCH', parameters: ['B'] }] + }; + coding.codes = [{ id: 41, ruleSets: [ruleSet] }]; + const before = JSON.stringify(coding); + const response = { id: 'v1', status: 'VALUE_CHANGED' as const, value: ['A', 'B'] }; + const previousResult = CodingFactory.code(response, coding); + dialogOpenSpy.and.callFake((_component, config) => { + let result: unknown; + const dialog = new SelectCodeRuleReferenceDialogComponent(config.data, { + close: (value: unknown) => { result = value; } + } as never); + dialog.okButtonClick(); + return { afterClosed: () => of(result) }; + }); + component.editArrayReference(ruleSet); + expect(JSON.stringify(coding)).toBe(before); + expect(CodingFactory.code(response, JSON.parse(JSON.stringify(coding)))).toEqual(previousResult); + expect(previousResult.code).toBe(41); + }); + it('deleteRuleSet should remove a ruleset and emit codeRulesChanged', () => { const emitSpy = spyOn(component.codeRulesChanged, 'emit'); diff --git a/projects/ngx-coding-components/src/lib/var-coding/code-rules/code-rules.component.ts b/projects/ngx-coding-components/src/lib/var-coding/code-rules/code-rules.component.ts index 9c402c74..e1d6af0b 100644 --- a/projects/ngx-coding-components/src/lib/var-coding/code-rules/code-rules.component.ts +++ b/projects/ngx-coding-components/src/lib/var-coding/code-rules/code-rules.component.ts @@ -31,6 +31,7 @@ import { } from '../../dialogs/select-code-rule-reference-dialog.component'; import { RuleReferencePipe } from '../../pipes/rule-reference.pipe'; import { SchemerService, UserRoleType } from '../../services/schemer.service'; +import { createDefaultRuleSet } from '../../services/schemer-code-ops'; import { CodeRuleListComponent } from './code-rule-list.component'; @Component({ @@ -89,15 +90,7 @@ export class CodeRulesComponent implements OnChanges, OnDestroy { return; } - const newRuleSet: RuleSet = { - ruleOperatorAnd: false, - rules: [ - { - method: 'MATCH', - parameters: [''] - } - ] - }; + const newRuleSet = createDefaultRuleSet(this.varInfo?.multiple); this.code.ruleSets = [...(this.code.ruleSets || []), newRuleSet]; diff --git a/projects/ngx-coding-components/src/lib/var-coding/var-coding.component.spec.ts b/projects/ngx-coding-components/src/lib/var-coding/var-coding.component.spec.ts index e4bf2e3e..829797d5 100644 --- a/projects/ngx-coding-components/src/lib/var-coding/var-coding.component.spec.ts +++ b/projects/ngx-coding-components/src/lib/var-coding/var-coding.component.spec.ts @@ -328,6 +328,14 @@ describe('VarCodingComponent', () => { expect(dialogOpenSpy).toHaveBeenCalled(); }); + it('passes multiple-value metadata when adding a code', () => { + component.varCoding = { id: 'v1', codes: [] } as unknown as VariableCodingData; + component.varInfo = { id: 'v1', multiple: true } as typeof component.varInfo; + addCodeSpy.and.returnValue('code.error-message.no-access'); + component.addCode('NO_CREDIT'); + expect(addCodeSpy).toHaveBeenCalledWith(component.varCoding.codes, 'NO_CREDIT', true); + }); + it('addCode should emit varCodingChanged on success and update residual flags', () => { const emitSpy = spyOn(component.varCodingChanged, 'emit'); component.varCoding = { diff --git a/projects/ngx-coding-components/src/lib/var-coding/var-coding.component.ts b/projects/ngx-coding-components/src/lib/var-coding/var-coding.component.ts index c5d2b543..283eb8a6 100644 --- a/projects/ngx-coding-components/src/lib/var-coding/var-coding.component.ts +++ b/projects/ngx-coding-components/src/lib/var-coding/var-coding.component.ts @@ -369,7 +369,8 @@ export class VarCodingComponent implements OnInit, OnDestroy, OnChanges { if (this.varCoding && this.varCoding.codes) { const addResult = this.schemerService.addCode( this.varCoding.codes, - codeType + codeType, + this.varInfo?.multiple || false ); if (typeof addResult === 'string') { this.messageDialog.open(MessageDialogComponent, { diff --git a/projects/ngx-coding-components/translations/de.json b/projects/ngx-coding-components/translations/de.json index 34e52a43..d4abb138 100644 --- a/projects/ngx-coding-components/translations/de.json +++ b/projects/ngx-coding-components/translations/de.json @@ -429,6 +429,9 @@ "label": "Position im Werte-Array", "title": "Position im Werte-Array ändern", "prompt": "Die Regeln beziehen sich auf alle vorhandenen Werte, mindestens einen Wert, eine bestimmte Position oder auf Summe bzw. Länge des Werte-Arrays.", + "legacy": "Bisherige Auswertung beibehalten", + "legacy-short": "Bisher", + "legacy-help": "Für jede Regel genügt ein passender Wert. Bei UND dürfen die Regeln auf unterschiedliche Werte zutreffen. Diese bestehende Einstellung bleibt erhalten, bis Sie eine andere Auswahl speichern.", "any": "Alle vorhandenen Werte müssen passen", "any-open": "Mindestens ein Wert muss passen", "any-help": "Zusätzliche nicht passende Werte verhindern einen Treffer. Beispiel: [passend, nicht passend] → kein Treffer.", diff --git a/src/assets/de.json b/src/assets/de.json index 54dcc9db..1a2b276c 100644 --- a/src/assets/de.json +++ b/src/assets/de.json @@ -201,6 +201,9 @@ "label": "Position im Werte-Array", "title": "Position im Werte-Array ändern", "prompt": "Die Regeln beziehen sich auf alle vorhandenen Werte, mindestens einen Wert, eine bestimmte Position oder auf Summe bzw. Länge des Werte-Arrays.", + "legacy": "Bisherige Auswertung beibehalten", + "legacy-short": "Bisher", + "legacy-help": "Für jede Regel genügt ein passender Wert. Bei UND dürfen die Regeln auf unterschiedliche Werte zutreffen. Diese bestehende Einstellung bleibt erhalten, bis Sie eine andere Auswahl speichern.", "any": "Alle vorhandenen Werte müssen passen", "any-open": "Mindestens ein Wert muss passen", "any-help": "Zusätzliche nicht passende Werte verhindern einen Treffer. Beispiel: [passend, nicht passend] → kein Treffer.", diff --git a/src/assets/en.json b/src/assets/en.json index bc04a881..beec49cf 100644 --- a/src/assets/en.json +++ b/src/assets/en.json @@ -4,6 +4,9 @@ "label": "Position in the value array", "title": "Change position in the value array", "prompt": "The rules apply to all existing values, at least one value, a specific position, or the sum or length of the value array.", + "legacy": "Keep existing evaluation", + "legacy-short": "Existing", + "legacy-help": "Each rule needs a matching value. With AND, different values may satisfy different rules. This existing setting is preserved until you save another selection.", "any": "All existing values must match", "any-open": "At least one value must match", "any-help": "Additional non-matching values prevent a match. Example: [matching, non-matching] → no match.",