|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | +import { Point } from '../../../src/geometry' |
| 3 | + |
| 4 | +const manhattanMock = vi.hoisted(() => vi.fn()) |
| 5 | + |
| 6 | +vi.mock('../../../src/registry/router/manhattan/index', () => ({ |
| 7 | + manhattan: manhattanMock, |
| 8 | +})) |
| 9 | + |
| 10 | +import { metro } from '../../../src/registry/router/metro' |
| 11 | + |
| 12 | +describe('metro router', () => { |
| 13 | + beforeEach(() => { |
| 14 | + manhattanMock.mockReset() |
| 15 | + }) |
| 16 | + |
| 17 | + it('should call manhattan with metro defaults', () => { |
| 18 | + manhattanMock.mockReturnValue([]) |
| 19 | + |
| 20 | + metro.call({} as any, [], {}, {} as any) |
| 21 | + |
| 22 | + expect(manhattanMock).toHaveBeenCalledTimes(1) |
| 23 | + const [, options] = manhattanMock.mock.calls[0] |
| 24 | + expect(options.maxDirectionChange).toBe(45) |
| 25 | + expect(typeof options.fallbackRoute).toBe('function') |
| 26 | + expect(typeof options.directions).toBe('function') |
| 27 | + }) |
| 28 | + |
| 29 | + it('should allow overriding default options', () => { |
| 30 | + manhattanMock.mockReturnValue([]) |
| 31 | + |
| 32 | + metro.call({} as any, [], { maxDirectionChange: 90 }, {} as any) |
| 33 | + |
| 34 | + const [, options] = manhattanMock.mock.calls[0] |
| 35 | + expect(options.maxDirectionChange).toBe(90) |
| 36 | + }) |
| 37 | + |
| 38 | + it('should generate 8-direction metro directions with diagonal costs', () => { |
| 39 | + manhattanMock.mockReturnValue([]) |
| 40 | + |
| 41 | + metro.call({} as any, [], { step: 10, cost: 1 } as any, {} as any) |
| 42 | + |
| 43 | + const [, options] = manhattanMock.mock.calls[0] |
| 44 | + const dirs = options.directions.call(options) |
| 45 | + |
| 46 | + expect(dirs).toHaveLength(8) |
| 47 | + expect(dirs[0]).toMatchObject({ cost: 1, offsetX: 10, offsetY: 0 }) |
| 48 | + expect(dirs[1]).toMatchObject({ cost: 15, offsetX: 10, offsetY: 10 }) |
| 49 | + expect(dirs[2]).toMatchObject({ cost: 1, offsetX: 0, offsetY: 10 }) |
| 50 | + expect(dirs[3]).toMatchObject({ cost: 15, offsetX: -10, offsetY: 10 }) |
| 51 | + expect(dirs[4]).toMatchObject({ cost: 1, offsetX: -10, offsetY: 0 }) |
| 52 | + expect(dirs[5]).toMatchObject({ cost: 15, offsetX: -10, offsetY: -10 }) |
| 53 | + expect(dirs[6]).toMatchObject({ cost: 1, offsetX: 0, offsetY: -10 }) |
| 54 | + expect(dirs[7]).toMatchObject({ cost: 15, offsetX: 10, offsetY: -10 }) |
| 55 | + }) |
| 56 | + |
| 57 | + it('fallbackRoute should not include from/to points and should set previousDirectionAngle', () => { |
| 58 | + manhattanMock.mockReturnValue([]) |
| 59 | + |
| 60 | + metro.call({} as any, [], {}, {} as any) |
| 61 | + |
| 62 | + const [, options] = manhattanMock.mock.calls[0] |
| 63 | + const fallbackRoute = options.fallbackRoute as any |
| 64 | + |
| 65 | + const resolvedOptions: any = { |
| 66 | + directions: Array.from({ length: 8 }, () => ({})), |
| 67 | + previousDirectionAngle: null, |
| 68 | + } |
| 69 | + |
| 70 | + const from = new Point(0, 0) |
| 71 | + const to = new Point(10, 0) |
| 72 | + const route: Point[] = fallbackRoute(from, to, resolvedOptions) |
| 73 | + |
| 74 | + expect(route.some((p) => p.equals(from))).toBe(false) |
| 75 | + expect(route.some((p) => p.equals(to))).toBe(false) |
| 76 | + expect(typeof resolvedOptions.previousDirectionAngle).toBe('number') |
| 77 | + expect(resolvedOptions.previousDirectionAngle % 45).toBe(0) |
| 78 | + }) |
| 79 | + |
| 80 | + it('fallbackRoute should return an intermediate point when lines intersect', () => { |
| 81 | + manhattanMock.mockReturnValue([]) |
| 82 | + |
| 83 | + metro.call({} as any, [], {}, {} as any) |
| 84 | + |
| 85 | + const [, options] = manhattanMock.mock.calls[0] |
| 86 | + const fallbackRoute = options.fallbackRoute as any |
| 87 | + |
| 88 | + const resolvedOptions: any = { |
| 89 | + directions: Array.from({ length: 8 }, () => ({})), |
| 90 | + previousDirectionAngle: null, |
| 91 | + } |
| 92 | + |
| 93 | + const from = new Point(0, 0) |
| 94 | + const to = new Point(100, 80) |
| 95 | + const route: Point[] = fallbackRoute(from, to, resolvedOptions) |
| 96 | + |
| 97 | + expect(route.length).toBeLessThanOrEqual(1) |
| 98 | + if (route.length === 1) { |
| 99 | + expect(route[0].equals(from)).toBe(false) |
| 100 | + expect(route[0].equals(to)).toBe(false) |
| 101 | + } |
| 102 | + expect(typeof resolvedOptions.previousDirectionAngle).toBe('number') |
| 103 | + expect(resolvedOptions.previousDirectionAngle % 45).toBe(0) |
| 104 | + }) |
| 105 | +}) |
0 commit comments