Skip to content

Commit

Permalink
Add cache fetch testing
Browse files Browse the repository at this point in the history
  • Loading branch information
steinbro committed Nov 27, 2024
1 parent 514db99 commit 84c85f7
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 120 deletions.
52 changes: 43 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
"@capacitor/core": "latest",
"@capacitor/splash-screen": "latest",
"@turf/turf": "^7.1.0",
"@types/isomorphic-fetch": "^0.0.39",
"@types/leaflet": "^1.9.14",
"isomorphic-fetch": "^3.0.0",
"leaflet": "^1.9.4",
"vue": "^3.4.31",
"vue-router": "^4.4.5"
Expand All @@ -45,6 +47,7 @@
"fake-indexeddb": "^6.0.0",
"mocha": "^10.8.2",
"morgan": "^1.10.0",
"nock": "^13.5.6",
"start-server-and-test": "^2.0.4",
"ts-node": "^10.9.2",
"typescript": "^5.7.2",
Expand Down
102 changes: 52 additions & 50 deletions src/composables/tile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,59 @@ import {
enumerateTilesAround,
} from "./tile";

describe("createBoundingBox", () => {
it("should create a half-kilometer bounding box around a point", () => {
const latitude = 40.7128;
const longitude = -74.006;
const radiusMeters = 500;

const bbox = createBoundingBox(latitude, longitude, radiusMeters);

expect(bbox[0]).to.be.lessThan(bbox[2]);
expect(bbox[1]).to.be.lessThan(bbox[3]);
});
});

describe("latLonToTileCoords", () => {
it("should return the correct tile coordinates for latitude 0 and longitude 0 at zoom level 0", () => {
const result = latLonToTileCoords(0, 0, 0);
expect(result).to.deep.equal({ x: 0, y: 0, z: 0 });
});

it("should return the correct tile coordinates for latitude 0 and longitude 0 at higher zoom level", () => {
const result = latLonToTileCoords(0, 0, 10);
expect(result).to.deep.equal({ x: 512, y: 512, z: 10 });
});

it("should return the correct tile coordinates for negative latitude and positive longitude", () => {
const result = latLonToTileCoords(-45, 90, 5);
expect(result).to.deep.equal({ x: 24, y: 20, z: 5 });
});

it("should return the correct tile coordinates for latitude 0 and positive longitude", () => {
const result = latLonToTileCoords(0, 90, 3);
expect(result).to.deep.equal({ x: 6, y: 4, z: 3 });
describe('Tile', () => {
describe("createBoundingBox", () => {
it("should create a half-kilometer bounding box around a point", () => {
const latitude = 40.7128;
const longitude = -74.006;
const radiusMeters = 500;

const bbox = createBoundingBox(latitude, longitude, radiusMeters);

expect(bbox[0]).to.be.lessThan(bbox[2]);
expect(bbox[1]).to.be.lessThan(bbox[3]);
});
});
});

describe("enumerateTilesAround", () => {
it('should return tiles around location', () => {
// 10m radius around Washington Monument
const result = enumerateTilesAround(38.889444, -77.035278, 10)
expect(result.length).to.equal(1);
expect(result[0]).to.include({
x: 18744,
y: 25072,
z: 16,
key: '16/18744/25072',

describe("latLonToTileCoords", () => {
it("should return the correct tile coordinates for latitude 0 and longitude 0 at zoom level 0", () => {
const result = latLonToTileCoords(0, 0, 0);
expect(result).to.deep.equal({ x: 0, y: 0, z: 0 });
});

it("should return the correct tile coordinates for latitude 0 and longitude 0 at higher zoom level", () => {
const result = latLonToTileCoords(0, 0, 10);
expect(result).to.deep.equal({ x: 512, y: 512, z: 10 });
});

it("should return the correct tile coordinates for negative latitude and positive longitude", () => {
const result = latLonToTileCoords(-45, 90, 5);
expect(result).to.deep.equal({ x: 24, y: 20, z: 5 });
});

it("should return the correct tile coordinates for latitude 0 and positive longitude", () => {
const result = latLonToTileCoords(0, 90, 3);
expect(result).to.deep.equal({ x: 6, y: 4, z: 3 });
});
});

it('should return more tiles for a bigger radius', () => {
// 1km radius around Washington Monument
const result = enumerateTilesAround(38.889444, -77.035278, 1000)
expect(result.length).to.equal(25);

describe("enumerateTilesAround", () => {
it('should return tiles around location', () => {
// 10m radius around Washington Monument
const result = enumerateTilesAround(38.889444, -77.035278, 10)
expect(result.length).to.equal(1);
expect(result[0]).to.include({
x: 18744,
y: 25072,
z: 16,
key: '16/18744/25072',
});
});

it('should return more tiles for a bigger radius', () => {
// 1km radius around Washington Monument
const result = enumerateTilesAround(38.889444, -77.035278, 1000)
expect(result.length).to.equal(25);
});
});
})
});
78 changes: 57 additions & 21 deletions src/state/beacon.test.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,72 @@
import { expect } from "chai";
import { beacon, isNearby, isOnCourse } from "./beacon";
import { beacon, isNearby, isOnCourse, relativePosition } from "./beacon";
import { myLocation } from '../state/location';

describe("beacon", () => {
it("should calculate on/off course", () => {
describe("Beacon", () => {
beforeEach(() => {
// Set beacon to Washington monument
beacon.set("Washington Monument", 38.889444, -77.035278);
beacon.enable();

// US Capitol (due east of monument)
// Set our location to US Capitol (due east of monument)
myLocation.setLocation(38.889722, -77.008889);
// Various heading roughly facing west
[-90.0, -95.0, -85.0].forEach(heading => {
myLocation.setHeading(heading);
expect(isOnCourse.value).to.be.true;
});

describe('relativePosition', () => {
[-90.0, -179.0, -1.0].forEach(heading => {
it(`should be in front at heading ${heading}`, () => {
// Beacon is in front of us
myLocation.setHeading(heading);
expect(relativePosition.value!.y).to.be.greaterThan(0);
});
});

[90.0, 179.0, 1.0].forEach(heading => {
it(`should be behind at heading ${heading}`, () => {
myLocation.setHeading(heading);
expect(relativePosition.value!.y).to.be.lessThan(0);
});
});

[-89.0, 0.0, 89.0].forEach(heading => {
it(`should be to the left at heading ${heading}`, () => {
myLocation.setHeading(heading);
expect(relativePosition.value!.x).to.be.lessThan(0);
});
});

[-91.0, 180.0, 91.0].forEach(heading => {
it(`should be to the right at heading ${heading}`, () => {
myLocation.setHeading(heading);
expect(relativePosition.value!.x).to.be.greaterThan(0);
});
});
});

// Various headings facing any other direction
describe('isOnCourse', () => {
[-90.0, -95.0, -85.0].forEach(heading => {
it(`should be on course at heading ${heading}`, () => {
myLocation.setHeading(heading);
expect(isOnCourse.value).to.be.true;
});
});

[0.0, 90.0, 180.0, -130.0, -50.0].forEach(heading => {
myLocation.setHeading(heading);
expect(isOnCourse.value).to.be.false;
it(`should be off course at heading ${heading}`, () => {
myLocation.setHeading(heading);
expect(isOnCourse.value).to.be.false;
});
});
});

it("should recognize nearby threshold", () => {
beacon.set("Washington Monument", 38.889444, -77.035278);
beacon.enable();

// US Capitol (due east of monument)
myLocation.setLocation(38.889722, -77.008889);
expect(isNearby.value).to.be.false;

myLocation.setLocation(beacon.latitude!, beacon.longitude!);
expect(isNearby.value).to.be.true;
describe('isNearby', () => {
it("should be false when far away", () => {
expect(isNearby.value).to.be.false;
});

it("should be true when nearby", () => {
myLocation.setLocation(beacon.latitude!, beacon.longitude!);
expect(isNearby.value).to.be.true;
});
});
});
Loading

0 comments on commit 84c85f7

Please sign in to comment.