scriptable-testlab
Version:
A lightweight, efficient tool designed to manage and update scripts for Scriptable.
176 lines • 5.91 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
var location_exports = {};
__export(location_exports, {
MockLocation: () => MockLocation
});
module.exports = __toCommonJS(location_exports);
var import_scriptable_abstract = require("scriptable-abstract");
const ACCURACY_METERS = {
best: 5,
tenMeters: 10,
hundredMeters: 100,
kilometer: 1e3,
threeKilometers: 3e3
};
const DEFAULT_STATE = {
currentLocation: {
latitude: 0,
longitude: 0,
altitude: 0,
horizontalAccuracy: ACCURACY_METERS.best,
verticalAccuracy: ACCURACY_METERS.best
},
accuracy: "best",
simulatedDelay: 100
};
const MOCK_LOCATIONS = {
"zh-CN": {
name: "\u6A21\u62DF\u4F4D\u7F6E",
locality: "\u6A21\u62DF\u57CE\u5E02",
subLocality: "\u6A21\u62DF\u533A",
thoroughfare: "\u6A21\u62DF\u8DEF",
subThoroughfare: "123\u53F7",
administrativeArea: "\u6A21\u62DF\u7701",
subAdministrativeArea: "\u6A21\u62DF\u53BF",
country: "\u4E2D\u56FD",
isoCountryCode: "CN"
},
"en-US": {
name: "Mock Location",
locality: "Mock City",
subLocality: "Mock District",
thoroughfare: "Mock Street",
subThoroughfare: "123",
administrativeArea: "Mock State",
subAdministrativeArea: "Mock County",
country: "United States",
isoCountryCode: "US"
},
"ja-JP": {
name: "\u30E2\u30C3\u30AF\u4F4D\u7F6E",
locality: "\u30E2\u30C3\u30AF\u5E02",
subLocality: "\u30E2\u30C3\u30AF\u533A",
thoroughfare: "\u30E2\u30C3\u30AF\u901A\u308A",
subThoroughfare: "123",
administrativeArea: "\u30E2\u30C3\u30AF\u770C",
subAdministrativeArea: "\u30E2\u30C3\u30AF\u90E1",
country: "\u65E5\u672C",
isoCountryCode: "JP"
}
};
const createMockGeocodeSummary = (latitude, longitude, locale = "en-US") => {
const localeData = MOCK_LOCATIONS[locale] || MOCK_LOCATIONS["en-US"];
return {
name: `${localeData.name} (${latitude.toFixed(6)}, ${longitude.toFixed(6)})`,
locality: localeData.locality,
subLocality: localeData.subLocality,
thoroughfare: localeData.thoroughfare,
subThoroughfare: localeData.subThoroughfare,
administrativeArea: localeData.administrativeArea,
subAdministrativeArea: localeData.subAdministrativeArea,
postalCode: "123456",
country: localeData.country,
isoCountryCode: localeData.isoCountryCode,
timeZone: "UTC",
ocean: null,
inlandWater: null,
areasOfInterest: [`${localeData.name} POI`],
location: {
latitude,
longitude,
altitude: 0
},
postalAddress: {
street: `${localeData.thoroughfare} ${localeData.subThoroughfare}`,
city: localeData.locality,
state: localeData.administrativeArea,
country: localeData.country,
postalCode: "123456",
isoCountryCode: localeData.isoCountryCode,
subAdministrativeArea: localeData.subAdministrativeArea,
subLocality: localeData.subLocality
}
};
};
class MockLocation extends import_scriptable_abstract.AbsLocation {
static get instance() {
return super.instance;
}
constructor() {
super(DEFAULT_STATE);
}
addLocationNoise(value, accuracyInMeters) {
const maxOffset = accuracyInMeters / 111e3;
return value + (Math.random() - 0.5) * maxOffset;
}
async simulateDelay() {
if (this.state.simulatedDelay) {
await new Promise((resolve) => setTimeout(resolve, this.state.simulatedDelay));
}
}
async current() {
await this.simulateDelay();
const accuracyInMeters = ACCURACY_METERS[this.state.accuracy];
const base = this.state.currentLocation;
return {
latitude: this.addLocationNoise(base.latitude, accuracyInMeters),
longitude: this.addLocationNoise(base.longitude, accuracyInMeters),
altitude: this.addLocationNoise(base.altitude, accuracyInMeters),
horizontalAccuracy: accuracyInMeters,
verticalAccuracy: accuracyInMeters
};
}
setAccuracyToBest() {
this.setState({ accuracy: "best" });
}
setAccuracyToTenMeters() {
this.setState({ accuracy: "tenMeters" });
}
setAccuracyToHundredMeters() {
this.setState({ accuracy: "hundredMeters" });
}
setAccuracyToKilometer() {
this.setState({ accuracy: "kilometer" });
}
setAccuracyToThreeKilometers() {
this.setState({ accuracy: "threeKilometers" });
}
async reverseGeocode(latitude, longitude, locale) {
await this.simulateDelay();
return [createMockGeocodeSummary(latitude, longitude, locale)];
}
// Testing helpers
setCurrentLocation(location) {
this.setState({ currentLocation: location });
}
setSimulatedDelay(delay) {
this.setState({ simulatedDelay: delay });
}
disableSimulatedDelay() {
this.setState({ simulatedDelay: 0 });
}
}
__publicField(MockLocation, "_instance");
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
MockLocation
});
//# sourceMappingURL=location.js.map