61 lines
3.3 KiB
JavaScript
61 lines
3.3 KiB
JavaScript
import assert from 'node:assert/strict'
|
|
import test from 'node:test'
|
|
import { createApplication } from '../src/application.js'
|
|
import { deposit, visitor } from './flow.js'
|
|
|
|
const makeQuote = (client) => client.post('orders/quote', { productId: 'dish', volumeMl: 250 }).quote
|
|
|
|
test('Недостаточный остаток и недопустимый объём', (t) => {
|
|
const app = createApplication()
|
|
t.after(app.close)
|
|
const client = visitor(app)
|
|
deposit(client, ['pet'])
|
|
assert.throws(() => client.post('orders', { quoteId: makeQuote(client).id }),
|
|
{ message: 'insufficientFunds', details: { missing: 45 } })
|
|
assert.throws(() => client.post('orders/quote', { productId: 'dish', volumeMl: 251 }), /noStock/)
|
|
assert.deepEqual(client.get('overview').wallet, { available: 30, held: 0 })
|
|
})
|
|
|
|
test('Резерв, обрыв связи, единственная команда и частичный розлив', (t) => {
|
|
const app = createApplication()
|
|
t.after(app.close)
|
|
const client = visitor(app)
|
|
deposit(client, ['aluminium', 'aluminium'])
|
|
const quote = makeQuote(client)
|
|
const { order } = client.post('orders', { quoteId: quote.id })
|
|
assert.equal(client.post('orders', { quoteId: quote.id }).order.id, order.id)
|
|
assert.deepEqual(client.get('overview').wallet, { available: 25, held: 75 })
|
|
const path = 'orders/' + order.id
|
|
assert.throws(() => client.post(path + '/dispense'), /invalidState/)
|
|
const confirmed = { confirmedContainerReady: true }
|
|
const { commandId } = client.post(path + '/dispense', confirmed).order
|
|
const mockEvent = (body) => client.post(path + '/mock-event', { commandId, ...body }).order
|
|
mockEvent({ eventId: 'event-lost', outcome: 'lost' })
|
|
assert.equal(client.get(path).order.status, 'reconciliation')
|
|
assert.equal(client.post(path + '/dispense', confirmed).order.commandId, commandId)
|
|
const measured = { eventId: 'event-measured', outcome: 'measured', actualMl: 100 }
|
|
assert.equal(mockEvent(measured).status, 'partial')
|
|
assert.equal(mockEvent(measured).charged, 30)
|
|
assert.deepEqual(client.get('overview').wallet, { available: 70, held: 0 })
|
|
})
|
|
|
|
test('При отсутствии связи или достаточного остатка розлив не запускается', (t) => {
|
|
const access = { operator: 'mock-operator-key-test-only' }
|
|
const app = createApplication(':memory:', { access })
|
|
t.after(app.close)
|
|
const client = visitor(app)
|
|
const changeMachine = (fields) => app.request('POST', '/v1/operator/machine', { machineId: 'garden', ...fields },
|
|
{ authorization: 'Bearer ' + access.operator })
|
|
deposit(client, ['aluminium', 'aluminium'])
|
|
changeMachine({ product: { id: 'dish', pricePerLitre: 300, remainingMl: 249 } })
|
|
assert.equal(client.get('products').products[0].volumes.length, 0)
|
|
assert.throws(() => makeQuote(client), /noStock/)
|
|
changeMachine({ product: { id: 'dish', pricePerLitre: 300, remainingMl: 250 } })
|
|
const { order } = client.post('orders', { quoteId: makeQuote(client).id })
|
|
changeMachine({ status: 'offline' })
|
|
const route = 'orders/' + order.id
|
|
assert.throws(() => client.post(route + '/dispense', { confirmedContainerReady: true }), /machineUnavailable/)
|
|
assert.equal(client.get(route).order.commandId, undefined)
|
|
assert.deepEqual(client.get('overview').wallet, { available: 25, held: 75 })
|
|
})
|