Skip to content

Commit d4e4dfb

Browse files
fix(formula): label() over a multi-select reads each selected option's label, joined (#34443)
Fixes #34339 Clause-②: no Metadata-protocol: semantic Skills-docs: follow-up #34444 (`skills/objectstack-formula/SKILL.md`, the `label(record.f)` row; the multi-select join is taught in content/docs/data-modeling/formulas.mdx in this PR) ## 维护者速读 - **改了什么**:公式 `label(record.X)` 作用在多选字段(`multiple: true` 的 select)上,不再出错读成 null,而是按读者语言输出每个已选项的标签,按记录里存储的顺序,用 `, ` 连接;空选择仍读 `''`,单选不变。 - **为什么改**:下游 CRM 报价行用多选字段 + `label()` 拼一句「无法估算毛利的原因」,在 18.5.0 上每条记录都读成 null,而 `os validate` / `os lint --strict` 都放行。 - **风险与回滚**:风险低。只有原先读成 null 的公式变成有文字;单选、空值、未知值的读法不变。和邮件模板 `option` 占位符走同一个拼接函数,两边不会再分叉。回滚即撤销本 PR。 - **先看**:`packages/formula/src/stdlib.ts` 的 `labelEachOption` 与 `optionLabelOf` / `oneOptionLabel`;`packages/formula/src/template-engine.ts` 的 `option` 格式化器那一行;`packages/objectql/src/engine-formula-option-label.test.ts` 末尾新增的 describe。 - **两分钟验证**:`pnpm --filter @objectstack/formula exec vitest run src/option-label.test.ts`,预期 18 个用例全绿(在 main 上其中 7 个红)。 - **需要你做的**:无,席位落地。 ## What changes `label(record.FIELD)` over a `multiple: true` select used to throw (`renders ONE option value, and FIELD holds a list of values`), so the formula field read null on every non-empty selection. It now reads: | Value | Reads | |---|---| | `['no_sales_price', 'no_customer']` | `无销售价, 无客户` (zh-CN) / `No sales price, No customer` (en-US) | | `['no_freight_region']` | that one label | | `[]`, `null` | `''` (unchanged; measured below) | | a listed value no option declares | as stored, as the single-value path reads an unknown value | | a list holding an object | runtime fault, as an object value faults today | - **One rule, one helper.** `labelEachOption` (`packages/formula/src/stdlib.ts`, package-internal) maps each listed value through the caller's single-value lookup and joins with `, `. The mail template's `option` hole (`template-engine.ts`, `OPTION_ARGUMENT_RULE`) now calls the same helper instead of its own `map(...).join(', ')`, so the two cannot drift. A unit test asserts `label()` and `formatValue('option', …)` print the same string for the same list. - **Order: stored order.** Measured: `['no_freight_region', 'no_sales_price']` reads `No freight region, No sales price`, although the options declare `no_sales_price` first. That is the order the template engine already used. This is the claim's choice (claim 6923097287, Shape: "by the same rule the mail-template option hole already uses"), and it differs from the card's words: ask 1 says "joined with `, ` in option order" while naming the mail-template `option` hole (`OPTION_ARGUMENT_RULE`) as precedent, and that hole measures STORED order. So a reader of the downstream CRM quote line gets the labels in the order the values were stored, not the order the options are declared. - **No `labels()` sibling, and no author-time refusal** (the card's ask 2): the card asks for it only "Until (1) ships", and ask 1 ships here; claim 6923097287 (Shape) records that choice. Ruling 6919059607 is the claim permission (batch 1), not a ruling on order or on ask 2. - **The objectql seam needed no change.** `formula-option-labels.ts` builds the label map from every served field that declares `options`, whatever `multiple` says, with the translation bundle laid over it. The real-engine pin below reads translated multi-select labels through it unchanged. - **Docs:** `content/docs/data-modeling/formulas.mdx` said "A multi-value select is refused." It now teaches the join, the order and the empty reading. ## Tests - `packages/formula/src/option-label.test.ts`: a new describe for the multi-select (zh-CN and en-US join, one element, stored order, empty list and null, unknown value, composition, a field without options, an object inside the list, and agreement with the mail `option` hole). The old "refuses a list value" assertion is removed. An object value is still refused, in its own case. - `packages/objectql/src/engine-formula-option-label.test.ts`: a new top-level describe on the real `ObjectQL` engine (stub driver, i18n service with a zh-CN bundle) reads `label(record.gaps)` over a multi-select in zh-CN and en-US, beside a single-select CONTROL on the same record, plus the one-element and empty cases. - `packages/qa/dogfood/test/formula-option-label-locale.dogfood.test.ts`: the fixture gains a multi-select and its formula. One new case reads it through REST (`POST` / `GET /data/:object/:id` with `Accept-Language`) in zh-CN and en-US, with the existing single-select name as the CONTROL, plus the one-element, empty-list and unset cases. ## Acceptance notes - `main` is red on `check:platform-checklist` (`approvals.approver-resolution-matrix` VARIANTS STALE). That red is main's, not this diff's; the seat has a stopgap PR. `Lint & Repo Gates` will show it on this PR. - A CEL read of a key the record does not carry (`record.gaps` on `{}`) faults with `No such key` before `label()` runs. This is the same for a single select and is not changed here. The engine hands formulas every declared field, so a stored record does not hit it. ## Verification All readings below come from runs under `scripts/pm/os-verify-lock.sh`, on a shared box. - Final head `4a1ed6a82f`: the dogfood file alone, after `turbo run build --filter='@objectstack/dogfood^...'` (70 tasks, exit 0): `pnpm --filter @objectstack/dogfood exec vitest run --maxWorkers=1 test/formula-option-label-locale.dogfood.test.ts` gives `Test Files 1 passed (1)`, `Tests 6 passed (6)` (5 existing + the new multi-select case). Lock VERDICT `command-exit 0`. Its red on main was not run; the objectql pin carries the red-on-main reading. <details><summary>Red on main (tests at c0547aa, formula source at the merge base)</summary> - `pnpm --filter @objectstack/formula exec vitest run --maxWorkers=2 src/option-label.test.ts`: `Tests 7 failed | 11 passed (18)`. Each of the seven multi-select cases answered `{ ok: false, … }`. - `pnpm --filter @objectstack/objectql exec vitest run --maxWorkers=2 src/engine-formula-option-label.test.ts`: `Tests 2 failed | 10 passed (12)`, `AssertionError: expected null to be '无销售价, 无客户'`. The engine logged the card's own line: `formula field 'gaps_note' on 'ol_quote_line' could not be evaluated, so it reads null … label(record.gaps) renders ONE option value, and gaps holds a list of values on this record.` - The empty-selection case was already green on main at the engine level: the engine reads `''` for `[]` and for `null` there, and still does. </details> <details><summary>Green, and the ablation (one locked run at 6fa079a; formula src unchanged since)</summary> - `pnpm --filter @objectstack/formula build`: exit 0. `node scripts/ablation-dist-preflight.mjs @objectstack/formula 'oneOptionLabel(field, item, options, true)'`: marker present in `dist/index.js` and `dist/index.mjs`. - objectql file: `Tests 12 passed (12)`. - `pnpm --filter @objectstack/formula typecheck`: exit 0. - Ablation: `node scripts/ablation-replace.mjs --file packages/formula/src/stdlib.ts --anchor ' if (Array.isArray(value)) return labelEachOption(…);' --delete -- …`. Anchor count went from 1 to 0, blob `af2083df5155` to `a999c7ce79c1`. Formula rebuilt, `--absent` preflight: marker absent from all 6 built files. Then objectql file `Tests 2 failed | 10 passed (12)` and formula file `Tests 7 failed | 11 passed (18)`. Restore: `blob == HEAD (af2083d) and git diff HEAD is empty`. Rebuilt, marker present again, objectql file `Tests 12 passed (12)`. - At the final head 4a1ed6a, which only drops one assertion from `option-label.test.ts`: `pnpm --filter @objectstack/formula test` gives `Test Files 67 passed (67)`, `Tests 1770 passed (1770)`. </details> <details><summary>Derived gates</summary> At the final head `4a1ed6a82f`: `node scripts/pm/dispatch-gates.mjs --commands` derived 94 commands, and all 94 were run locally, each exit code captured before any pipe: 94 exit 0. `node scripts/pm/dispatch-gates.mjs --ran` gives: `94 derived famil(ies) accounted for — 94 run, 0 NOT-MEASURED (a DERIVED zero — all 94 recorded an exit code and none of them is 3)`. CI's `Lint & Repo Gates` runs the full farm. </details> - Dogfood battery: not owed (no `!` in the title, no `(narrowing)` arm). --- _Generated by [Claude Code](https://claude.ai/code/session_01FEkQ5kgF3mo9f7LXuv5hac)_ <!-- signed: repo:objectstack#1 · os-project-manager · session_01FEkQ5kgF3mo9f7LXuv5hac --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 6aeb665 commit d4e4dfb

7 files changed

Lines changed: 261 additions & 17 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
'@objectstack/formula': minor
3+
---
4+
5+
fix(formula): `label()` over a multi-select reads each selected option's label, joined with `, `
6+
7+
Clause-②: no
8+
Metadata-protocol: semantic
9+
10+
A formula `label(record.<field>)` over a `multiple: true` select used to fault on every non-empty selection, so the formula field read null in every view (the engine logged "renders ONE option value … holds a list of values"), while `os validate` and `os lint --strict` passed it.
11+
12+
It now reads the label of each selected value in the reader's language, in the order the record stores the values, joined with `, `: a record holding `['no_sales_price', 'no_customer']` reads `无销售价, 无客户` to a `zh-CN` reader and `No sales price, No customer` to an `en-US` one. A one-value selection reads that one label. An empty selection or an empty field reads `''`, as before. A listed value no option declares reads as stored, as a single value does.
13+
14+
This is the rule a mail template's `option` hole already prints a multi-select by; both now go through one shared helper, so the two cannot drift. A single select reads exactly as before. Nothing to migrate: a formula that read null now reads text.

‎content/docs/data-modeling/formulas.mdx‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,12 @@ page title, related lists and lookups all follow it.
304304
(`os validate`, `os build`, the object save door), with the field named,
305305
because it has no label to read and the formula would read null on every row.
306306
An empty value reads `''`; a stored value no option declares reads as itself.
307-
A multi-value select is refused.
307+
- On a multi-value select (`multiple: true`), `label()` reads the label of each
308+
selected value, in the order the record stores them, joined with `, `: a
309+
record holding `['no_sales_price', 'no_customer']` reads `无销售价, 无客户` to a
310+
`zh-CN` reader and `No sales price, No customer` to an `en-US` one. An empty
311+
selection reads `''`. A mail template's `option` hole prints the same list
312+
the same way.
308313
- A read with no locale (a script, a scheduled job) reads the declared labels.
309314
- A **stored** computed field (a `text` field with an `expression`) is computed
310315
once, on the write, so it stores the declared label whatever the writer's

‎packages/formula/src/option-label.test.ts‎

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, expect, it } from 'vitest';
22

33
import { celEngine } from './cel-engine';
4+
import { formatValue } from './template-engine';
45
import { inferExpressionType } from './validate';
56
import type { EvalOptionLabels } from './types';
67
import type { Expression } from '@objectstack/spec';
@@ -58,7 +59,7 @@ describe('label(record.<field>) — option labels in the reader\'s locale (#2660
5859
}
5960
});
6061

61-
it('refuses a field that declares no options, on every row, and a list value', () => {
62+
it('refuses a field that declares no options, on every row', () => {
6263
const plain = celEngine.evaluate(cel('label(record.title)'), { record, optionLabels: zhCN });
6364
expect(plain.ok).toBe(false);
6465
if (!plain.ok) {
@@ -69,12 +70,12 @@ describe('label(record.<field>) — option labels in the reader\'s locale (#2660
6970
const empty = celEngine.evaluate(cel('label(record.title)'), { record: { ...record, title: null }, optionLabels: zhCN });
7071
expect(empty.ok).toBe(false);
7172
if (!empty.ok) expect(empty.error.message).toContain('`title` declares no options');
72-
const list = celEngine.evaluate(cel('label(record.tags)'), {
73-
record,
74-
optionLabels: { ...zhCN, tags: { a: 'A' } },
75-
});
76-
expect(list.ok).toBe(false);
77-
if (!list.ok) expect(list.error.message).toContain('renders ONE option value');
73+
});
74+
75+
it('refuses an object value at run time — it is no option value', () => {
76+
const r = celEngine.evaluate(cel('label(record.kind)'), { record: { kind: { v: 'clean' } }, optionLabels: zhCN });
77+
expect(r.ok).toBe(false);
78+
if (!r.ok) expect(r.error.kind).toBe('runtime');
7879
});
7980

8081
it('compiles the one argument shape and refuses every other at the type check', () => {
@@ -100,3 +101,76 @@ describe('label(record.<field>) — option labels in the reader\'s locale (#2660
100101
expect(inferExpressionType('label(record.kind)')).toBe('text');
101102
});
102103
});
104+
105+
/**
106+
* `label(record.<field>)` over a `multiple: true` select — a list of stored
107+
* values — prints the label of each value, in the order the record stores
108+
* them, joined with `, `: the rule the mail template's `option` hole applies
109+
* to the same list (`OPTION_ARGUMENT_RULE`), through one shared helper.
110+
*/
111+
describe('label(record.<field>) over a multi-select — each selected label, joined', () => {
112+
const GAPS = 'label(record.gaps)';
113+
const zh: EvalOptionLabels = {
114+
gaps: { no_sales_price: '无销售价', no_customer: '无客户', no_freight_region: '无运费区域' },
115+
one: { no_customer: '无客户' },
116+
};
117+
const en: EvalOptionLabels = {
118+
gaps: { no_sales_price: 'No sales price', no_customer: 'No customer', no_freight_region: 'No freight region' },
119+
one: { no_customer: 'No customer' },
120+
};
121+
const evalIn = (labels: EvalOptionLabels, rec: Record<string, unknown>, source = GAPS) =>
122+
celEngine.evaluate(cel(source), { record: rec, optionLabels: labels });
123+
124+
it('reads each selected value\'s label in the reader\'s language, joined with `, `', () => {
125+
const rec = { gaps: ['no_sales_price', 'no_customer'] };
126+
expect(evalIn(zh, rec)).toEqual({ ok: true, value: '无销售价, 无客户' });
127+
expect(evalIn(en, rec)).toEqual({ ok: true, value: 'No sales price, No customer' });
128+
});
129+
130+
it('reads a one-element list as that one label', () => {
131+
expect(evalIn(zh, { gaps: ['no_freight_region'] })).toEqual({ ok: true, value: '无运费区域' });
132+
expect(evalIn(en, { gaps: ['no_freight_region'] })).toEqual({ ok: true, value: 'No freight region' });
133+
});
134+
135+
it('keeps the order the record stores, not the order the options are declared in', () => {
136+
expect(evalIn(en, { gaps: ['no_freight_region', 'no_sales_price'] }))
137+
.toEqual({ ok: true, value: 'No freight region, No sales price' });
138+
});
139+
140+
it('reads an empty list and an empty field as \'\', as a single select reads empty', () => {
141+
expect(evalIn(zh, { gaps: [] })).toEqual({ ok: true, value: '' });
142+
expect(evalIn(zh, { gaps: null })).toEqual({ ok: true, value: '' });
143+
});
144+
145+
it('reads a listed value no option declares as itself, as the single-value path does', () => {
146+
expect(evalIn(en, { one: 'retired' }, 'label(record.one)')).toEqual({ ok: true, value: 'retired' });
147+
expect(evalIn(en, { gaps: ['no_customer', 'retired'] })).toEqual({ ok: true, value: 'No customer, retired' });
148+
});
149+
150+
it('composes into a sentence like any string', () => {
151+
const r = evalIn(zh, { gaps: ['no_customer'] }, "'缺: ' + label(record.gaps)");
152+
expect(r).toEqual({ ok: true, value: '缺: 无客户' });
153+
});
154+
155+
it('a field that declares no options still faults on a list, as on every other value', () => {
156+
const r = celEngine.evaluate(cel('label(record.tags)'), { record: { tags: ['a'] }, optionLabels: zh });
157+
expect(r.ok).toBe(false);
158+
if (!r.ok) expect(r.error.kind).toBe('runtime');
159+
});
160+
161+
it('a list holding an object faults — an object is no option value', () => {
162+
const r = evalIn(zh, { gaps: ['no_customer', { v: 'x' }] });
163+
expect(r.ok).toBe(false);
164+
if (!r.ok) expect(r.error.kind).toBe('runtime');
165+
});
166+
167+
it('⭐ prints what the mail template\'s `option` hole prints for the same list — one rule', () => {
168+
const list = ['no_freight_region', 'retired', 'no_customer'];
169+
const viaLabel = evalIn(en, { gaps: list });
170+
const viaHole = formatValue('option', list, 'quote_line.gaps', {
171+
optionLabels: { 'quote_line.gaps': en.gaps },
172+
});
173+
expect(viaLabel).toEqual({ ok: true, value: viaHole });
174+
expect(viaHole).toBe('No freight region, retired, No customer');
175+
});
176+
});

‎packages/formula/src/stdlib.ts‎

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -490,14 +490,32 @@ export function optionlessLabelSentence(field: string): string {
490490
);
491491
}
492492

493+
/**
494+
* A list of stored option values — a multi-select's value — as the label of
495+
* each, in the order the list holds them, joined with `, `.
496+
*
497+
* The ONE spelling of that rule: `label(record.<field>)` ({@link optionLabelOf})
498+
* and the mail template's `option` hole (`template-engine.ts`,
499+
* `OPTION_ARGUMENT_RULE`) both print a multi-select through it, each with its
500+
* own single-value lookup as `labelOf`, so the two agree on the order and the
501+
* separator by construction.
502+
*
503+
* Package-internal (NOT re-exported from `index.ts`).
504+
*/
505+
export function labelEachOption(values: readonly unknown[], labelOf: (value: unknown) => string): string {
506+
return values.map((value) => labelOf(value)).join(', ');
507+
}
508+
493509
/**
494510
* The label a reader reads for `value` of `field`, from the pinned map.
495511
*
496512
* `null` / absent is `''`, so a name composes cleanly over an empty select
497513
* (`trim()` answers `''` for the same reason). A value no option declares reads
498-
* as itself — the select control shows such a value the same way. Everything
499-
* else that cannot be answered THROWS, which the engine reports as
500-
* `{ ok: false, kind: 'runtime' }`.
514+
* as itself — the select control shows such a value the same way. A list — a
515+
* `multiple: true` select's value — reads the label of each of its values, in
516+
* stored order, joined with `, ` ({@link labelEachOption}); an empty list is
517+
* `''`. Everything else that cannot be answered THROWS, which the engine
518+
* reports as `{ ok: false, kind: 'runtime' }`.
501519
*/
502520
function optionLabelOf(field: string, value: unknown, labels: EvalOptionLabels | undefined): string {
503521
if (labels === undefined) {
@@ -513,11 +531,23 @@ function optionLabelOf(field: string, value: unknown, labels: EvalOptionLabels |
513531
// row, not only on the rows where it happens to hold a value.
514532
const options = Object.prototype.hasOwnProperty.call(labels, field) ? labels[field] : undefined;
515533
if (options === undefined) throw new Error(optionlessLabelSentence(field));
534+
if (Array.isArray(value)) return labelEachOption(value, (item) => oneOptionLabel(field, item, options, true));
535+
return oneOptionLabel(field, value, options, false);
536+
}
537+
538+
/** One stored option value of `field` as its label from `options`, or as stored when no option declares it. */
539+
function oneOptionLabel(
540+
field: string,
541+
value: unknown,
542+
options: Readonly<Record<string, string>>,
543+
listed: boolean,
544+
): string {
516545
if (value === null || value === undefined) return '';
517546
if (typeof value === 'object') {
518547
throw new Error(
519-
`label(record.${field}) renders ONE option value, and \`${field}\` holds ` +
520-
`${Array.isArray(value) ? 'a list of values' : 'an object'} on this record.`,
548+
`label(record.${field}) reads a select field's option values, and \`${field}\` holds ` +
549+
`${listed ? 'a list with a non-option entry' : 'an object'} on this record: an option value is ` +
550+
'text or a number.',
521551
);
522552
}
523553
const stored = String(value);

‎packages/formula/src/template-engine.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import type { Expression } from '@objectstack/spec';
2020

21-
import { buildScope } from './stdlib';
21+
import { buildScope, labelEachOption } from './stdlib';
2222
import type { DialectEngine, EvalContext, EvalResult } from './types';
2323

2424
/**
@@ -165,13 +165,14 @@ const FORMATTERS: Record<string, Formatter> = {
165165
// field it throws, whatever the value (`optionTableRefusal`): the hole
166166
// promises a label, so the stored value is never printed in its place, as
167167
// `currency` never picks a currency. A multi-select value labels each
168-
// element, joined with `, `.
168+
// element, in stored order, joined with `, ` — `labelEachOption`, the rule
169+
// `label(record.<field>)` prints a multi-select by too.
169170
option: (v, arg, _locale, _timeZone, _currency, optionLabels) => {
170171
const objectField = arg ?? '';
171172
const refusal = optionTableRefusal(`option:${objectField}`, objectField, optionLabels);
172173
if (refusal) throw new RangeError(refusal);
173174
const labels = optionLabels![objectField];
174-
if (Array.isArray(v)) return v.map((item) => optionLabelOf(item, labels)).join(', ');
175+
if (Array.isArray(v)) return labelEachOption(v, (item) => optionLabelOf(item, labels));
175176
return optionLabelOf(v, labels);
176177
},
177178
};

‎packages/objectql/src/engine-formula-option-label.test.ts‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,81 @@ describe('#33634 — the declared sourceLocale, not defaultLocale, is the author
215215
expect(a.display_name).toBe('v2 · 清稿');
216216
});
217217
});
218+
219+
describe('label() over a multi-select reads each selected label, joined, in the reader\'s locale', () => {
220+
// The card's quote-line shape: a multi-select of gap kinds named by a
221+
// formula, beside a CONTROL single select named the same way.
222+
const LINE = {
223+
name: 'ol_quote_line',
224+
label: 'Quote Line',
225+
fields: {
226+
id: { name: 'id', label: 'ID', type: 'text' as const, primaryKey: true },
227+
gaps: {
228+
name: 'gaps', label: 'Gaps', type: 'select' as const, multiple: true,
229+
options: [
230+
{ label: 'No sales price', value: 'no_sales_price' },
231+
{ label: 'No customer', value: 'no_customer' },
232+
{ label: 'No freight region', value: 'no_freight_region' },
233+
],
234+
},
235+
gaps_note: {
236+
name: 'gaps_note', label: 'Gaps Note', type: 'formula' as const,
237+
expression: { dialect: 'cel', source: 'label(record.gaps)' },
238+
},
239+
one: {
240+
name: 'one', label: 'One Gap', type: 'select' as const,
241+
options: [{ label: 'No sales price', value: 'no_sales_price' }, { label: 'No customer', value: 'no_customer' }],
242+
},
243+
one_note: {
244+
name: 'one_note', label: 'One Note', type: 'formula' as const,
245+
expression: { dialect: 'cel', source: 'label(record.one)' },
246+
},
247+
},
248+
};
249+
const GAP_LABELS = { no_sales_price: '无销售价', no_customer: '无客户', no_freight_region: '无运费区域' };
250+
251+
async function makeLineEngine() {
252+
const engine = new ObjectQL();
253+
const rig = makeStubDriver();
254+
engine.registerDriver(rig.driver as never, true);
255+
await engine.init();
256+
engine.registry.registerObject(LINE as never, 'test');
257+
engine.setI18nService(makeI18n({
258+
getTranslations: (locale: string) => (locale === 'zh-CN'
259+
? { objects: { ol_quote_line: { fields: { gaps: { options: GAP_LABELS }, one: { options: GAP_LABELS } } } } }
260+
: {}),
261+
}));
262+
return { engine, ...rig };
263+
}
264+
265+
const readIn = (engine: ObjectQL, id: unknown, context: unknown) =>
266+
engine.findOne('ol_quote_line', { where: { id }, context } as never) as Promise<Rec>;
267+
268+
it('a record read answers the joined labels in zh-CN and en-US, beside the single-select control', async () => {
269+
const { engine } = await makeLineEngine();
270+
const created = await engine.insert(
271+
'ol_quote_line', { gaps: ['no_sales_price', 'no_customer'], one: 'no_customer' }, { context: enUS },
272+
) as Rec;
273+
const zh = await readIn(engine, created.id, zhCN);
274+
expect(zh.gaps_note).toBe('无销售价, 无客户');
275+
expect(zh.one_note).toBe('无客户');
276+
const en = await readIn(engine, created.id, enUS);
277+
expect(en.gaps_note).toBe('No sales price, No customer');
278+
expect(en.one_note).toBe('No customer');
279+
});
280+
281+
it('a one-element selection reads that one label; the write response reads the writer\'s locale', async () => {
282+
const { engine } = await makeLineEngine();
283+
const created = await engine.insert('ol_quote_line', { gaps: ['no_freight_region'] }, { context: zhCN }) as Rec;
284+
expect(created.gaps_note).toBe('无运费区域');
285+
expect((await readIn(engine, created.id, enUS)).gaps_note).toBe('No freight region');
286+
});
287+
288+
it('an empty selection and an empty field read \'\'', async () => {
289+
const { engine } = await makeLineEngine();
290+
const empty = await engine.insert('ol_quote_line', { gaps: [] }, { context: zhCN }) as Rec;
291+
expect((await readIn(engine, empty.id, zhCN)).gaps_note).toBe('');
292+
const none = await engine.insert('ol_quote_line', { gaps: null }, { context: enUS }) as Rec;
293+
expect((await readIn(engine, none.id, enUS)).gaps_note).toBe('');
294+
});
295+
});

‎packages/qa/dogfood/test/formula-option-label-locale.dogfood.test.ts‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { defineStack } from '@objectstack/spec';
2323

2424
const OBJECT = 'fol_version';
2525
const NAME = "'v' + string(record.version_no) + ' · ' + label(record.kind)";
26+
const GAPS = 'label(record.gaps)';
2627

2728
const fixtureStack = defineStack({
2829
manifest: {
@@ -50,13 +51,35 @@ const fixtureStack = defineStack({
5051
],
5152
},
5253
display_name: { type: 'formula', label: 'Name', expression: { dialect: 'cel', source: NAME }, returnType: 'text' },
54+
// A multi-select read through `label()`: each selected label, joined
55+
// with `, `. `kind` + `display_name` above is its single-select CONTROL.
56+
gaps: {
57+
type: 'select',
58+
label: 'Gaps',
59+
multiple: true,
60+
options: [
61+
{ label: 'No sales price', value: 'no_sales_price' },
62+
{ label: 'No customer', value: 'no_customer' },
63+
{ label: 'No freight region', value: 'no_freight_region' },
64+
],
65+
},
66+
gaps_note: {
67+
type: 'formula', label: 'Gaps Note', expression: { dialect: 'cel', source: GAPS }, returnType: 'text',
68+
},
5369
},
5470
},
5571
],
5672
translations: [
5773
{
5874
'zh-CN': {
59-
objects: { [OBJECT]: { fields: { kind: { options: { clean: '清稿', redline: '修订稿' } } } } },
75+
objects: {
76+
[OBJECT]: {
77+
fields: {
78+
kind: { options: { clean: '清稿', redline: '修订稿' } },
79+
gaps: { options: { no_sales_price: '无销售价', no_customer: '无客户', no_freight_region: '无运费区域' } },
80+
},
81+
},
82+
},
6083
},
6184
},
6285
],
@@ -142,4 +165,23 @@ describe('dogfood: a formula name reads its select option label in the reader\'s
142165
expect((await readOne(locale, created.id)).display_name, locale).toBe(`v7 · ${shown}`);
143166
}
144167
});
168+
169+
it('label() over a multi-select reads each selected label, joined, in each reader\'s locale', async () => {
170+
const created = await create('en-US', { version_no: 8, kind: 'clean', gaps: ['no_sales_price', 'no_customer'] });
171+
const zh = await readOne('zh-CN', created.id);
172+
expect(zh.gaps_note).toBe('无销售价, 无客户');
173+
expect(zh.display_name).toBe('v8 · 清稿'); // CONTROL: the single select on the same record
174+
const en = await readOne('en-US', created.id);
175+
expect(en.gaps_note).toBe('No sales price, No customer');
176+
expect(en.display_name).toBe('v8 · Clean');
177+
178+
const one = await create('zh-CN', { version_no: 9, kind: 'clean', gaps: ['no_freight_region'] });
179+
expect(one.gaps_note).toBe('无运费区域');
180+
expect((await readOne('en-US', one.id)).gaps_note).toBe('No freight region');
181+
182+
const empty = await create('en-US', { version_no: 10, kind: 'clean', gaps: [] });
183+
expect((await readOne('zh-CN', empty.id)).gaps_note).toBe('');
184+
const none = await create('en-US', { version_no: 11, kind: 'clean' });
185+
expect((await readOne('en-US', none.id)).gaps_note).toBe('');
186+
});
145187
});

0 commit comments

Comments
 (0)