2024-04-15 14:26:34 +02:00
|
|
|
import * as fs from 'fs';
|
|
|
|
|
|
|
|
import { parseGPX } from '../src/io';
|
|
|
|
|
|
|
|
describe("Parsing tests", () => {
|
2024-04-16 09:54:41 +02:00
|
|
|
it("Simple", () => {
|
2024-04-15 14:26:34 +02:00
|
|
|
const path = "test-data/simple.gpx";
|
|
|
|
const data = fs.readFileSync(path, 'utf8');
|
|
|
|
const result = parseGPX(data);
|
|
|
|
|
|
|
|
expect(result.creator).toBe("https://gpx.studio");
|
|
|
|
expect(result.metadata.name).toBe("simple");
|
|
|
|
expect(result.metadata.author.name).toBe("gpx.studio");
|
|
|
|
expect(result.metadata.author.link.href).toBe("https://gpx.studio");
|
|
|
|
|
2024-04-16 11:48:42 +02:00
|
|
|
expect(result.trk.length).toBe(1);
|
2024-04-15 14:26:34 +02:00
|
|
|
|
2024-04-16 11:48:42 +02:00
|
|
|
const track = result.trk[0];
|
2024-04-15 14:26:34 +02:00
|
|
|
expect(track.name).toBe("simple");
|
|
|
|
expect(track.type).toBe("Cycling");
|
|
|
|
expect(track.trkseg.length).toBe(1);
|
|
|
|
|
|
|
|
const segment = track.trkseg[0];
|
|
|
|
expect(segment.trkpt.length).toBe(80);
|
|
|
|
|
|
|
|
for (let i = 0; i < segment.trkpt.length; i++) {
|
|
|
|
const point = segment.trkpt[i];
|
|
|
|
expect(point).toHaveProperty('lat');
|
|
|
|
expect(point).toHaveProperty('lon');
|
|
|
|
expect(point).toHaveProperty('ele');
|
|
|
|
}
|
2024-04-16 09:54:41 +02:00
|
|
|
|
|
|
|
expect(segment.trkpt[0].lat).toBe(50.790867);
|
|
|
|
expect(segment.trkpt[0].lon).toBe(4.404968);
|
|
|
|
expect(segment.trkpt[0].ele).toBe(109.0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Multiple tracks", () => {
|
|
|
|
const path = "test-data/with_tracks.gpx";
|
|
|
|
const data = fs.readFileSync(path, 'utf8');
|
|
|
|
const result = parseGPX(data);
|
|
|
|
|
2024-04-16 11:48:42 +02:00
|
|
|
expect(result.trk.length).toBe(2);
|
2024-04-16 09:54:41 +02:00
|
|
|
|
2024-04-16 11:48:42 +02:00
|
|
|
const track_1 = result.trk[0];
|
2024-04-16 09:54:41 +02:00
|
|
|
expect(track_1.name).toBe("track 1");
|
|
|
|
expect(track_1.trkseg.length).toBe(1);
|
|
|
|
expect(track_1.trkseg[0].trkpt.length).toBe(49);
|
|
|
|
|
2024-04-16 11:48:42 +02:00
|
|
|
const track_2 = result.trk[1];
|
2024-04-16 09:54:41 +02:00
|
|
|
expect(track_2.name).toBe("track 2");
|
|
|
|
expect(track_2.trkseg.length).toBe(1);
|
|
|
|
expect(track_2.trkseg[0].trkpt.length).toBe(28);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Multiple segments", () => {
|
|
|
|
const path = "test-data/with_segments.gpx";
|
|
|
|
const data = fs.readFileSync(path, 'utf8');
|
|
|
|
const result = parseGPX(data);
|
|
|
|
|
2024-04-16 11:48:42 +02:00
|
|
|
expect(result.trk.length).toBe(1);
|
2024-04-16 09:54:41 +02:00
|
|
|
|
2024-04-16 11:48:42 +02:00
|
|
|
const track = result.trk[0];
|
2024-04-16 09:54:41 +02:00
|
|
|
expect(track.trkseg.length).toBe(2);
|
|
|
|
expect(track.trkseg[0].trkpt.length).toBe(49);
|
|
|
|
expect(track.trkseg[1].trkpt.length).toBe(28);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Waypoint", () => {
|
|
|
|
const path = "test-data/with_waypoint.gpx";
|
|
|
|
const data = fs.readFileSync(path, 'utf8');
|
|
|
|
const result = parseGPX(data);
|
|
|
|
|
2024-04-16 11:48:42 +02:00
|
|
|
expect(result.wpt.length).toBe(1);
|
2024-04-16 09:54:41 +02:00
|
|
|
|
2024-04-16 11:48:42 +02:00
|
|
|
const waypoint = result.wpt[0];
|
2024-04-16 09:54:41 +02:00
|
|
|
expect(waypoint.lat).toBe(50.7836710064975);
|
|
|
|
expect(waypoint.lon).toBe(4.410764082658738);
|
|
|
|
expect(waypoint.ele).toBe(122.0);
|
|
|
|
expect(waypoint.name).toBe("Waypoint");
|
|
|
|
expect(waypoint.cmt).toBe("Comment");
|
|
|
|
expect(waypoint.desc).toBe("Description");
|
|
|
|
expect(waypoint.sym).toBe("Bike Trail");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Time", () => {
|
|
|
|
const path = "test-data/with_time.gpx";
|
|
|
|
const data = fs.readFileSync(path, 'utf8');
|
|
|
|
const result = parseGPX(data);
|
|
|
|
|
2024-04-16 11:48:42 +02:00
|
|
|
const track = result.trk[0];
|
2024-04-16 09:54:41 +02:00
|
|
|
const segment = track.trkseg[0];
|
|
|
|
|
|
|
|
for (let i = 0; i < segment.trkpt.length; i++) {
|
|
|
|
expect(segment.trkpt[i].time).toBeInstanceOf(Date);
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(segment.trkpt[0].time).toEqual(new Date("2023-12-31T23:00:00.000Z"));
|
|
|
|
expect(segment.trkpt[segment.trkpt.length - 1].time).toEqual(new Date("2023-12-31T23:06:40.567Z"));
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Heart rate", () => {
|
|
|
|
const path = "test-data/with_hr.gpx";
|
|
|
|
const data = fs.readFileSync(path, 'utf8');
|
|
|
|
const result = parseGPX(data);
|
|
|
|
|
2024-04-16 11:48:42 +02:00
|
|
|
const track = result.trk[0];
|
2024-04-16 09:54:41 +02:00
|
|
|
const segment = track.trkseg[0];
|
|
|
|
|
|
|
|
for (let i = 0; i < segment.trkpt.length; i++) {
|
|
|
|
expect(segment.trkpt[i]).toHaveProperty('extensions');
|
2024-04-16 11:48:42 +02:00
|
|
|
expect(segment.trkpt[i].extensions).toHaveProperty('TrackPointExtension');
|
|
|
|
expect(segment.trkpt[i].extensions.TrackPointExtension).toHaveProperty('hr');
|
2024-04-16 09:54:41 +02:00
|
|
|
}
|
|
|
|
|
2024-04-16 11:48:42 +02:00
|
|
|
expect(segment.trkpt[0].extensions.TrackPointExtension.hr).toBe(150);
|
|
|
|
expect(segment.trkpt[segment.trkpt.length - 1].extensions.TrackPointExtension.hr).toBe(160);
|
2024-04-16 09:54:41 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Cadence", () => {
|
|
|
|
const path = "test-data/with_cad.gpx";
|
|
|
|
const data = fs.readFileSync(path, 'utf8');
|
|
|
|
const result = parseGPX(data);
|
|
|
|
|
2024-04-16 11:48:42 +02:00
|
|
|
const track = result.trk[0];
|
2024-04-16 09:54:41 +02:00
|
|
|
const segment = track.trkseg[0];
|
|
|
|
|
|
|
|
for (let i = 0; i < segment.trkpt.length; i++) {
|
|
|
|
expect(segment.trkpt[i]).toHaveProperty('extensions');
|
2024-04-16 11:48:42 +02:00
|
|
|
expect(segment.trkpt[i].extensions).toHaveProperty('TrackPointExtension');
|
|
|
|
expect(segment.trkpt[i].extensions.TrackPointExtension).toHaveProperty('cad');
|
2024-04-16 09:54:41 +02:00
|
|
|
}
|
|
|
|
|
2024-04-16 11:48:42 +02:00
|
|
|
expect(segment.trkpt[0].extensions.TrackPointExtension.cad).toBe(80);
|
|
|
|
expect(segment.trkpt[segment.trkpt.length - 1].extensions.TrackPointExtension.cad).toBe(90);
|
2024-04-16 09:54:41 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Temperature", () => {
|
|
|
|
const path = "test-data/with_temp.gpx";
|
|
|
|
const data = fs.readFileSync(path, 'utf8');
|
|
|
|
const result = parseGPX(data);
|
|
|
|
|
2024-04-16 11:48:42 +02:00
|
|
|
const track = result.trk[0];
|
2024-04-16 09:54:41 +02:00
|
|
|
const segment = track.trkseg[0];
|
|
|
|
|
|
|
|
for (let i = 0; i < segment.trkpt.length; i++) {
|
|
|
|
expect(segment.trkpt[i]).toHaveProperty('extensions');
|
2024-04-16 11:48:42 +02:00
|
|
|
expect(segment.trkpt[i].extensions).toHaveProperty('TrackPointExtension');
|
|
|
|
expect(segment.trkpt[i].extensions.TrackPointExtension).toHaveProperty('atemp');
|
2024-04-16 09:54:41 +02:00
|
|
|
}
|
|
|
|
|
2024-04-16 11:48:42 +02:00
|
|
|
expect(segment.trkpt[0].extensions.TrackPointExtension.atemp).toBe(21);
|
|
|
|
expect(segment.trkpt[segment.trkpt.length - 1].extensions.TrackPointExtension.atemp).toBe(22);
|
2024-04-16 09:54:41 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Power 1", () => {
|
|
|
|
const path = "test-data/with_power_1.gpx";
|
|
|
|
const data = fs.readFileSync(path, 'utf8');
|
|
|
|
const result = parseGPX(data);
|
|
|
|
|
2024-04-16 11:48:42 +02:00
|
|
|
const track = result.trk[0];
|
2024-04-16 09:54:41 +02:00
|
|
|
const segment = track.trkseg[0];
|
|
|
|
|
|
|
|
for (let i = 0; i < segment.trkpt.length; i++) {
|
|
|
|
expect(segment.trkpt[i]).toHaveProperty('extensions');
|
2024-04-16 11:48:42 +02:00
|
|
|
expect(segment.trkpt[i].extensions).toHaveProperty('PowerExtension');
|
|
|
|
expect(segment.trkpt[i].extensions.PowerExtension).toHaveProperty('PowerInWatts');
|
2024-04-16 09:54:41 +02:00
|
|
|
}
|
|
|
|
|
2024-04-16 11:48:42 +02:00
|
|
|
expect(segment.trkpt[0].extensions.PowerExtension.PowerInWatts).toBe(200);
|
|
|
|
expect(segment.trkpt[segment.trkpt.length - 1].extensions.PowerExtension.PowerInWatts).toBe(210);
|
2024-04-16 09:54:41 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Power 2", () => {
|
|
|
|
const path = "test-data/with_power_2.gpx";
|
|
|
|
const data = fs.readFileSync(path, 'utf8');
|
|
|
|
const result = parseGPX(data);
|
|
|
|
|
2024-04-16 11:48:42 +02:00
|
|
|
const track = result.trk[0];
|
2024-04-16 09:54:41 +02:00
|
|
|
const segment = track.trkseg[0];
|
|
|
|
|
|
|
|
for (let i = 0; i < segment.trkpt.length; i++) {
|
|
|
|
expect(segment.trkpt[i]).toHaveProperty('extensions');
|
2024-04-16 11:48:42 +02:00
|
|
|
expect(segment.trkpt[i].extensions).toHaveProperty('PowerExtension');
|
|
|
|
expect(segment.trkpt[i].extensions.PowerExtension).toHaveProperty('PowerInWatts');
|
2024-04-16 09:54:41 +02:00
|
|
|
}
|
|
|
|
|
2024-04-16 11:48:42 +02:00
|
|
|
expect(segment.trkpt[0].extensions.PowerExtension.PowerInWatts).toBe(200);
|
|
|
|
expect(segment.trkpt[segment.trkpt.length - 1].extensions.PowerExtension.PowerInWatts).toBe(210);
|
2024-04-16 09:54:41 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Surface", () => {
|
|
|
|
const path = "test-data/with_surface.gpx";
|
|
|
|
const data = fs.readFileSync(path, 'utf8');
|
|
|
|
const result = parseGPX(data);
|
|
|
|
|
2024-04-16 11:48:42 +02:00
|
|
|
const track = result.trk[0];
|
2024-04-16 09:54:41 +02:00
|
|
|
const segment = track.trkseg[0];
|
|
|
|
|
|
|
|
for (let i = 0; i < segment.trkpt.length; i++) {
|
|
|
|
expect(segment.trkpt[i]).toHaveProperty('extensions');
|
2024-04-16 11:48:42 +02:00
|
|
|
expect(segment.trkpt[i].extensions).toHaveProperty('TrackPointExtension');
|
|
|
|
expect(segment.trkpt[i].extensions.TrackPointExtension).toHaveProperty('Extensions');
|
|
|
|
expect(segment.trkpt[i].extensions.TrackPointExtension.Extensions).toHaveProperty('surface');
|
2024-04-16 09:54:41 +02:00
|
|
|
}
|
|
|
|
|
2024-04-16 11:48:42 +02:00
|
|
|
expect(segment.trkpt[0].extensions.TrackPointExtension.Extensions.surface).toBe("asphalt");
|
|
|
|
expect(segment.trkpt[segment.trkpt.length - 1].extensions.TrackPointExtension.Extensions.surface).toBe("cobblestone");
|
2024-04-16 09:54:41 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Track style", () => {
|
|
|
|
const path = "test-data/with_style.gpx";
|
|
|
|
const data = fs.readFileSync(path, 'utf8');
|
|
|
|
const result = parseGPX(data);
|
|
|
|
|
2024-04-16 11:48:42 +02:00
|
|
|
const track = result.trk[0];
|
2024-04-16 09:54:41 +02:00
|
|
|
|
2024-04-16 11:48:42 +02:00
|
|
|
expect(track).toHaveProperty('extensions');
|
|
|
|
expect(track.extensions).toHaveProperty('line');
|
2024-04-16 09:54:41 +02:00
|
|
|
|
2024-04-16 11:48:42 +02:00
|
|
|
expect(track.extensions.line).toHaveProperty('color');
|
|
|
|
expect(track.extensions.line).toHaveProperty('opacity');
|
|
|
|
expect(track.extensions.line).toHaveProperty('weight');
|
2024-04-16 09:54:41 +02:00
|
|
|
|
2024-04-16 11:48:42 +02:00
|
|
|
expect(track.extensions.line.color).toBe("2d3ee9");
|
|
|
|
expect(track.extensions.line.opacity).toBe(0.5);
|
|
|
|
expect(track.extensions.line.weight).toBe(5);
|
2024-04-15 14:26:34 +02:00
|
|
|
});
|
|
|
|
});
|