import test from 'node:test' import assert from 'node:assert/strict' import { assistantCanExcludeDraftRows, assistantExcludeDraftRows, assistantRestoreDraftRow } from '../../src/utils/assistant-draft-exclusions.js' import { assistantRowsSignature, canConfirmAssistantDraft } from '../../src/utils/business-assistant.js' const draft = { id: 1, action: 'purchase.create', status: 'WAIT_CONFIRM', version: 1, ready: true, results: [] } const action = { allowed: true, query: false } const context = { draft, action } const rows = [{ _rowId: 'a', materialName: '水泵', quantity: 2 }, { _rowId: 'b', materialName: '未匹配项目', _sourceRow: 24, _sourceText: '原表第24行', _errors: ['待补充'] }, { _rowId: 'c', materialName: '管件', quantity: 4 }] test('removing draft rows preserves data for undo and forces re-preview instead of calling business deletion', () => { const original = structuredClone(rows), result = assistantExcludeDraftRows(rows, ['b'], context) assert.deepEqual(result.rows.map(row => row._rowId), ['a', 'c']); assert.deepEqual(result.removed[0].row, rows[1]); assert.deepEqual(rows, original) const dirty = assistantRowsSignature(result.rows) !== assistantRowsSignature(rows) assert.equal(canConfirmAssistantDraft(draft, { dirty }), false) assert.deepEqual(assistantRestoreDraftRow(result.rows, result.removed[0], context), rows) }) test('bulk removals restore in either order without replacing other row edits', () => { const result = assistantExcludeDraftRows(rows, ['a', 'b'], context) for (const order of [result.removed, [...result.removed].reverse()]) { let restored = result.rows.map(row => ({ ...row, quantity: 8 })) for (const entry of order) restored = assistantRestoreDraftRow(restored, entry, context) assert.deepEqual(restored.map(row => row._rowId), ['a', 'b', 'c']); assert.equal(restored[2].quantity, 8) } }) test('this applies to pending imports, master data and other writes, never queries, permissions-denied, completed or price-ledger drafts', () => { for (const key of ['purchase.create', 'item.save', 'user.create', 'stock.move', 'purchase.importQuote']) assert.equal(assistantCanExcludeDraftRows({ ...draft, action: key }, action), true) for (const options of [{ draft: { ...draft, readOnly: true }, action }, { draft, action: { allowed: false } }, { draft, action: { query: true } }, { draft: { ...draft, status: 'SUCCEEDED' }, action }, { draft: { ...draft, permissionDenied: true }, action }, { draft: { ...draft, action: 'purchase.correctPrice' }, action }]) { assert.equal(assistantExcludeDraftRows(rows, ['b'], options).rows, rows) } }) test('successful and last remaining rows are protected; duplicate restores and unknown removals do not change the draft', () => { const completed = { ...draft, results: [{ rowId: 'a', status: 'SUCCEEDED' }] } assert.equal(assistantExcludeDraftRows(rows, ['a', 'b'], { draft: completed, action }).rows, rows) assert.match(assistantExcludeDraftRows(rows, ['a', 'b', 'c'], context).error, /取消操作/) assert.equal(assistantExcludeDraftRows(rows, ['missing'], context).rows, rows) assert.equal(assistantRestoreDraftRow(rows, { row: rows[1], index: 1 }, context), rows) })