Skip to content

Commit

Permalink
Add beacon unit tests; move unit tests into src
Browse files Browse the repository at this point in the history
  • Loading branch information
steinbro committed Nov 23, 2024
1 parent 02041d6 commit ebcbf9d
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .mocharc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"extension": [
"ts"
],
"spec": "test/test.ts",
"spec": "src/**/*.test.ts",
"timeout": 5000,
"node-option": [
"experimental-specifier-resolution=node",
Expand Down
46 changes: 23 additions & 23 deletions package-lock.json

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

17 changes: 10 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,30 @@
"@capacitor/assets": "^3.0.5",
"@capacitor/cli": "latest",
"@types/chai": "^5.0.1",
"@types/mocha": "^10.0.9",
"@types/mocha": "^10.0.10",
"@vite-pwa/assets-generator": "^0.2.4",
"@vitejs/plugin-vue": "^5.0.5",
"@vue/cli-plugin-typescript": "^5.0.8",
"axios": "^1.7.2",
"chai": "^5.1.1",
"chai": "^5.1.2",
"cypress": "^13.15.2",
"esm": "^3.2.25",
"express": "^4.19.2",
"mocha": "^10.6.0",
"mocha": "^10.8.2",
"morgan": "^1.10.0",
"start-server-and-test": "^2.0.4",
"ts-node": "^10.9.2",
"typescript": "^5.6.3",
"typescript": "^5.7.2",
"vite": "^5.3.3",
"vite-plugin-checker": "^0.8.0",
"vite-plugin-pwa": "^0.20.0",
"vite-tsconfig-paths": "^5.1.2",
"vite-tsconfig-paths": "^5.1.3",
"workbox-window": "^7.1.0"
},
"author": "",
"author": "Daniel W. Steinbrook",
"license": "ISC",
"type": "module"
"type": "module",
"imports": {
"#vendor/*": "./src/vendor/*"
}
}
4 changes: 2 additions & 2 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
<router-view v-if="!isWelcomeScreenVisible"></router-view>
</template>

<script setup>
<script setup lang="ts">
import WelcomeScreen from './components/WelcomeScreen.vue';
import { ref } from 'vue';
const isWelcomeScreenVisible = ref(true);
const isWelcomeScreenVisible = ref<boolean>(true);
const handleInitialized = () => {
isWelcomeScreenVisible.value = false;
};
Expand Down
2 changes: 1 addition & 1 deletion test/test.ts → src/composables/tile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect } from "chai";
import {
createBoundingBox,
latLonToTileCoords,
} from "../src/composables/tile";
} from "./tile";

describe("createBoundingBox", () => {
it("should create a half-kilometer bounding box around a point", () => {
Expand Down
4 changes: 3 additions & 1 deletion src/state/audio.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/// <reference path="../vendor/declarations.d.ts" />

// Copyright (c) Daniel W. Steinbrook.
// with many thanks to ChatGPT

import { Feature, Point } from 'geojson';
import { ref } from 'vue';
import { SpeechSynthesisVoice, TextToSpeech } from "@capacitor-community/text-to-speech";
import unmute from "@vendor/unmute.js";
import unmute from "#vendor/unmute.js";
import { normalizedRelativePositionTo, distanceTo } from '../state/location';

export var audioContext: AudioContext;
Expand Down
24 changes: 24 additions & 0 deletions src/state/beacon.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { expect } from "chai";
import { beacon, isOnCourse } from "./beacon";
import { myLocation } from '../state/location';

describe("beacon", () => {
it("should calculate on/off course", () => {
beacon.set({
name: "Washington Monument",
latitude: 38.889444,
longitude: -77.035278,
});
beacon.enabled = true;

// US Capitol (due east of monument)
myLocation.setLocation(38.889722, -77.008889);
// Facing west
myLocation.setHeading(-90.0);
expect(isOnCourse.value).to.be.true;

// Facing north
myLocation.setHeading(0.0);
expect(isOnCourse.value).to.be.false;
});
});
6 changes: 3 additions & 3 deletions src/state/beacon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,13 @@ const relativePosition = computed(() => {

// Set the beacon sound effect spatial position.
watch(relativePosition, (newValue, oldVAlue) => {
if (beacon.enabled && newValue !== undefined) {
if (beacon.enabled && beacon.audio && newValue) {
beacon.getAudio().panner.setCoordinates(newValue.x, newValue.y);
}
});

// True if we are roughly facing the beacon, +/- onCourseAngle
const isOnCourse = computed(() => {
export const isOnCourse = computed(() => {
if (beacon.enabled && relativePosition.value) {
const angle = Math.atan2(
relativePosition.value.x,
Expand Down Expand Up @@ -205,7 +205,7 @@ const looper: Looper = {

// Announce the beacon distance periodically
watch(distanceMeters, (newValue, oldValue) => {
if (!beacon.enabled || typeof newValue !== "number") {
if (!beacon.enabled || !beacon.audio || typeof newValue !== "number") {
return;
} else if(newValue < foundProximityMeters) {
// Stop the beacon when we're within the threshold.
Expand Down
4 changes: 2 additions & 2 deletions src/vendor/declarations.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
declare module "@vendor/unmute.js" {
export default function unmute(
declare module "#vendor/unmute.js" {
export default function unmute(
audioContext: AudioContext,
allowBackgroundPlayback: boolean,
forceIOSBehavior: boolean
Expand Down
7 changes: 7 additions & 0 deletions src/vendor/packages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "vendor",
"type": "module",
"exports": {
"./unmute.js": "./unmute.js"
}
}
2 changes: 1 addition & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default defineConfig({
// Use relative paths for assets, since our Github Pages deploy is not at the root
resolve: {
alias: {
'@vendor': 'src/vendor'
'#vendor': 'src/vendor'
}
},
assetsInclude: ['**/*.wav'],
Expand Down

0 comments on commit ebcbf9d

Please sign in to comment.