|
| 1 | +import {setup} from '../../setup.mjs'; |
| 2 | + |
| 3 | +const appName = 'InstanceServiceRemoveUndoCaptureTest'; |
| 4 | + |
| 5 | +setup({ |
| 6 | + neoConfig: { |
| 7 | + unitTestMode: true |
| 8 | + }, |
| 9 | + appConfig: { |
| 10 | + name : appName, |
| 11 | + isMounted : () => true, |
| 12 | + vnodeInitialising: false |
| 13 | + } |
| 14 | +}); |
| 15 | + |
| 16 | +import {test, expect} from '@playwright/test'; |
| 17 | +import Neo from '../../../../src/Neo.mjs'; |
| 18 | +import * as core from '../../../../src/core/_export.mjs'; |
| 19 | +import Component from '../../../../src/component/Base.mjs'; |
| 20 | +import Container from '../../../../src/container/Base.mjs'; |
| 21 | +import ComponentManager from '../../../../src/manager/Component.mjs'; // binds Neo.getComponent + registers components |
| 22 | +import InstanceManager from '../../../../src/manager/Instance.mjs'; // binds Neo.get + registers instances |
| 23 | +import InstanceService from '../../../../src/ai/client/InstanceService.mjs'; |
| 24 | +import TransactionService from '../../../../src/ai/TransactionService.mjs'; |
| 25 | +import WriteGuard from '../../../../src/ai/WriteGuard.mjs'; |
| 26 | + |
| 27 | +// `remove_component` forwards server-side as a server-stamped `call_method` `destroy(true)`; the app-side |
| 28 | +// InstanceService.callMethod captures the reverse (`insert(index, config)` on the parent) BEFORE destroy runs — the |
| 29 | +// component's parent/index/config must be snapshotted before it is gone. These tests isolate the capture wiring |
| 30 | +// (`indexOf`/`insert`/`destroy` are stubbed — their vdom path is Neo's, exercised elsewhere), asserting the |
| 31 | +// position-preserving reverse, the round-trip re-dispatch, the generic-call_method non-capture (gpt guardrail 1), |
| 32 | +// and the `buildRemoveReverse` fail-closed branches. |
| 33 | + |
| 34 | +const ID = {agentId: 'agent-a', sessionId: 'sess-a'}; |
| 35 | +let seq = 0; // unique ids per test (Date/random are unavailable; a stubbed destroy won't unregister) |
| 36 | + |
| 37 | +test.describe('Neo.ai.client.InstanceService — remove_component undo capture', () => { |
| 38 | + let parent, child, parentId, childId, service, transactionService, dispatched; |
| 39 | + |
| 40 | + test.beforeEach(() => { |
| 41 | + seq++; |
| 42 | + parentId = `rm-parent-${seq}`; |
| 43 | + childId = `rm-child-${seq}`; |
| 44 | + |
| 45 | + transactionService = Neo.create(TransactionService); |
| 46 | + dispatched = []; |
| 47 | + |
| 48 | + const client = { |
| 49 | + transactionService, |
| 50 | + writeGuard : Neo.create(WriteGuard), |
| 51 | + // mirrors Neo.ai.Client#handleRequest; records the undo re-dispatch + routes it back to the service |
| 52 | + handleRequest: (method, params, ctx) => { |
| 53 | + dispatched.push({method, params}); |
| 54 | + return service[Neo.snakeToCamel(method)]?.(params, ctx) |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + service = Neo.create(InstanceService, {client}); |
| 59 | + parent = Neo.create(Container, {appName, id: parentId, items: []}); |
| 60 | + child = Neo.create(Component, {appName, id: childId, width: 120}); |
| 61 | + |
| 62 | + child.parentId = parentId; |
| 63 | + child.toJSON = () => ({ntype: 'component', id: childId, width: 120}); // stub: a small JSON-safe config — a real toJSON can exceed record()'s payload cap / carry non-serializable refs (the documented serializable-config bound) |
| 64 | + parent.indexOf = () => 2; // stub: the child's tree position (real indexOf needs it mounted in items) |
| 65 | + parent.insert = () => child; // stub: the re-insert (its vdom path is Neo's; we assert the re-dispatch args) |
| 66 | + child.destroy = () => {} // stub: skip the unitTestMode vdom teardown (capture happens before destroy) |
| 67 | + }); |
| 68 | + |
| 69 | + test.afterEach(() => { |
| 70 | + !parent.isDestroyed && parent.destroy() |
| 71 | + }); |
| 72 | + |
| 73 | + test('captures remove\'s inverse — insert(index, config) on the parent — snapshotted before destroy', async () => { |
| 74 | + await service.callMethod({id: childId, method: 'destroy', args: [true], undoKind: 'remove_component'}, ID); |
| 75 | + |
| 76 | + const stack = transactionService.stackOf({id: ID}); |
| 77 | + expect(stack.committed).toHaveLength(1); |
| 78 | + |
| 79 | + const op = stack.committed[0].ops[0]; |
| 80 | + expect(op.forward).toEqual({tool: 'remove_component', args: {componentId: childId}}); |
| 81 | + expect(op.reverse.tool).toBe('call_method'); |
| 82 | + expect(op.reverse.args.id).toBe(parentId); |
| 83 | + expect(op.reverse.args.method).toBe('insert'); |
| 84 | + expect(op.reverse.args.args[0]).toBe(2); // the captured index (position-preserving) |
| 85 | + expect(op.reverse.args.args[1]).toMatchObject({id: childId}); // the captured config (toJSON snapshot) |
| 86 | + expect(op.originWriter).toEqual(ID) |
| 87 | + }); |
| 88 | + |
| 89 | + test('round-trip: a captured remove → undo → re-dispatches insert(index, config) + consumes the tx', async () => { |
| 90 | + await service.callMethod({id: childId, method: 'destroy', args: [true], undoKind: 'remove_component'}, ID); |
| 91 | + expect(transactionService.stackOf({id: ID}).committed).toHaveLength(1); |
| 92 | + |
| 93 | + const result = await service.undo({}, ID); |
| 94 | + |
| 95 | + expect(result.undone).toBe(true); |
| 96 | + const insertCall = dispatched.find(d => d.params?.method === 'insert'); |
| 97 | + expect(insertCall).toBeTruthy(); |
| 98 | + expect(insertCall.params.id).toBe(parentId); |
| 99 | + expect(insertCall.params.args[0]).toBe(2); // re-inserted at the original index |
| 100 | + expect(transactionService.stackOf({id: ID}).committed).toHaveLength(0) // tx consumed |
| 101 | + }); |
| 102 | + |
| 103 | + test('a generic call_method destroy (NO server-stamped marker) is NOT captured — generic call_method stays non-undoable', async () => { |
| 104 | + await service.callMethod({id: childId, method: 'destroy', args: [true]}, ID); // no undoKind |
| 105 | + |
| 106 | + expect(transactionService.stackOf({id: ID}).committed).toHaveLength(0) |
| 107 | + }); |
| 108 | + |
| 109 | + test('buildRemoveReverse is fail-closed — only a marked, canonical, attributed, non-replay remove captures', () => { |
| 110 | + const base = {context: ID, id: childId, method: 'destroy', args: [true], undoKind: 'remove_component', instance: child}; |
| 111 | + |
| 112 | + expect(service.buildRemoveReverse(base)).not.toBeNull(); // happy path |
| 113 | + expect(service.buildRemoveReverse({...base, undoKind: undefined})).toBeNull(); // no marker |
| 114 | + expect(service.buildRemoveReverse({...base, context: {...ID, undoReplay: true}})).toBeNull(); // undo replay |
| 115 | + expect(service.buildRemoveReverse({...base, context: null})).toBeNull(); // no writer identity |
| 116 | + expect(service.buildRemoveReverse({...base, method: 'hide'})).toBeNull(); // non-canonical method |
| 117 | + expect(service.buildRemoveReverse({...base, args: [false]})).toBeNull(); // non-canonical arg (not destroy(true)) |
| 118 | + expect(service.buildRemoveReverse({...base, instance: {parentId: null, toJSON: () => ({})}})).toBeNull() // unresolvable parent |
| 119 | + }) |
| 120 | +}); |
0 commit comments