import assert from 'node:assert/strict' import test from 'node:test' import { effectScope, reactive, nextTick } from 'vue' import { useMobileCertFacet } from '../src/views/m/pl/plan-cert/useMobileCertFacet' function harness() { const requests: any[] = [] const context = reactive({ planNoIn: 'chosen', planType: 'outbound', page: 2, sortField: 'date' }) const account = reactive({ token: 'a' }) const scope = effectScope() const api = scope.run(() => useMobileCertFacet({ defs: [{ prop: 'planNo', type: 'enum', label: '计划编号' }], context: () => context, account: () => account.token, fetch: (params: any) => new Promise((resolve, reject) => requests.push({ params, resolve, reject })) }) )! return { api, requests, context, account, scope } } const flush = async () => { await Promise.resolve() await nextTick() } test('remote keyword finds values outside first 500; strips self and paging; preserves selection and cancel', async () => { const h = harness() h.api.open('planNo', ['chosen']) assert.equal(h.requests[0].params.planNoIn, undefined) assert.equal(h.requests[0].params.page, undefined) assert.equal(h.requests[0].params.sortField, undefined) h.requests[0].resolve({ list: Array.from({ length: 500 }, (_, i) => ({ value: `${i}` })) }) await flush() h.api.search('PO20261009022') assert.equal(h.requests[1].params.keyword, 'PO20261009022') h.requests[1].resolve({ data: { list: [{ value: 'PO20261009022' }] } }) await flush() assert.equal(h.api.state.options[0].value, 'PO20261009022') h.api.toggle('PO20261009022') h.api.search('no result') h.requests[2].resolve({ list: [] }) await flush() assert.deepEqual(h.api.confirm(), { field: 'planNo', selected: ['chosen', 'PO20261009022'] }) h.api.open('planNo', ['chosen']) h.api.toggle('changed') h.api.close() assert.equal(h.api.confirm(), null) h.api.open('planNo', ['chosen']) assert.deepEqual(h.api.state.selected, ['chosen']) h.scope.stop() }) test('reverse query success/error/finally cannot replace active request; retry is distinct from empty', async () => { const h = harness() h.api.open('planNo', []) h.api.search('new') h.requests[0].reject(new Error('stale')) await flush() assert.equal(h.api.state.error, '') assert.equal(h.api.state.loading, true) h.requests[1].reject(new Error('network')) await flush() assert.equal(h.api.state.error, 'network') assert.equal(h.api.state.loading, false) h.api.retry() h.requests[2].resolve({ list: [] }) await flush() assert.equal(h.api.state.error, '') assert.deepEqual(h.api.state.options, []) h.api.search('older') h.api.search('latest') h.requests[4].resolve({ list: [{ value: 'latest' }] }) await flush() h.requests[3].resolve({ list: [{ value: 'older' }] }) await flush() assert.equal(h.api.state.options[0].value, 'latest') h.scope.stop() }) test('close/reopen, context, account and disposal invalidate requests and confirmation', async () => { const h = harness() h.api.open('planNo', ['a']) h.api.close() h.api.open('planNo', ['b']) h.requests[0].resolve({ list: [{ value: 'old' }] }) await flush() assert.deepEqual(h.api.state.options, []) assert.equal(h.api.state.loading, true) h.context.planType = 'inbound' assert.equal(h.api.confirm(), null) h.requests[1].reject(new Error('stale')) await flush() assert.equal(h.api.state.error, '') h.api.open('planNo', ['b']) h.account.token = 'b' assert.equal(h.api.confirm(), null) h.api.open('planNo', ['c']) h.scope.stop() h.requests[3].resolve({ list: [{ value: 'after disposal' }] }) await flush() assert.equal(h.api.confirm(), null) assert.deepEqual(h.api.state.options, []) })