Skip to content

Commit 302a4c3

Browse files
fix(scenarios): the gallery export merges the card's seed rows into the blank scaffold's data list (#38672)
Fixes #38647 Clause-②: yes (widening) Metadata-protocol: none ## 维护者速读 - 用户能看到什么变化:用默认空白模板搭建、保留了模板自带示例文章的应用,现在可以导出到应用画廊,导出包里同时带模板的示例文章和场景卡的示例数据;此前这类导出一律被拒。 - 为什么做:属于“场景应用导出为可试用模板包”这条线,解决评测流水线从默认模板起步的应用进不了画廊的问题。 - 出错会怎样:只影响画廊导出这一内部评测工具,出错时导出被拒并写明原因,不会写出半成品;改动可直接撤回。 - 要你做什么:无。 ## What changes Route: **merge into the existing array.** The `blank` template keeps declaring `data: [articles]`; the template and the getting-started page are untouched. `withSampleData` (`packages/qa/scenarios/src/export/trial-package.ts`) used to refuse any `defineStack({ … })` literal that declared `data`. Now, when that `data:` is an array literal, it keeps the list and spreads the card's rows in after its last item: - `data: [articles]` becomes `data: [articles, ...gallerySampleData]` - `data: []` becomes `data: [...gallerySampleData]` - a multi-line list keeps its layout and trailing comma: `faqs,` becomes `faqs, ...gallerySampleData,` The import line is added as before. A literal with no `data:` still gets `data: gallerySampleData,` as its first property. The refusal stays for the shapes the exporter cannot read safely, each with its reason: - a spread in the literal (existing pin, unchanged) - a default export that is not `defineStack` with an object literal (two existing pins, unchanged) - a binding already named `gallerySampleData` (existing pin, unchanged) - a `data:` that is not an array literal, such as a shorthand `data` or a binding. The shorthand pin is kept and its message is now more precise. - `data` declared twice (new pin) What the pins now expect, and why: - The `data: []` case of the refusal table used to expect a refusal. An empty list is now a list the exporter can read, so that case moved to the new merge table, which expects `[...gallerySampleData]`. - The refusal table's first row is now `data: seeds`, a binding, which is the non-literal shape the old row stood for. - The export-level refusal pin now writes `data: seeds` and pins the new full refusal text. ## Tests - New merge table (3 cases): empty list, a list of seeds, and a multi-line list with a trailing comma. Each case is checked byte for byte against the expected edit. - New pin on a fresh `blank` scaffold. The test reads `packages/create-objectstack/src/templates/blank/objectstack.config.ts` from the tree and runs `trialPackageSource` on it. It checks that the result has exactly one `data`, equal to `[articles, ...gallerySampleData]`, that the imports gained the sample-data import, that `installModes` is `['unmanaged']`, and that `src/sample-data.json` holds the card's rows. - New export-level pin: the crm run's project gets the template config, `exportGallery` refuses nothing, and the exported config lists both sources. <details><summary>Package test + typecheck at dbd6328</summary> ``` pnpm --filter @objectstack/scenarios build && pnpm --filter @objectstack/scenarios test && pnpm --filter @objectstack/scenarios typecheck Test Files 45 passed (45) Tests 1430 passed (1430) tsc --noEmit (clean) os-verify-lock: VERDICT command-exit 0 ``` </details> <details><summary>Ablation (scripts/ablation-replace.mjs)</summary> The mutation changed `if (existing && !list) {` to `if (existing) {`, which brings back the old refusal of every `data:`. - Anchor hits: 1 before, 0 after. Blob `2454d1c63a07` became `9c86f7e97dae`. - `exporter.test.ts`: 5 failed, 69 passed. The 3 merge cases, the fresh-scaffold pin and the export-level scaffold pin went red. - After the restore the blob matched HEAD `2454d1c63a07` and `git diff HEAD` was empty. - The suite imports `src/` directly, so no dist leg was needed. </details> ## Acceptance notes - **Cross-package read, declared (claim amendment 7266905293, route A).** The pin reads the `blank` template's config from the tree. That path is now declared in two lines: one glob on the `@objectstack/scenarios` entry of `scripts/cross-package-test-inputs.mjs`, and one `$TURBO_ROOT$` input on `@objectstack/scenarios#test` in `turbo.json`. `check:cross-package-test-inputs` exits 0 at 356648c. - Template mode's gallery run (`templateSampleData`, `orchestrate.ts`) sends the card's rows **as** the manifest's `data[]`, replacing what the build declares. The exported package carries both the scaffold's `articles` and the card's rows. So the template-mode run does not install the scaffold's rows through the seed copy. The build's own boot does load them, since each boot inserts `data: [articles]`. They are a different object (the template's `blank_article`), so the two sources cannot collide on a row. Noted only, not filed. - The export README line "Sample data: the card's N seed rows in M objects" still counts the card's rows only. It is still true; it just does not mention the scaffold's rows. ## Verification Files to read first: 1. `packages/qa/scenarios/src/export/trial-package.ts`: the `withSampleData` hunk (the `list` / `last` insertion point). 2. `packages/qa/scenarios/test/export/exporter.test.ts`: the fresh `blank` scaffold pin. Two-minute check: `pnpm --filter @objectstack/scenarios exec vitest run --maxWorkers=2 test/export/exporter.test.ts` should end with `Tests 74 passed (74)`. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01ADHkmQJUGAAa5rz9Yi2DvW <!-- signed: repo:objectstack#3 · os-steve · session_01ADHkmQJUGAAa5rz9Yi2DvW --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 39064f6 commit 302a4c3

5 files changed

Lines changed: 106 additions & 12 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
'@objectstack/scenarios': patch
3+
---
4+
5+
fix(scenarios): the gallery export no longer refuses a build that kept the `blank` scaffold's `data:` list
6+
7+
Clause-②: yes (widening)
8+
Metadata-protocol: none
9+
10+
The `blank` template of `create-objectstack` declares its own sample rows in `objectstack.config.ts` (`data: [articles]`), and the gallery export refused any config that declared `data:`, so every build that kept the scaffold's config could not become a trial package. When the default-exported `defineStack({ … })` literal already declares `data:` as a list written in place, the export now keeps that list and spreads the card's seed rows in after its last item (`data: [articles, ...gallerySampleData]`): the package arrives with the scaffold's rows the build kept and the card's seed rows, and every other byte of the config is as the AI wrote it. A config with no `data:` gains the `data: gallerySampleData` property as before.
11+
12+
The export still refuses, with its reason, the shapes it cannot read safely: a spread in the literal, a default export that is not `defineStack` with an object literal, a `data:` that is not an array literal (a shorthand `data`, a binding or a call), a `data:` declared more than once, and a config that already uses the import's name.

‎packages/qa/scenarios/src/export/trial-package.ts‎

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
* unmanaged install's seed copy read.
2424
* - `objectstack.config.ts` reads that file into its `data:`
2525
* ({@link withSampleData}): one import line and one property, spliced
26-
* into the default-exported `defineStack({ … })` literal, every other byte
26+
* into the default-exported `defineStack({ … })` literal, or, when that
27+
* literal already lists its own `data: [ … ]` (the `blank` scaffold's
28+
* sample rows), one spread item appended to that list; every other byte
2729
* as the AI wrote it.
2830
*
2931
* A user-handle field (`run.userHandleFields`, `account.owner` = `u_sam`) is
@@ -177,10 +179,18 @@ export async function trialPackageSource(
177179
* import, and `data: BINDING,` as the first property of the object literal the
178180
* default export hands `defineStack`. Every other byte is kept.
179181
*
182+
* A literal that already declares `data:` as a list written in place keeps
183+
* it, and the card's rows are spread in after its last item: the `blank`
184+
* scaffold's `data: [articles]` becomes `data: [articles, ...BINDING]`, so the
185+
* package arrives with the scaffold's rows the build kept and the card's seed
186+
* rows, both loaded by the same install.
187+
*
180188
* Refused, each with its reason, when the edit cannot be proved safe: the
181189
* default export is not `defineStack({ … })` with an object literal; that
182-
* literal already declares `data` or spreads another value (which may carry
183-
* one); or the binding name is already used in the file.
190+
* literal spreads another value (which may carry a `data:`), declares `data`
191+
* more than once, or declares it as anything but an array literal (a
192+
* shorthand `data`, a binding, a call), whose items cannot be read as
193+
* written; or the binding name is already used in the file.
184194
*/
185195
export function withSampleData(ts: Ts, source: string): string {
186196
const file = parseConfig(ts, source);
@@ -190,9 +200,18 @@ export function withSampleData(ts: Ts, source: string): string {
190200
'objectstack.config.ts does not default-export `defineStack({ … })` with an object literal, so the sample data cannot be added to its `data:`',
191201
);
192202
}
203+
const declared: import('typescript').ObjectLiteralElementLike[] = [];
193204
for (const p of literal.properties) {
194205
if (ts.isSpreadAssignment(p)) throw new TrialPackageRefusal('objectstack.config.ts spreads a value into `defineStack({ … })`, which may declare `data:` already, so the sample data is not added');
195-
if (propertyName(ts, p) === 'data') throw new TrialPackageRefusal('objectstack.config.ts already declares `data:`, so the card\'s seed rows are not added as the sample data');
206+
if (propertyName(ts, p) === 'data') declared.push(p);
207+
}
208+
if (declared.length > 1) throw new TrialPackageRefusal('objectstack.config.ts declares `data:` more than once in `defineStack({ … })`, so the card\'s seed rows are not added as the sample data');
209+
const existing = declared[0];
210+
const list = existing && ts.isPropertyAssignment(existing) && ts.isArrayLiteralExpression(existing.initializer) ? existing.initializer : undefined;
211+
if (existing && !list) {
212+
throw new TrialPackageRefusal(
213+
'objectstack.config.ts already declares `data:` as something other than a list written in place (`data: [ … ]`), so its items cannot be read and the card\'s seed rows are not added as the sample data',
214+
);
196215
}
197216
let used = false;
198217
const visit = (n: import('typescript').Node): void => {
@@ -205,8 +224,13 @@ export function withSampleData(ts: Ts, source: string): string {
205224
const imports = file.statements.filter(ts.isImportDeclaration);
206225
const importLine = `import ${SAMPLE_DATA_BINDING} from '${SAMPLE_DATA_SPECIFIER}';`;
207226
const brace = literal.getStart(file) + 1;
227+
const last = list?.elements[list.elements.length - 1];
208228
const edits: Array<{ at: number; text: string }> = [
209-
{ at: brace, text: `\n // The card's seed rows, the sample data an install loads (added by the gallery export).\n data: ${SAMPLE_DATA_BINDING},` },
229+
!list
230+
? { at: brace, text: `\n // The card's seed rows, the sample data an install loads (added by the gallery export).\n data: ${SAMPLE_DATA_BINDING},` }
231+
: last
232+
? { at: last.getEnd(), text: `, ...${SAMPLE_DATA_BINDING}` }
233+
: { at: list.getStart(file) + 1, text: `...${SAMPLE_DATA_BINDING}` },
210234
imports.length > 0 ? { at: imports[imports.length - 1]!.getEnd(), text: `\n${importLine}` } : { at: 0, text: `${importLine}\n` },
211235
];
212236
let out = source;

‎packages/qa/scenarios/test/export/exporter.test.ts‎

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
import { createHash } from 'node:crypto';
1212
import { existsSync, mkdirSync, readdirSync, readFileSync, realpathSync, statSync, writeFileSync } from 'node:fs';
13-
import { join, relative } from 'node:path';
13+
import { dirname, join, relative, resolve } from 'node:path';
14+
import { fileURLToPath } from 'node:url';
1415

1516
import { describe, expect, it } from 'vitest';
1617

@@ -518,6 +519,14 @@ export default defineStack({
518519
});
519520
`;
520521

522+
/**
523+
* create-objectstack's `blank` template config, read from the tree as a fresh
524+
* scaffold starts from it (its own `data: [articles]` sample rows included),
525+
* so a change to the template's `data:` moves this suite's verdict.
526+
*/
527+
const HERE = dirname(fileURLToPath(import.meta.url));
528+
const BLANK_TEMPLATE_CONFIG = resolve(HERE, '../../../../create-objectstack/src/templates/blank/objectstack.config.ts');
529+
521530
/** The `defineStack({ … })` literal's property names, and the module's import specifiers, read back with the compiler. */
522531
function stackShape(text: string): { keys: string[]; imports: string[]; data?: string } {
523532
const file = ts.createSourceFile('objectstack.config.ts', text, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
@@ -600,8 +609,53 @@ describe('exportGallery — the trial package: listing text, facet and sample da
600609
});
601610

602611
it.each([
603-
['declares `data:` already', "import { defineStack } from '@objectstack/spec';\nexport default defineStack({ manifest: {}, data: [] });\n", /already declares `data:`/],
604-
['declares `data` as a shorthand property', "import { defineStack } from '@objectstack/spec';\nconst data = [];\nexport default defineStack({ manifest: {}, data });\n", /already declares `data:`/],
612+
['an empty list', 'data: []', `data: [...${SAMPLE_DATA_BINDING}]`],
613+
['a list of seeds', 'data: [articles]', `data: [articles, ...${SAMPLE_DATA_BINDING}]`],
614+
['a multi-line list with a trailing comma', 'data: [\n articles,\n faqs,\n ]', `data: [\n articles,\n faqs, ...${SAMPLE_DATA_BINDING},\n ]`],
615+
])("a config that declares `data:` as %s keeps it, with the card's rows spread in after its items", (_, declared, merged) => {
616+
const text = `import { defineStack } from '@objectstack/spec';\nimport { articles, faqs } from './src/data';\n\nexport default defineStack({\n manifest: { id: 'com.example.democrm' },\n ${declared},\n});\n`;
617+
const edited = withSampleData(ts, text);
618+
const after = stackShape(edited);
619+
expect(after.keys).toEqual(['manifest', 'data']);
620+
expect(after.imports).toEqual(['@objectstack/spec', './src/data', `./${SAMPLE_DATA_FILE}`]);
621+
expect(edited).toBe(text.replace(declared, merged).replace("from './src/data';", `from './src/data';\nimport ${SAMPLE_DATA_BINDING} from './${SAMPLE_DATA_FILE}';`));
622+
});
623+
624+
it("a fresh `blank` scaffold's config, read from the template: its `data: [articles]` is kept and the card's seed rows are spread in", async () => {
625+
const scaffold = readFileSync(BLANK_TEMPLATE_CONFIG, 'utf8');
626+
expect(stackShape(scaffold).data).toBe('[articles]');
627+
const card = loadCrm().card;
628+
const files = new Map([['objectstack.config.ts', Buffer.from(scaffold)]]);
629+
const ok = await trialPackageSource(files, card, new Set(['democrm_account', 'democrm_contact']));
630+
const config = ok.files.get('objectstack.config.ts')!.toString('utf8');
631+
const shape = stackShape(config);
632+
expect(shape.data).toBe(`[articles, ...${SAMPLE_DATA_BINDING}]`);
633+
expect(shape.keys.filter((k) => k === 'data')).toHaveLength(1);
634+
expect(shape.imports).toEqual([...stackShape(scaffold).imports, `./${SAMPLE_DATA_FILE}`]);
635+
expect(declaredInstallModes(ts, config)).toEqual({ modes: ['unmanaged'], declared: true });
636+
const data = JSON.parse(ok.files.get(SAMPLE_DATA_FILE)!.toString('utf8')) as Array<{ object: string; records: unknown[] }>;
637+
expect(data.map((d) => [d.object, d.records.length])).toEqual([
638+
['democrm_account', card.run.seed.account!.length],
639+
['democrm_contact', card.run.seed.contact!.length],
640+
]);
641+
});
642+
643+
it("an export of a build that kept the `blank` scaffold's `data:` succeeds, and its config lists the scaffold's rows and the card's", async () => {
644+
const { out, fx, opts } = await setup();
645+
const config = join(scratchProjectDir(fx.work, fx.scenario.card), 'objectstack.config.ts');
646+
writeFileSync(config, readFileSync(BLANK_TEMPLATE_CONFIG, 'utf8'));
647+
const s = await exportGallery(opts());
648+
expect(s.refused).toEqual([]);
649+
const source = join(out, 'apps', CARD_ID, 'source');
650+
expect(stackShape(readFileSync(join(source, 'objectstack.config.ts'), 'utf8')).data).toBe(`[articles, ...${SAMPLE_DATA_BINDING}]`);
651+
const data = JSON.parse(readFileSync(join(source, SAMPLE_DATA_FILE), 'utf8')) as Array<{ object: string }>;
652+
expect(data.map((d) => d.object)).toEqual(['democrm_account', 'democrm_contact']);
653+
});
654+
655+
it.each([
656+
['declares `data:` as a binding, not a list written in place', "import { defineStack } from '@objectstack/spec';\nimport seeds from './seeds';\nexport default defineStack({ manifest: {}, data: seeds });\n", /already declares `data:` as something other than a list written in place/],
657+
['declares `data` as a shorthand property', "import { defineStack } from '@objectstack/spec';\nconst data = [];\nexport default defineStack({ manifest: {}, data });\n", /already declares `data:` as something other than a list written in place/],
658+
['declares `data:` twice', "import { defineStack } from '@objectstack/spec';\nexport default defineStack({ manifest: {}, data: [], 'data': [] });\n", /declares `data:` more than once/],
605659
['spreads a value into the literal', "import { defineStack } from '@objectstack/spec';\nconst base = {};\nexport default defineStack({ ...base, manifest: {} });\n", /spreads a value/],
606660
['default-exports a binding, not the call', "import { defineStack } from '@objectstack/spec';\nconst stack = defineStack({ manifest: {} });\nexport default stack;\n", /does not default-export `defineStack\(\{ … \}\)`/],
607661
['hands `defineStack` no object literal', "import { defineStack } from '@objectstack/spec';\nimport base from './base';\nexport default defineStack(base);\n", /does not default-export `defineStack\(\{ … \}\)`/],
@@ -623,12 +677,14 @@ describe('exportGallery — the trial package: listing text, facet and sample da
623677
expect(ok.omitted).toEqual(['account.owner']);
624678
});
625679

626-
it('an export whose config already declares `data:` is refused with the reason, and no app directory is written', async () => {
680+
it('an export whose config declares `data:` as a value it cannot read is refused with the reason, and no app directory is written', async () => {
627681
const { out, fx, opts } = await setup();
628682
const config = join(scratchProjectDir(fx.work, fx.scenario.card), 'objectstack.config.ts');
629-
writeFileSync(config, "import { defineStack } from '@objectstack/spec';\nexport default defineStack({ manifest: { id: 'com.example.democrm', namespace: 'democrm' }, data: [] });\n");
683+
writeFileSync(config, "import { defineStack } from '@objectstack/spec';\nimport seeds from './seeds';\nexport default defineStack({ manifest: { id: 'com.example.democrm', namespace: 'democrm' }, data: seeds });\n");
630684
const s = await exportGallery(opts());
631-
expect(s.refused).toEqual([{ id: CARD_ID, reasons: ["objectstack.config.ts already declares `data:`, so the card's seed rows are not added as the sample data"] }]);
685+
expect(s.refused).toEqual([
686+
{ id: CARD_ID, reasons: ["objectstack.config.ts already declares `data:` as something other than a list written in place (`data: [ … ]`), so its items cannot be read and the card's seed rows are not added as the sample data"] },
687+
]);
632688
expect(existsSync(join(out, 'apps', CARD_ID))).toBe(false);
633689
});
634690

‎scripts/cross-package-test-inputs.mjs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,6 +1465,7 @@ export const CROSS_PACKAGE_TEST_INPUTS = {
14651465
'docs/qa/platform-checklist/areas/*.json',
14661466
'packages/cli/src/lint/corpus.ts',
14671467
'docs/adr/0150-behavioural-metadata-addressed-by-package-objects-keep-the-literal-prefix.md',
1468+
'packages/create-objectstack/src/templates/blank/objectstack.config.ts',
14681469
],
14691470
heldBy: {
14701471
// The areas directory is listed (`readdirSync`) and its basenames taken,

‎turbo.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,8 @@
727727
"$TURBO_ROOT$/docs/qa/scenarios/builds/*.json",
728728
"$TURBO_ROOT$/docs/qa/platform-checklist/areas/*.json",
729729
"$TURBO_ROOT$/packages/cli/src/lint/corpus.ts",
730-
"$TURBO_ROOT$/docs/adr/0150-behavioural-metadata-addressed-by-package-objects-keep-the-literal-prefix.md"
730+
"$TURBO_ROOT$/docs/adr/0150-behavioural-metadata-addressed-by-package-objects-keep-the-literal-prefix.md",
731+
"$TURBO_ROOT$/packages/create-objectstack/src/templates/blank/objectstack.config.ts"
731732
]
732733
},
733734
"create-objectstack#test": {

0 commit comments

Comments
 (0)