| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- import assert from 'node:assert/strict'
- import { readFile } from 'node:fs/promises'
- import ts from 'typescript'
- // Isolate the provider contract without accessing a browser, a key or live orders.
- const calls = []
- const policies = []
- let response
- let now = 100000
- const requestedAt = []
- Date.now = () => now
- globalThis.window = {
- setTimeout: (callback, ms) => {
- if (ms < 10000) {
- now += ms
- return setTimeout(callback, 0)
- }
- return setTimeout(callback, ms)
- },
- AMap: {
- plugin: (_plugins, done) => done(),
- Driving: class {
- constructor(options) {
- policies.push(options.policy)
- }
- search(start, end, options, done) {
- calls.push([start, ...options.waypoints, end])
- requestedAt.push(now)
- response(done)
- }
- }
- }
- }
- const source = await readFile(new URL('../src/utils/waterAmap.ts', import.meta.url), 'utf8')
- const { outputText } = ts.transpileModule(source, {
- compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.ESNext }
- })
- const { planWaterDriving, collectWaterRoadMatrix } = await import(
- `data:text/javascript;base64,${Buffer.from(outputText).toString('base64')}`
- )
- const success = (done) =>
- done('complete', {
- routes: [
- {
- distance: 1000,
- time: 60,
- steps: [
- {
- path: [
- [113, 22],
- [114, 23]
- ]
- }
- ]
- }
- ]
- })
- response = success
- const stops = Array.from({ length: 37 }, (_, i) => [113 + i / 1000, 22])
- const result = await planWaterDriving(stops, () => true)
- assert.equal(calls.length, 3)
- assert.ok(calls.every((chunk) => chunk.length <= 18))
- assert.deepEqual([calls[0][0], ...calls.flatMap((chunk) => chunk.slice(1))], stops)
- assert.equal(result.distance, 3000)
- assert.equal(result.time, 180)
- calls.length = 0
- await planWaterDriving([stops[0], stops[0], stops[1], stops[1]], () => true)
- assert.deepEqual(calls, [[stops[0], stops[1]]])
- calls.length = 0
- assert.equal((await planWaterDriving([stops[0], stops[0]], () => true)).distance, 0)
- assert.equal(calls.length, 0)
- let active = true
- response = (done) => {
- active = false
- success(done)
- }
- assert.equal(await planWaterDriving(stops, () => active), undefined)
- assert.equal(calls.length, 1)
- calls.length = 0
- assert.equal(await planWaterDriving(stops, () => false), undefined)
- assert.equal(calls.length, 0)
- response = (done) => (calls.length === 1 ? success(done) : done('error', 'OVER_QUOTA'))
- await assert.rejects(
- planWaterDriving(stops, () => true),
- /OVER_QUOTA/
- )
- assert.equal(calls.length, 2)
- response = (done) => done('complete', { routes: [{ distance: 1, time: 1, steps: [] }] })
- await assert.rejects(
- planWaterDriving(stops.slice(0, 2), () => true),
- /未返回可用/
- )
- console.log(
- 'PASS: ordered chunking, totals, duplicate stops, same location, stale requests, partial failure, invalid paths'
- )
- calls.length = 0
- policies.length = 0
- response = success
- let progress
- const matrix = await collectWaterRoadMatrix(
- stops.slice(0, 3),
- 'shortest',
- () => true,
- (done, total) => {
- progress = [done, total]
- }
- )
- assert.deepEqual(progress, [6, 6])
- assert.equal(calls.length, 4)
- assert.ok(policies.every((value) => value === 2))
- assert.deepEqual(calls[2], [stops[1], stops[2]])
- assert.deepEqual(calls[3], [stops[2], stops[1]])
- assert.equal(matrix[1][0], null)
- assert.deepEqual(matrix[1][1], { distance: 0, time: 0 })
- calls.length = 0
- await collectWaterRoadMatrix(
- stops.slice(0, 3),
- 'shortest',
- () => true,
- () => {}
- )
- assert.equal(calls.length, 0, 'repeat directed matrix uses fresh shared cost cache')
- now += 60001
- await collectWaterRoadMatrix(
- stops.slice(0, 3),
- 'shortest',
- () => true,
- () => {}
- )
- assert.equal(calls.length, 4, 'expired road costs are refreshed')
- response = (done) => done('error', 'NO_DATA')
- const unreachable = await collectWaterRoadMatrix(
- stops.slice(0, 2),
- 'fastest',
- () => true,
- () => {}
- )
- assert.equal(unreachable[0][1], null)
- response = (done) => done('error', 'OVER_QUOTA')
- await assert.rejects(
- collectWaterRoadMatrix(
- stops.slice(0, 2),
- 'deadline',
- () => true,
- () => {}
- ),
- /OVER_QUOTA/
- )
- console.log(
- 'PASS: directed road matrix, shortest policy, progress, same-coordinate cells, unreachable roads and provider errors'
- )
- requestedAt.length = 0
- let attempts = 0
- response = (done) =>
- ++attempts < 3 ? done('error', 'CUQPS_HAS_EXCEEDED_THE_LIMIT') : success(done)
- await planWaterDriving(stops.slice(0, 2), () => true)
- assert.equal(attempts, 3)
- assert.ok(requestedAt[1] - requestedAt[0] >= 1500)
- assert.ok(requestedAt[2] - requestedAt[1] >= 3000)
- attempts = 0
- response = (done) => {
- attempts++
- done('error', 'CUQPS_HAS_EXCEEDED_THE_LIMIT')
- }
- await assert.rejects(
- planWaterDriving(stops.slice(0, 2), () => true),
- /请求过于频繁/
- )
- assert.equal(attempts, 3, 'QPS retry count is bounded')
- requestedAt.length = 0
- response = success
- await Promise.all([
- planWaterDriving(stops.slice(0, 2), () => true),
- planWaterDriving(stops.slice(1, 3), () => true)
- ])
- assert.ok(requestedAt[1] - requestedAt[0] >= 1100, 'concurrent planners share pacing')
- let current = true
- const cancelled = planWaterDriving(stops.slice(0, 2), () => current)
- current = false
- assert.equal(await cancelled, undefined, 'queued stale request never reaches provider')
- console.log('PASS: shared pacing, TTL cache, QPS backoff, bounded retries and queued cancellation')
|