Skip to content

Commit 44fb60d

Browse files
committed
test(app-shell): the door double carries a client-typed face
Claude-Session: https://claude.ai/code/session_01L6bhgWAbKPqt7vTgX5qgUn Co-authored-by: Claude <noreply@anthropic.com>
1 parent ee397fd commit 44fb60d

3 files changed

Lines changed: 70 additions & 62 deletions

File tree

‎ui/packages/app-shell/src/views/__tests__/draftPublishDoor-37440.ts‎

Lines changed: 62 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
* publish reaches the door: the race the pins are for.
2424
*/
2525

26+
import type { MetadataClient } from '@object-ui/data-objectstack';
27+
2628
type Options = Record<string, unknown>;
2729

2830
interface Row {
@@ -37,6 +39,8 @@ export interface DraftPublishDoor {
3739
reset: (type: string, name: string, options?: Options) => Promise<unknown>;
3840
getDraft: (type: string, name: string, options?: Options) => Promise<unknown>;
3941
};
42+
/** The same doors, typed as the console's client for callers that take one. */
43+
metadataClient: Pick<MetadataClient, 'save' | 'publish' | 'reset' | 'getDraft'>;
4044
/** The pending draft body at the address, or `undefined`. */
4145
draftOf: (type: string, name: string, packageId?: string | null) => unknown;
4246
/** The live body at the address, or `undefined`. */
@@ -71,63 +75,67 @@ export function draftPublishDoor(): DraftPublishDoor {
7175
drafts.set(k, row);
7276
return row;
7377
};
74-
return {
75-
client: {
76-
async save(type, name, body, options = {}) {
77-
calls.push(['save', type, name, options]);
78-
const k = key(type, name, options.packageId);
79-
if (options.mode !== 'draft') {
80-
const row = { body, version: `live-${++seq}` };
81-
live.set(k, row);
82-
return { success: true, version: row.version };
83-
}
84-
const current = drafts.get(k);
85-
if (options.ifNoneMatch === '*' && current) throw conflict(current.version);
86-
if (typeof options.ifMatch === 'string' && options.ifMatch !== current?.version) {
87-
throw conflict(current?.version ?? null);
88-
}
89-
const row = writeDraft(k, body);
90-
return { success: true, version: row.version, state: 'draft' };
91-
},
92-
async publish(type, name, options = {}) {
93-
calls.push(['publish', type, name, options]);
94-
if (pendingInterleave) {
95-
const run = pendingInterleave;
96-
pendingInterleave = null;
97-
run();
98-
}
99-
const k = key(type, name, options.packageId);
100-
const draft = drafts.get(k);
101-
if (options.draftVersion !== undefined && options.draftVersion !== draft?.version) {
102-
throw conflict(draft?.version ?? null);
103-
}
104-
const active = live.get(k);
105-
if (typeof options.ifMatch === 'string' && options.ifMatch !== active?.version) throw conflict(active?.version ?? null);
106-
if (options.ifNoneMatch === '*' && active) throw conflict(active.version);
107-
if (!draft) throw Object.assign(new Error('no draft'), { status: 404, code: 'NO_DRAFT' });
108-
const row = { body: draft.body, version: `live-${++seq}` };
78+
const client: DraftPublishDoor['client'] = {
79+
async save(type, name, body, options = {}) {
80+
calls.push(['save', type, name, options]);
81+
const k = key(type, name, options.packageId);
82+
if (options.mode !== 'draft') {
83+
const row = { body, version: `live-${++seq}` };
10984
live.set(k, row);
110-
drafts.delete(k);
11185
return { success: true, version: row.version };
112-
},
113-
async reset(type, name, options = {}) {
114-
calls.push(['reset', type, name, options]);
115-
const k = key(type, name, options.packageId);
116-
if (options.state === 'draft') {
117-
const draft = drafts.get(k);
118-
if (typeof options.ifMatch === 'string' && options.ifMatch !== draft?.version) throw conflict(draft?.version ?? null);
119-
drafts.delete(k);
120-
} else {
121-
live.delete(k);
122-
}
123-
return { success: true };
124-
},
125-
async getDraft(type, name, options = {}) {
126-
calls.push(['getDraft', type, name, options]);
127-
const draft = drafts.get(key(type, name, options.packageId));
128-
return draft ? { type, name, item: draft.body, version: draft.version } : null;
129-
},
86+
}
87+
const current = drafts.get(k);
88+
if (options.ifNoneMatch === '*' && current) throw conflict(current.version);
89+
if (typeof options.ifMatch === 'string' && options.ifMatch !== current?.version) {
90+
throw conflict(current?.version ?? null);
91+
}
92+
const row = writeDraft(k, body);
93+
return { success: true, version: row.version, state: 'draft' };
94+
},
95+
async publish(type, name, options = {}) {
96+
calls.push(['publish', type, name, options]);
97+
if (pendingInterleave) {
98+
const run = pendingInterleave;
99+
pendingInterleave = null;
100+
run();
101+
}
102+
const k = key(type, name, options.packageId);
103+
const draft = drafts.get(k);
104+
if (options.draftVersion !== undefined && options.draftVersion !== draft?.version) {
105+
throw conflict(draft?.version ?? null);
106+
}
107+
const active = live.get(k);
108+
if (typeof options.ifMatch === 'string' && options.ifMatch !== active?.version) throw conflict(active?.version ?? null);
109+
if (options.ifNoneMatch === '*' && active) throw conflict(active.version);
110+
if (!draft) throw Object.assign(new Error('no draft'), { status: 404, code: 'NO_DRAFT' });
111+
const row = { body: draft.body, version: `live-${++seq}` };
112+
live.set(k, row);
113+
drafts.delete(k);
114+
return { success: true, version: row.version };
130115
},
116+
async reset(type, name, options = {}) {
117+
calls.push(['reset', type, name, options]);
118+
const k = key(type, name, options.packageId);
119+
if (options.state === 'draft') {
120+
const draft = drafts.get(k);
121+
if (typeof options.ifMatch === 'string' && options.ifMatch !== draft?.version) throw conflict(draft?.version ?? null);
122+
drafts.delete(k);
123+
} else {
124+
live.delete(k);
125+
}
126+
return { success: true };
127+
},
128+
async getDraft(type, name, options = {}) {
129+
calls.push(['getDraft', type, name, options]);
130+
const draft = drafts.get(key(type, name, options.packageId));
131+
return draft ? { type, name, item: draft.body, version: draft.version } : null;
132+
},
133+
};
134+
return {
135+
client,
136+
// The doors answer what the console's client answers for these calls; its
137+
// generic signatures are what the cast stands in for.
138+
metadataClient: client as unknown as DraftPublishDoor['metadataClient'],
131139
draftOf: (type, name, packageId) => drafts.get(key(type, name, packageId))?.body,
132140
liveOf: (type, name, packageId) => live.get(key(type, name, packageId))?.body,
133141
seedLive: (type, name, packageId, body) => {

‎ui/packages/app-shell/src/views/metadata-admin/detailSaveAtOnce-37440.test.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe("the item page's Save publishes only the draft it wrote (objectstack#37
3636
const door = draftPublishDoor();
3737
const draftVersion = await savedDraft(door);
3838
door.interleave(TARGET.type, TARGET.name, PKG, { label: 'theirs' });
39-
const outcome = await publishDetailSave(door.client, { ...TARGET, draftVersion }, false);
39+
const outcome = await publishDetailSave(door.metadataClient, { ...TARGET, draftVersion }, false);
4040
expect(outcome).toMatchObject({ kind: 'refused', draftDiscarded: false });
4141
expect(door.liveOf(TARGET.type, TARGET.name, PKG)).toBeUndefined();
4242
expect(door.draftOf(TARGET.type, TARGET.name, PKG)).toEqual({ label: 'theirs' });
@@ -49,7 +49,7 @@ describe("the item page's Save publishes only the draft it wrote (objectstack#37
4949
const draftVersion = await savedDraft(door);
5050
const { guard, ask } = guardOver(door);
5151
door.interleave(TARGET.type, TARGET.name, PKG, { label: 'theirs' });
52-
const outcome = await publishDetailSave(door.client, { ...TARGET, draftVersion }, false, guard.publish);
52+
const outcome = await publishDetailSave(door.metadataClient, { ...TARGET, draftVersion }, false, guard.publish);
5353
expect(outcome.kind).toBe('refused');
5454
expect(outcome.kind === 'refused' && isPublishDraftMoved(outcome.error)).toBe(true);
5555
expect(ask).not.toHaveBeenCalled();
@@ -62,7 +62,7 @@ describe("the item page's Save publishes only the draft it wrote (objectstack#37
6262
const door = draftPublishDoor();
6363
const draftVersion = await savedDraft(door);
6464
const pinned = guarded ? guardOver(door).guard.publish : undefined;
65-
expect(await publishDetailSave(door.client, { ...TARGET, draftVersion }, false, pinned)).toEqual({ kind: 'published' });
65+
expect(await publishDetailSave(door.metadataClient, { ...TARGET, draftVersion }, false, pinned)).toEqual({ kind: 'published' });
6666
expect(door.liveOf(TARGET.type, TARGET.name, PKG)).toEqual({ label: 'mine' });
6767
}
6868
});

‎ui/packages/app-shell/src/views/studio-design/studioSaveBar.draftPin-37440.test.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('publishSavedItem, unpinned door: the publish names the draft the Save
2828
const door = draftPublishDoor();
2929
const draftVersion = await savedDraft(door, { label: 'mine' });
3030
door.interleave(ITEM.type, ITEM.name, PKG, { label: 'theirs' });
31-
const live = await publishSavedItem(door.client, { ...ITEM, draftVersion }, false);
31+
const live = await publishSavedItem(door.metadataClient, { ...ITEM, draftVersion }, false);
3232
expect(live.ok).toBe(false);
3333
expect(!live.ok && isDraftVersionConflict(live.error)).toBe(true);
3434
expect(!live.ok && live.draftDiscarded).toBe(false);
@@ -41,7 +41,7 @@ describe('publishSavedItem, unpinned door: the publish names the draft the Save
4141
it('a refusal for another reason takes back only the draft this Save wrote, pinned to its receipt', async () => {
4242
const door = draftPublishDoor();
4343
const draftVersion = await savedDraft(door, { label: 'mine' });
44-
const refusing = { ...door.client, publish: async () => Promise.reject(Object.assign(new Error('rule'), { status: 422 })) };
44+
const refusing = { ...door.metadataClient, publish: async () => Promise.reject(Object.assign(new Error('rule'), { status: 422 })) };
4545
const live = await publishSavedItem(refusing, { ...ITEM, draftVersion }, false);
4646
expect(!live.ok && live.draftDiscarded).toBe(true);
4747
expect(door.calls.find(([c]) => c === 'reset')?.[3]).toEqual({ state: 'draft', packageId: PKG, ifMatch: draftVersion });
@@ -51,7 +51,7 @@ describe('publishSavedItem, unpinned door: the publish names the draft the Save
5151
it('CONTROL: with no write in between, the same pinned publish takes the Save live', async () => {
5252
const door = draftPublishDoor();
5353
const draftVersion = await savedDraft(door, { label: 'mine' });
54-
expect(await publishSavedItem(door.client, { ...ITEM, draftVersion }, false)).toEqual({ ok: true });
54+
expect(await publishSavedItem(door.metadataClient, { ...ITEM, draftVersion }, false)).toEqual({ ok: true });
5555
expect(door.liveOf(ITEM.type, ITEM.name, PKG)).toEqual({ label: 'mine' });
5656
});
5757
});
@@ -61,7 +61,7 @@ describe('publishSavedItem, a create (`publishCreated`): the publish names the d
6161
const door = draftPublishDoor();
6262
const draftVersion = await savedDraft(door, { label: 'mine' });
6363
door.interleave(ITEM.type, ITEM.name, PKG, { label: 'theirs' });
64-
const live = await publishSavedItem(door.client, { ...ITEM, draftVersion }, false, publishCreated);
64+
const live = await publishSavedItem(door.metadataClient, { ...ITEM, draftVersion }, false, publishCreated);
6565
expect(live.ok).toBe(false);
6666
expect(!live.ok && 'draftDiscarded' in live && live.draftDiscarded).toBe(false);
6767
expect(door.liveOf(ITEM.type, ITEM.name, PKG)).toBeUndefined();
@@ -73,7 +73,7 @@ describe('publishSavedItem, a create (`publishCreated`): the publish names the d
7373
it('CONTROL: with no write in between, the create goes live', async () => {
7474
const door = draftPublishDoor();
7575
const draftVersion = await savedDraft(door, { label: 'mine' });
76-
expect(await publishSavedItem(door.client, { ...ITEM, draftVersion }, false, publishCreated)).toEqual({ ok: true });
76+
expect(await publishSavedItem(door.metadataClient, { ...ITEM, draftVersion }, false, publishCreated)).toEqual({ ok: true });
7777
expect(door.liveOf(ITEM.type, ITEM.name, PKG)).toEqual({ label: 'mine' });
7878
});
7979
});

0 commit comments

Comments
 (0)