quotationSubmit.workflow.test.mjs
4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import path from 'node:path'
import { describe, it } from 'node:test'
import { fileURLToPath } from 'node:url'
import {
classifyQuotationResponse,
submitStandardQuotation
} from '../pages/carQuotation/services/standardQuotationSubmit.workflow.js'
import { submitSimpleQuotation } from '../pages/carQuotation/services/simpleQuotationSubmit.workflow.js'
const projectRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..')
it('quotation pages do not use an async Promise executor', () => {
for (const relativePath of [
'packageEquity/pages/carQuotation/createOrderCarQuotation/createOrderCarQuotation.js',
'packageEquity/pages/carQuotation/createOrderCarQuotationSimple/createOrderCarQuotationSimple.js'
]) {
assert.doesNotMatch(
readFileSync(path.join(projectRoot, relativePath), 'utf8'),
/new\s+Promise\s*\(\s*async\b/,
relativePath
)
}
})
it('quotation pages consume workflow status before navigating', () => {
const standard = readFileSync(
path.join(
projectRoot,
'packageEquity/pages/carQuotation/createOrderCarQuotation/createOrderCarQuotation.js'
),
'utf8'
)
const simple = readFileSync(
path.join(
projectRoot,
'packageEquity/pages/carQuotation/createOrderCarQuotationSimple/createOrderCarQuotationSimple.js'
),
'utf8'
)
assert.match(standard, /outcome\.status\s*!==\s*['"]success['"]/)
assert.match(standard, /return\s+submitStandardQuotation\s*\(/)
assert.match(simple, /submitOutcome\.status\s*!==\s*['"]success['"]/)
assert.match(simple, /status:\s*submitOutcome\.status/)
})
describe('standardQuotationSubmit.workflow', () => {
it('keeps add payload identity and classifies a string zero as success', async () => {
const payload = { owner: { owner: '张三' }, check: true }
let received
const result = await submitStandardQuotation(
{ payload, isEdit: false },
{
quotationAdd: async value => {
received = value
return { code: '0', data: 101 }
},
updateQuotation: async () => assert.fail('must not update')
}
)
assert.equal(received, payload)
assert.deepEqual(result, {
status: 'success',
response: { code: '0', data: 101 }
})
})
it('selects update only for edit and propagates API rejection', async () => {
const rejection = new Error('network unavailable')
await assert.rejects(
submitStandardQuotation(
{ payload: { id: 9 }, isEdit: true },
{
quotationAdd: async () => assert.fail('must not add'),
updateQuotation: async () => {
throw rejection
}
}
),
rejection
)
})
it('does not leave a rejected request pending', async () => {
const settled = await Promise.race([
submitStandardQuotation(
{ payload: {}, isEdit: false },
{
quotationAdd: async () => {
throw new Error('boom')
},
updateQuotation: async () => ({ code: 0 })
}
).then(
() => 'resolved',
() => 'rejected'
),
new Promise(resolve => setTimeout(() => resolve('timeout'), 50))
])
assert.equal(settled, 'rejected')
})
it('classifies non-zero and missing responses as business errors', () => {
assert.deepEqual(classifyQuotationResponse({ code: 2, message: '失败' }), {
ok: false,
code: 2,
message: '失败',
data: undefined
})
assert.deepEqual(classifyQuotationResponse(undefined), {
ok: false,
code: undefined,
message: '报价失败',
data: undefined
})
})
})
describe('simpleQuotationSubmit.workflow', () => {
for (const [action, expectedDependency] of [
['add', 'quotationAdd'],
['update', 'updateQuotation'],
['correct', 'errorOrderById']
]) {
it(`selects ${expectedDependency} for ${action} and preserves payload identity`, async () => {
const payload = { id: 7, marker: action }
const calls = []
const dependencies = Object.fromEntries(
['quotationAdd', 'updateQuotation', 'errorOrderById'].map(name => [
name,
async value => {
calls.push([name, value])
return { code: 0, data: 7 }
}
])
)
const result = await submitSimpleQuotation(
{ action, payload },
dependencies
)
assert.deepEqual(calls, [[expectedDependency, payload]])
assert.equal(result.status, 'success')
})
}
it('rejects an unsupported action before invoking an API', async () => {
await assert.rejects(
submitSimpleQuotation(
{ action: 'unknown', payload: {} },
{
quotationAdd: async () => assert.fail('must not add'),
updateQuotation: async () => assert.fail('must not update'),
errorOrderById: async () => assert.fail('must not correct')
}
),
/Unsupported simple quotation action/
)
})
})