Skip to content
Merged
12 changes: 12 additions & 0 deletions .changeset/39395-stored-bulk-actions-declared-selection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@objectstack/spec': patch
---

fix(spec): `os migrate meta --stored --apply` rewrites a stored list view's `bulkActions` when the object's declared `actionLayout.selection` already draws its order

Clause-②: no
Metadata-protocol: none

The protocol-19 conversion `view-bulk-actions-to-action-selection` left every list of an object as stored, with a TODO, whenever the object already declared `actionLayout.selection`, without comparing it. The source half (`os migrate meta --from 18 --write`) writes exactly that arm when a list's order is not the default one, merged from every list of the object, so after the source edits a Studio-saved list view row stayed unconverted, `--stored --apply` exited 1, and its TODO prescribed the edit that was already made.

The conversion now reads the declared arm against the list's order the way it already reads each list against the merged order of several lists: the arm agrees when it names every action the list names and its order (`pinned`, then `more`) restricted to those names is the list's. Which of them are buttons is the arm's own split, as declared, never recomputed from one list, since the merge already lets a list's names land in `more`; so an arm the source half merged from several lists agrees with each of their rows replayed alone. When it agrees, the list moves and the arm is left as declared; the named actions still gain `'selection'` where they lack it. Any other arm (another order, an action of the list not named, not an arm) is the author's arrangement: it is never overwritten, the row stays as stored, and the TODO now names the declared arm beside an arm that would keep the list's order. The workaround (re-save the view without `bulkActions`) still works.
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.

/**
* `view-bulk-actions-to-action-selection` (protocol 19) — the object that
* ALREADY declares `actionLayout.selection`.
*
* The source half (`os migrate meta --from 18 --write`) writes the arm
* `{ pinned: <first three>, more?: <the rest> }` from the list's order. The
* stored half (`os migrate meta --stored --apply`) then replays this same
* entry over a Studio-saved view row with the deployment's declared objects
* and actions beside it (`judgeStoredViewBulkActions` builds exactly the
* `{ views: [row], objects, actions }` stack used here). The row is rewritten
* when the declared arm already keeps its order — every name in the arm, and
* the arm's order (pinned, then more) restricted to the row's names is the
* row; which names are buttons is the arm's own split, as the entry's
* multi-list merge lets a list's names land in more — and left as stored,
* with its TODO, for any other arm: a declared arrangement is never
* overwritten and never read as agreeing with a list it does not keep.
*/

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

import { ALL_CONVERSIONS } from '../../registry.js';
import type { ConversionApplication, ConversionTodoDetail } from '../../types.js';

const ID = 'view-bulk-actions-to-action-selection';
const entry = () => ALL_CONVERSIONS.find((c) => c.id === ID)!;

/** Five actions of `crm_lead`, declared in alphabetical order, all already placed at `selection`. */
const ACTIONS = ['alpha_act', 'beta_act', 'gamma_act', 'delta_act', 'epsilon_act'].map((name) => ({
name,
label: name,
objectName: 'crm_lead',
type: 'script',
locations: ['record', 'selection'],
}));

/** A Studio-saved list view row naming `names`, as the stored pass reads it. */
const row = (names: string[]) => ({
name: 'lead_grid',
object: 'crm_lead',
list: { type: 'grid', data: { provider: 'object', object: 'crm_lead' }, columns: ['name'], bulkActions: names },
});

/** The stack the stored pass replays the entry over: the row alone, beside the deployment's declarations. */
const storedStack = (names: string[], selection: unknown) => ({
views: [row(names)],
objects: [{ name: 'crm_lead', actionLayout: { selection } }],
actions: structuredClone(ACTIONS),
});

function run(stack: Record<string, unknown>) {
const applied: ConversionApplication[] = [];
const todos: ConversionTodoDetail[] = [];
const out = entry().apply(structuredClone(stack), (d) => applied.push(d), {
atRest: true,
reportTodo: (t) => todos.push(t),
}) as { views: Array<{ list: Record<string, unknown> }>; objects: Array<Record<string, unknown>> };
return { out, applied, todos };
}

describe(`${ID} — a declared actionLayout.selection`, () => {
// Not the default order (alpha, beta, …), so keeping it needs the arm.
const TWO = ['beta_act', 'alpha_act'];
const FOUR = ['delta_act', 'beta_act', 'alpha_act', 'gamma_act'];

it('rewrites the row when the declared arm is the one the source half writes (pinned only)', () => {
const { out, applied, todos } = run(storedStack(TWO, { pinned: ['beta_act', 'alpha_act'] }));
expect(todos).toEqual([]);
expect(out.views[0]!.list).not.toHaveProperty('bulkActions');
// The row is the whole write: no action and no object is touched.
expect(applied.filter((d) => !d.path.startsWith('views[0]'))).toEqual([]);
expect(out.objects[0]!.actionLayout).toEqual({ selection: { pinned: ['beta_act', 'alpha_act'] } });
});

it('rewrites the row when the declared arm is the one the source half writes (pinned and more)', () => {
const arm = { pinned: ['delta_act', 'beta_act', 'alpha_act'], more: ['gamma_act'] };
const { out, applied, todos } = run(storedStack(FOUR, arm));
expect(todos).toEqual([]);
expect(out.views[0]!.list).not.toHaveProperty('bulkActions');
expect(applied.filter((d) => !d.path.startsWith('views[0]'))).toEqual([]);
});

it.each([
['the arm the entry writes with an empty more', TWO, { pinned: ['beta_act', 'alpha_act'], more: [] }],
['wider, naming another action after the row', TWO, { pinned: ['beta_act', 'alpha_act', 'gamma_act'] }],
['wider, naming another action between the row', TWO, { pinned: ['beta_act', 'gamma_act', 'alpha_act'] }],
['wider in more', FOUR, { pinned: ['delta_act', 'beta_act', 'alpha_act'], more: ['gamma_act', 'epsilon_act'] }],
// The arm keeps the row's order; which names are buttons is the arm's own
// split, and the source half's merge accepts a list whose names land in more.
['the same order split differently', TWO, { pinned: ['beta_act'], more: ['alpha_act'] }],
['wider, with a named action in more', TWO, { pinned: ['gamma_act', 'delta_act', 'beta_act'], more: ['alpha_act'] }],
])('rewrites the row when the declared arm is %s', (_label, names, arm) => {
const { out, applied, todos } = run(storedStack(names, arm));
expect(todos).toEqual([]);
expect(out.views[0]!.list).not.toHaveProperty('bulkActions');
expect(applied.filter((d) => !d.path.startsWith('views[0]'))).toEqual([]);
expect(out.objects[0]!.actionLayout).toEqual({ selection: arm });
});

it.each([
['one a prefix of the other', [TWO, [...TWO, 'gamma_act']], { pinned: ['beta_act', 'alpha_act', 'gamma_act'] }],
[
'the shorter one past the third place of the merged order',
[['alpha_act', 'beta_act', 'gamma_act', 'epsilon_act', 'delta_act'], ['epsilon_act', 'delta_act']],
{ pinned: ['alpha_act', 'beta_act', 'gamma_act'], more: ['epsilon_act', 'delta_act'] },
],
])('rewrites each stored row of an object whose arm the source half merged from two lists, %s', (_label, lists, merged) => {
// Source half: both lists in one stack, the object declaring no arm.
const source = run({
views: lists.map((names, i) => ({ ...row(names), name: `lead_grid_${i}` })),
objects: [{ name: 'crm_lead' }],
actions: structuredClone(ACTIONS),
});
expect(source.todos).toEqual([]);
const arm = (source.out.objects[0]!.actionLayout as { selection: unknown }).selection;
expect(arm).toEqual(merged);
// Stored half: each row replayed alone against that arm.
for (const names of lists) {
const { out, applied, todos } = run(storedStack(names, arm));
expect(todos).toEqual([]);
expect(out.views[0]!.list).not.toHaveProperty('bulkActions');
expect(applied.filter((d) => !d.path.startsWith('views[0]'))).toEqual([]);
}
});

it.each([
['another order', TWO, { pinned: ['alpha_act', 'beta_act'] }],
['another order across the split', TWO, { pinned: ['alpha_act'], more: ['beta_act'] }],
['a subset of the names', FOUR, { pinned: ['delta_act', 'beta_act', 'alpha_act'] }],
['wider, in another order', TWO, { pinned: ['alpha_act', 'gamma_act', 'beta_act'] }],
['not an arm', TWO, ['beta_act', 'alpha_act']],
])('leaves the row as stored, with its TODO, when the declared arm is %s', (_label, names, arm) => {
const { out, applied, todos } = run(storedStack(names, arm));
expect(applied).toEqual([]);
expect(todos.map((t) => t.path)).toEqual(['views[0].list.bulkActions']);
expect(todos[0]!.reason).toContain(`the arm it declares, ${JSON.stringify(arm)}, is not`);
expect(out.views[0]!.list.bulkActions).toEqual(names);
expect(out.objects[0]!.actionLayout).toEqual({ selection: arm });
});

it('still writes the arm where the object declares none, and still leaves an undeclared object as stored', () => {
const fresh = run({ ...storedStack(TWO, undefined), objects: [{ name: 'crm_lead' }] });
expect(fresh.todos).toEqual([]);
expect(fresh.out.objects[0]!.actionLayout).toEqual({ selection: { pinned: ['beta_act', 'alpha_act'] } });

const orphan = run({ ...storedStack(TWO, undefined), objects: [] });
expect(orphan.applied).toEqual([]);
expect(orphan.todos.map((t) => t.path)).toEqual(['views[0].list.bulkActions']);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,20 @@ import { type Dict, isDict } from '../shared.js';
* package's action, a misspelling), or its object cannot be told;
* - lists the same actions in an order another list of the object contradicts;
* - needs `actionLayout.selection` on an object the stack does not declare, or
* on one that already has a `selection` arm.
* on one that already has a different `selection` arm.
*
* An object whose `selection` arm already keeps the lists' order needs
* nothing more: its lists move and the arm is left as declared. It keeps it
* when it names every action the lists merge to, and its order (`pinned`,
* then `more`) restricted to those names is theirs. Which names are buttons
* is read from the arm as declared, never recomputed from one list: the
* merge below already lets a list's names fall in `more`. So an arm the
* source half merged from several lists keeps the order of each of them, and
* the stored half rewrites each Studio-saved row against it. For a list not
* in the default order, any other arm (a name missing, another order, not an
* arm) is the author's arrangement: it is never overwritten, nor read as
* agreeing. (A list already in the default order moves before any arm is
* read.)
*
* A list left as stored is refused by the protocol-19 schema until it is
* moved by hand.
Expand Down Expand Up @@ -299,25 +312,56 @@ function planObject(
.filter((n, i, all) => all.indexOf(n) === i);
if (byDefault.join('\u0000') === plan.names.join('\u0000')) return plan;

const pinned = plan.names.slice(0, OBJECT_SELECTION_ACTIONS_LIMIT);
const more = plan.names.slice(OBJECT_SELECTION_ACTIONS_LIMIT);
const arm = more.length > 0 ? { pinned, more } : { pinned };
const objects = Array.isArray(stack.objects) ? stack.objects : [];
const oi = objects.findIndex((o) => isDict(o) && o.name === object);
const target = oi >= 0 ? (objects[oi] as Dict) : undefined;
const layout = target?.actionLayout;
if (!target || (isDict(layout) && layout.selection !== undefined)) {
const declaredArm = isDict(layout) ? layout.selection : undefined;
// The declared arm already draws this order (the source half wrote it, from
// these lists or from more of the object's lists): there is nothing to write.
if (target && declaredArm !== undefined && armKeepsOrder(declaredArm, plan.names)) return plan;
if (!target || declaredArm !== undefined) {
plan.todo = `The selection bar of object \`${object}\` drew its actions in the order its list views name `
+ `them (${plan.names.map((n) => `\`${n}\``).join(', ')}), which is not their default order, and keeping it `
+ `needs the object's \`actionLayout.selection\`, which ${target ? 'it already declares' : 'this metadata does not declare the object to carry'}. `
+ 'Left as stored; add `\'selection\'` to the `locations` of each action they name, write the order as '
+ 'the object\'s `actionLayout.selection`, and delete the key on each view.';
+ `them (${plan.names.map((n) => `\`${n}\``).join(', ')}), which is not their default order. Keeping it needs `
+ 'the object\'s `actionLayout.selection` to name them all in that order (`pinned`, then `more`), '
+ `as ${JSON.stringify(arm)} does, which `
+ `${target ? `the arm it declares, ${JSON.stringify(declaredArm)}, is not` : 'this metadata does not declare the object to carry'}. `
+ 'Left as stored; add `\'selection\'` to the `locations` of each action they name, write the order you '
+ 'keep as the object\'s `actionLayout.selection`, and delete the key on each view.';
return plan;
}
const pinned = plan.names.slice(0, OBJECT_SELECTION_ACTIONS_LIMIT);
const more = plan.names.slice(OBJECT_SELECTION_ACTIONS_LIMIT);
plan.arm = more.length > 0 ? { pinned, more } : { pinned };
plan.arm = arm;
plan.objectPath = `objects[${oi}]`;
return plan;
}

/** The string entries of a declared arm part, or `undefined` when it is not an array of strings. */
function armPart(part: unknown): string[] | undefined {
if (part === undefined) return [];
return Array.isArray(part) && part.every((n) => typeof n === 'string') ? part : undefined;
}

/**
* Whether a declared `selection` arm already keeps `names` in their order:
* every name is in the arm, and the arm's order (`pinned`, then `more`)
* restricted to the names is `names`. Which of them are buttons is the arm's
* own split, as declared: the multi-list merge above lets a list's names land
* in `more` too, so an arm the source half built from several lists keeps the
* order of each of them read alone. Any other arm (a name missing, another
* order, not an arm) is the author's arrangement, never read as agreeing.
*/
function armKeepsOrder(declared: unknown, names: readonly string[]): boolean {
if (!isDict(declared)) return false;
const pinned = armPart(declared.pinned);
const more = armPart(declared.more);
if (!pinned || !more) return false;
const drawn = [...pinned, ...more].filter((n) => names.includes(n));
return drawn.join('\u0000') === names.join('\u0000');
}

/** The TODO path of one list. */
function todoAt(list: ListSite): string {
return `${list.path}.bulkActions`;
Expand Down
Loading