import * as dotenv from 'dotenv'; import { beforeAll, describe, expect, test } from 'vitest'; import { z } from 'zod'; import { VietmapApi } from './vietmap-api'; import { VietmapPolyline, LatLng } from './helper/polyline_handle'; import { PlaceRequest, PlaceResponse, ReverseResponse, RouteRequest, RouteResponse, SearchRequest, SearchResponse, TSPRequest, } from './models'; import { Polyline } from './helper'; dotenv.config(); const envVariables = z .object({ VIETMAP_API_KEY: z.string(), }) .parse(process.env); describe('Vietmap Api Module', () => { let vietmapApi: VietmapApi; const util = require('util'); beforeAll(() => { vietmapApi = new VietmapApi({}); }); test('Nearest LatLng On Line', () => { const line = [ [106.70594000000001, 10.79631], [106.70664000000001, 10.79593], [106.70673000000001, 10.795890000000002], [106.70700000000001, 10.79579], [106.70727000000001, 10.795720000000001], [106.70743, 10.79568], [106.70758000000001, 10.79565], [106.70778000000001, 10.795570000000001], [106.70811, 10.795430000000001], [106.70828, 10.79536], [106.70841000000001, 10.795300000000001], [106.70929000000001, 10.794820000000001], [106.70932, 10.79479], [106.70985, 10.795340000000001], [106.70999, 10.795530000000001], [106.71006000000001, 10.79565], [106.71013, 10.795850000000002], [106.71019000000001, 10.79601], [106.71034, 10.7964], [106.71050000000001, 10.796830000000002], [106.71056000000002, 10.797], [106.71071, 10.79738], [106.71085000000001, 10.79779], [106.71089, 10.797930000000001], [106.71096000000001, 10.798190000000002], [106.71099000000001, 10.798300000000001], [106.71105000000001, 10.798620000000001], [106.71107, 10.79889], [106.7111, 10.799130000000002], [106.71112000000001, 10.799410000000002], [106.71113000000001, 10.79954], [106.71117000000001, 10.799930000000002], [106.71123000000001, 10.80044], [106.71126000000001, 10.800770000000002], [106.71132000000001, 10.80119], [106.71141000000001, 10.801210000000001], [106.71150000000002, 10.80128], [106.71154000000001, 10.801390000000001], [106.71154000000001, 10.80146], [106.71152000000001, 10.80152], [106.71148000000001, 10.801570000000002], [106.71143000000001, 10.80161], [106.71137, 10.801630000000001], [106.71145000000001, 10.802190000000001], [106.71147, 10.80249], [106.71148000000001, 10.802710000000001], [106.71147, 10.802800000000001], [106.71133, 10.80287], [106.71127000000001, 10.80288], [106.71089, 10.802890000000001], [106.71042000000001, 10.802930000000002], [106.71012, 10.802950000000001], [106.7094, 10.80302], [106.709, 10.80305], [106.70845000000001, 10.803090000000001], [106.70816, 10.80311], [106.70778000000001, 10.80314], [106.70763000000001, 10.80314], [106.70766, 10.802990000000001], [106.70769000000001, 10.80255], [106.70772000000001, 10.802010000000001], [106.70774000000002, 10.80169], [106.7077, 10.801430000000002], [106.70765000000002, 10.801430000000002], [106.70737000000001, 10.802000000000001], [106.70735, 10.80197], [106.70732000000001, 10.80207], [106.7073, 10.80212], [106.70707, 10.80213], [106.70703, 10.80208], [106.70700000000001, 10.802090000000002], [106.70688000000001, 10.802100000000001], [106.70685, 10.802200000000001], [106.70679000000001, 10.802190000000001], [106.70677, 10.80212], [106.70656000000001, 10.80211], [106.70653000000001, 10.802090000000002], [106.7065, 10.802100000000001], [106.70647000000001, 10.80213], [106.70641, 10.802140000000001], [106.70641, 10.80213], ]; const point = [106.70929000000001, 10.794820000000001]; const result = VietmapPolyline.nearestLatLngOnLine( line as LatLng[], point as LatLng, ); expect(result).toEqual({ distance: 0, index: 11, location: 0.37572645765831725, point: [106.70929000000001, 10.794820000000001], }); }); test('Split Route By LatLng', () => { const line = [ [106.70594000000001, 10.79631], [106.70664000000001, 10.79593], [106.70673000000001, 10.795890000000002], [106.70700000000001, 10.79579], [106.70727000000001, 10.795720000000001], [106.70743, 10.79568], [106.70758000000001, 10.79565], [106.70778000000001, 10.795570000000001], [106.70811, 10.795430000000001], [106.70828, 10.79536], [106.70841000000001, 10.795300000000001], [106.70929000000001, 10.794820000000001], [106.70932, 10.79479], [106.70985, 10.795340000000001], [106.70999, 10.795530000000001], [106.71006000000001, 10.79565], [106.71013, 10.795850000000002], [106.71019000000001, 10.79601], [106.71034, 10.7964], [106.71050000000001, 10.796830000000002], [106.71056000000002, 10.797], [106.71071, 10.79738], [106.71085000000001, 10.79779], [106.71089, 10.797930000000001], [106.71096000000001, 10.798190000000002], [106.71099000000001, 10.798300000000001], [106.71105000000001, 10.798620000000001], [106.71107, 10.79889], [106.7111, 10.799130000000002], [106.71112000000001, 10.799410000000002], [106.71113000000001, 10.79954], [106.71117000000001, 10.799930000000002], [106.71123000000001, 10.80044], [106.71126000000001, 10.800770000000002], [106.71132000000001, 10.80119], [106.71141000000001, 10.801210000000001], [106.71150000000002, 10.80128], [106.71154000000001, 10.801390000000001], [106.71154000000001, 10.80146], [106.71152000000001, 10.80152], [106.71148000000001, 10.801570000000002], [106.71143000000001, 10.80161], [106.71137, 10.801630000000001], [106.71145000000001, 10.802190000000001], [106.71147, 10.80249], [106.71148000000001, 10.802710000000001], [106.71147, 10.802800000000001], [106.71133, 10.80287], [106.71127000000001, 10.80288], [106.71089, 10.802890000000001], [106.71042000000001, 10.802930000000002], [106.71012, 10.802950000000001], [106.7094, 10.80302], [106.709, 10.80305], [106.70845000000001, 10.803090000000001], [106.70816, 10.80311], [106.70778000000001, 10.80314], [106.70763000000001, 10.80314], [106.70766, 10.802990000000001], [106.70769000000001, 10.80255], [106.70772000000001, 10.802010000000001], [106.70774000000002, 10.80169], [106.7077, 10.801430000000002], [106.70765000000002, 10.801430000000002], [106.70737000000001, 10.802000000000001], [106.70735, 10.80197], [106.70732000000001, 10.80207], [106.7073, 10.80212], [106.70707, 10.80213], [106.70703, 10.80208], [106.70700000000001, 10.802090000000002], [106.70688000000001, 10.802100000000001], [106.70685, 10.802200000000001], [106.70679000000001, 10.802190000000001], [106.70677, 10.80212], [106.70656000000001, 10.80211], [106.70653000000001, 10.802090000000002], [106.7065, 10.802100000000001], [106.70647000000001, 10.80213], [106.70641, 10.802140000000001], [106.70641, 10.80213], ]; const point = [106.70929000000001, 10.794820000000001]; const [line1, line2] = VietmapPolyline.splitRouteByLatLng( line as LatLng[], point as LatLng, ); expect(line1).toEqual([ [106.70594000000001, 10.79631], [106.70664000000001, 10.79593], [106.70673000000001, 10.795890000000002], [106.70700000000001, 10.79579], [106.70727000000001, 10.795720000000001], [106.70743, 10.79568], [106.70758000000001, 10.79565], [106.70778000000001, 10.795570000000001], [106.70811, 10.795430000000001], [106.70828, 10.79536], [106.70841000000001, 10.795300000000001], [106.70929000000001, 10.794820000000001], ]); expect(line2).toEqual([ [106.70929000000001, 10.794820000000001], [106.70929000000001, 10.794820000000001], [106.70932, 10.79479], [106.70985, 10.795340000000001], [106.70999, 10.795530000000001], [106.71006000000001, 10.79565], [106.71013, 10.795850000000002], [106.71019000000001, 10.79601], [106.71034, 10.7964], [106.71050000000001, 10.796830000000002], [106.71056000000002, 10.797], [106.71071, 10.79738], [106.71085000000001, 10.79779], [106.71089, 10.797930000000001], [106.71096000000001, 10.798190000000002], [106.71099000000001, 10.798300000000001], [106.71105000000001, 10.798620000000001], [106.71107, 10.79889], [106.7111, 10.799130000000002], [106.71112000000001, 10.799410000000002], [106.71113000000001, 10.79954], [106.71117000000001, 10.799930000000002], [106.71123000000001, 10.80044], [106.71126000000001, 10.800770000000002], [106.71132000000001, 10.80119], [106.71141000000001, 10.801210000000001], [106.71150000000002, 10.80128], [106.71154000000001, 10.801390000000001], [106.71154000000001, 10.80146], [106.71152000000001, 10.80152], [106.71148000000001, 10.801570000000002], [106.71143000000001, 10.80161], [106.71137, 10.801630000000001], [106.71145000000001, 10.802190000000001], [106.71147, 10.80249], [106.71148000000001, 10.802710000000001], [106.71147, 10.802800000000001], [106.71133, 10.80287], [106.71127000000001, 10.80288], [106.71089, 10.802890000000001], [106.71042000000001, 10.802930000000002], [106.71012, 10.802950000000001], [106.7094, 10.80302], [106.709, 10.80305], [106.70845000000001, 10.803090000000001], [106.70816, 10.80311], [106.70778000000001, 10.80314], [106.70763000000001, 10.80314], [106.70766, 10.802990000000001], [106.70769000000001, 10.80255], [106.70772000000001, 10.802010000000001], [106.70774000000002, 10.80169], [106.7077, 10.801430000000002], [106.70765000000002, 10.801430000000002], [106.70737000000001, 10.802000000000001], [106.70735, 10.80197], [106.70732000000001, 10.80207], [106.7073, 10.80212], [106.70707, 10.80213], [106.70703, 10.80208], [106.70700000000001, 10.802090000000002], [106.70688000000001, 10.802100000000001], [106.70685, 10.802200000000001], [106.70679000000001, 10.802190000000001], [106.70677, 10.80212], [106.70656000000001, 10.80211], [106.70653000000001, 10.802090000000002], [106.7065, 10.802100000000001], [106.70647000000001, 10.80213], [106.70641, 10.802140000000001], [106.70641, 10.80213], ]); }); test('Reverse api', async () => { const latitude = 35.71619; const longitude = 51.36247; const apikey = envVariables.VIETMAP_API_KEY; const res = await vietmapApi.reverse({ latitude, longitude, apikey, }); expect(res).toBeInstanceOf(ReverseResponse); }); test('Search api', async () => { const res = await vietmapApi.search( new SearchRequest({ text: 'Vietmap', apikey: envVariables.VIETMAP_API_KEY, }), ); expect(res[0]).toBeInstanceOf(SearchResponse); }); test('Route api', async () => { const res = await vietmapApi.route( [ [10.79628438955497, 106.70592293472612], [10.801891047584164, 106.70660958023404], ], new RouteRequest({ vehicle: 'car', apikey: envVariables.VIETMAP_API_KEY, points_encoded: true, optimize: true, }), ); expect(res).toBeInstanceOf(RouteResponse); }); test('Autocomplete Search api', async () => { const res = await vietmapApi.autoCompleteSearch( new SearchRequest({ text: 'Vietmap', apikey: envVariables.VIETMAP_API_KEY, focus: [10, 106], }), ); expect(res[0]).toBeInstanceOf(SearchResponse); }); test('Place api', async () => { const apikey = envVariables.VIETMAP_API_KEY; const res = await vietmapApi.place( new PlaceRequest({ refId: 'vm:ADDRESS:8D92EB120DDE9996', apikey, }), ); expect(res).toBeInstanceOf(PlaceResponse); }); test('TSP api', async () => { const res = await vietmapApi.tsp( [ [10.79628438955497, 106.70592293472612], [10.801891047584164, 106.70660958023404], ], new TSPRequest({ vehicle: 'car', apikey: envVariables.VIETMAP_API_KEY, points_encoded: true, optimize: true, round_trip: true, }), ); expect(res).toBeInstanceOf(RouteResponse); }); });