|
| 1 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest' |
| 2 | +import { startServer } from './e2e.test-utils.js' |
| 3 | +import type { E2EServer } from './e2e.test-utils.js' |
| 4 | + |
| 5 | +/** |
| 6 | + * The plugin as a registered v2 Track API provider, inside a real server. |
| 7 | + * |
| 8 | + * This is what the unit suite cannot show: that the server offered |
| 9 | + * `registerTrackApiProvider` to a plugin loaded from a packed tarball, that the |
| 10 | + * plugin took it, and that a query arriving over HTTP — parsed and validated by |
| 11 | + * the server, not by a test stub — reaches this provider and comes back as |
| 12 | + * GeoJSON. |
| 13 | + * |
| 14 | + * Needs a server checkout carrying SignalK/signalk-server#2995 at |
| 15 | + * SIGNALK_SERVER_DIR. Run with `npm run test:e2e`. |
| 16 | + */ |
| 17 | + |
| 18 | +const CTX = 'vessels.urn:mrn:imo:mmsi:244170002' |
| 19 | +const MINUTE = 60_000 |
| 20 | + |
| 21 | +interface Feature { |
| 22 | + type: string |
| 23 | + geometry: { type: string; coordinates: [number, number][][] } | null |
| 24 | + properties: { |
| 25 | + context: string |
| 26 | + isSelf: boolean |
| 27 | + providerId?: string |
| 28 | + from: string |
| 29 | + to: string |
| 30 | + bbox?: [number, number, number, number] |
| 31 | + pointCount: number |
| 32 | + resolution?: string |
| 33 | + coordTimes?: string[][] |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +interface Collection { |
| 38 | + type: string |
| 39 | + features: Feature[] |
| 40 | +} |
| 41 | + |
| 42 | +let server: E2EServer |
| 43 | +let t0: number |
| 44 | + |
| 45 | +beforeAll(async () => { |
| 46 | + server = await startServer({ config: { segmentGapMinutes: 5 } }) |
| 47 | + t0 = Date.now() - 10 * MINUTE |
| 48 | + await server.feed(CTX, [60.1, 24.9], t0) |
| 49 | + await server.feed(CTX, [60.11, 24.91], t0 + MINUTE) |
| 50 | + await server.feed(CTX, [60.12, 24.92], t0 + 2 * MINUTE) |
| 51 | +}, 180_000) |
| 52 | + |
| 53 | +afterAll(() => { |
| 54 | + server?.stop() |
| 55 | +}) |
| 56 | + |
| 57 | +describe('the plugin registers as a track provider', () => { |
| 58 | + // Without a registration the server answers 501 "No track api provider |
| 59 | + // configured", which is what this server does with the plugin absent. |
| 60 | + it('answers a v2 query rather than reporting no provider', async () => { |
| 61 | + const { status, body } = await server.apiV2(`/tracks?contexts=${CTX}`) |
| 62 | + |
| 63 | + expect(status).toBe(200) |
| 64 | + const collection = body as Collection |
| 65 | + expect(collection.type).toBe('FeatureCollection') |
| 66 | + expect(collection.features).toHaveLength(1) |
| 67 | + }) |
| 68 | + |
| 69 | + // The server stamps which provider answered, so a fan-out response can be |
| 70 | + // attributed. It is added by the server, not by this plugin. |
| 71 | + it('is attributed to this plugin', async () => { |
| 72 | + const { body } = await server.apiV2(`/tracks?contexts=${CTX}`) |
| 73 | + const [feature] = (body as Collection).features |
| 74 | + |
| 75 | + expect(feature!.properties.providerId).toBe('tracks') |
| 76 | + }) |
| 77 | +}) |
| 78 | + |
| 79 | +describe('a v2 query through the real HTTP route', () => { |
| 80 | + it('returns GeoJSON in lng,lat order', async () => { |
| 81 | + const { body } = await server.apiV2(`/tracks?contexts=${CTX}`) |
| 82 | + const [feature] = (body as Collection).features |
| 83 | + |
| 84 | + expect(feature!.type).toBe('Feature') |
| 85 | + expect(feature!.geometry!.type).toBe('MultiLineString') |
| 86 | + // Longitude first, and roughly where the positions were fed. |
| 87 | + const [first] = feature!.geometry!.coordinates[0]! |
| 88 | + expect(first![0]).toBeCloseTo(24.9, 1) |
| 89 | + expect(first![1]).toBeCloseTo(60.1, 1) |
| 90 | + expect(feature!.properties.context).toBe(CTX) |
| 91 | + expect(feature!.properties.isSelf).toBe(false) |
| 92 | + expect(feature!.properties.pointCount).toBeGreaterThanOrEqual(2) |
| 93 | + }) |
| 94 | + |
| 95 | + it('serves coordTimes when ?times is asked for', async () => { |
| 96 | + const { body } = await server.apiV2(`/tracks?contexts=${CTX}×`) |
| 97 | + const [feature] = (body as Collection).features |
| 98 | + |
| 99 | + const segments = feature!.geometry!.coordinates |
| 100 | + expect(feature!.properties.coordTimes).toHaveLength(segments.length) |
| 101 | + expect(feature!.properties.coordTimes![0]).toHaveLength(segments[0]!.length) |
| 102 | + expect(Date.parse(feature!.properties.coordTimes![0]![0]!)).not.toBeNaN() |
| 103 | + }) |
| 104 | + |
| 105 | + it('omits the geometry for ?geometry=false, keeping the metadata', async () => { |
| 106 | + const { body } = await server.apiV2(`/tracks?contexts=${CTX}&geometry=false`) |
| 107 | + const [feature] = (body as Collection).features |
| 108 | + |
| 109 | + expect(feature!.geometry).toBeNull() |
| 110 | + expect(feature!.properties.pointCount).toBeGreaterThanOrEqual(2) |
| 111 | + }) |
| 112 | + |
| 113 | + // The server parses bbox as west,south,east,north and hands the provider the |
| 114 | + // same order; a swap anywhere along that path shows up here. |
| 115 | + it('filters by bbox in GeoJSON order', async () => { |
| 116 | + const inside = await server.apiV2(`/tracks?contexts=${CTX}&bbox=24,59,26,61`) |
| 117 | + expect((inside.body as Collection).features).toHaveLength(1) |
| 118 | + |
| 119 | + const elsewhere = await server.apiV2(`/tracks?contexts=${CTX}&bbox=130,-35,139,-33`) |
| 120 | + expect((elsewhere.body as Collection).features).toHaveLength(0) |
| 121 | + }) |
| 122 | + |
| 123 | + // duration is resolved into from/to by the server, so this exercises the |
| 124 | + // server's parsing and the provider's window handling together. |
| 125 | + it('honours a duration window', async () => { |
| 126 | + const wide = await server.apiV2(`/tracks?contexts=${CTX}&duration=PT30M`) |
| 127 | + expect((wide.body as Collection).features).toHaveLength(1) |
| 128 | + |
| 129 | + // The track ends ~8 minutes ago, so a one-minute window excludes it. |
| 130 | + const narrow = await server.apiV2(`/tracks?contexts=${CTX}&duration=PT1M`) |
| 131 | + expect((narrow.body as Collection).features).toHaveLength(0) |
| 132 | + }) |
| 133 | + |
| 134 | + // The server validates `resolution` as any positive ISO 8601 duration, so a |
| 135 | + // calendar unit passes validation and reaches the provider. Resolving one |
| 136 | + // needs a reference point; without it this came back as a 500. |
| 137 | + it('answers a calendar-unit resolution rather than erroring', async () => { |
| 138 | + for (const unit of ['P1W', 'P1M', 'P1Y']) { |
| 139 | + const { status, body } = await server.apiV2(`/tracks?contexts=${CTX}&resolution=${unit}`) |
| 140 | + |
| 141 | + expect(status).toBe(200) |
| 142 | + expect((body as Collection).features).toHaveLength(1) |
| 143 | + } |
| 144 | + }) |
| 145 | + |
| 146 | + it('rejects a malformed query before reaching the provider', async () => { |
| 147 | + const { status } = await server.apiV2(`/tracks?contexts=${CTX}&bbox=1,2,3`) |
| 148 | + expect(status).toBe(400) |
| 149 | + }) |
| 150 | +}) |
0 commit comments