Skip to content

Commit

Permalink
Merge pull request #1085 from the-bay-kay/diaryServices-rewrite
Browse files Browse the repository at this point in the history
✏️📖 Diary Services Rewrite
  • Loading branch information
shankari authored Jan 25, 2024
2 parents 3cbb5f0 + f54257b commit ee32008
Show file tree
Hide file tree
Showing 24 changed files with 1,215 additions and 609 deletions.
2 changes: 2 additions & 0 deletions package.cordovabuild.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
"@react-navigation/native": "^6.1.7",
"@react-navigation/stack": "^6.3.17",
"@shopify/flash-list": "^1.3.1",
"@types/leaflet": "1.9.4",
"angular": "1.6.7",
"angular-animate": "1.6.7",
"angular-local-storage": "^0.7.1",
Expand Down Expand Up @@ -145,6 +146,7 @@
"enketo-transformer": "^4.0.0",
"fast-xml-parser": "^4.2.2",
"fs-extra": "^9.0.1",
"humanize-duration": "3.31.0",
"i18next": "^22.5.0",
"install": "^0.13.0",
"ionic-datepicker": "1.2.1",
Expand Down
2 changes: 2 additions & 0 deletions package.serve.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"@react-navigation/stack": "^6.3.17",
"@shopify/flash-list": "^1.3.1",
"@types/jest": "^29.5.5",
"@types/leaflet": "1.9.4",
"angular": "1.6.7",
"angular-animate": "1.6.7",
"angular-local-storage": "^0.7.1",
Expand All @@ -76,6 +77,7 @@
"enketo-transformer": "^4.0.0",
"fast-xml-parser": "^4.2.2",
"fs-extra": "^9.0.1",
"humanize-duration": "3.31.0",
"i18next": "^22.5.0",
"install": "^0.13.0",
"ionic-datepicker": "1.2.1",
Expand Down
27 changes: 23 additions & 4 deletions www/__mocks__/cordovaMocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ export const mockFile = () => {
//for consent document
const _storage = {};

type MessageData = any;
type Message = { key: string; data: MessageData; metadata: { write_ts: number; [k: string]: any } };
export const mockBEMUserCache = (config?) => {
const _cache = {};
const messages = [];
const messages: Message[] = [];
const mockBEMUserCache = {
getLocalStorage: (key: string, isSecure: boolean) => {
return new Promise((rs, rj) =>
Expand Down Expand Up @@ -98,18 +100,35 @@ export const mockBEMUserCache = (config?) => {
putMessage: (key: string, value: any) => {
return new Promise<void>((rs, rj) =>
setTimeout(() => {
messages.push({ key, value });
messages.push({
key,
data: value,
// write_ts is epoch time in seconds
metadata: { write_ts: Math.floor(Date.now() / 1000) },
});
rs();
}, 100),
);
},
getAllMessages: (key: string, withMetadata?: boolean) => {
return new Promise<any[]>((rs, rj) =>
return new Promise<Message[] | MessageData[]>((rs, rj) =>
setTimeout(() => {
rs(messages.filter((m) => m.key == key).map((m) => m.value));
rs(messages.filter((m) => m.key == key).map((m) => (withMetadata ? m : m.data)));
}, 100),
);
},
getMessagesForInterval: (key: string, tq, withMetadata?: boolean) => {
return new Promise<Message[] | MessageData[]>((rs, rj) =>
setTimeout(() => {
rs(
messages
.filter((m) => m.key == key)
.filter((m) => m.metadata[tq.key] >= tq.startTs && m.metadata.write_ts <= tq.endTs)
.map((m) => (withMetadata ? m : m.data)),
);
}, 100),
);
}, // Used for getUnifiedDataForInterval
putRWDocument: (key: string, value: any) => {
if (key == 'config/app_ui_config') {
return new Promise<void>((rs, rj) =>
Expand Down
256 changes: 256 additions & 0 deletions www/__mocks__/timelineHelperMocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
import { MetaData, BEMData, ServerResponse } from '../js/types/serverData';
import {
CompositeTrip,
ConfirmedPlace,
FilteredLocation,
TripTransition,
UnprocessedTrip,
} from '../js/types/diaryTypes';
import { LabelOptions } from '../js/types/labelTypes';
import { AppConfig } from '../js/types/appConfigTypes';

const mockMetaData: MetaData = {
write_ts: 1,
key: 'test/value/one',
platform: 'test',
time_zone: 'America/Los_Angeles',
write_fmt_time: '1969-07-16T07:01:49.000Z',
write_local_dt: null,
origin_key: '1',
};

export const mockLabelOptions: LabelOptions = {
MODE: null,
PURPOSE: null,
REPLACED_MODE: null,
translations: null,
};

const mockConfirmedPlaceData: ConfirmedPlace = {
source: 'DwellSegmentationTimeFilter',
key: null,
origin_key: null,
location: {
type: 'Point',
coordinates: [-122.0876886, 37.3887767],
},
cleaned_place: null,
additions: [],
user_input: {},
enter_fmt_time: '2015-07-22T08:14:53.881000-07:00',
exit_fmt_time: '2015-07-22T08:14:53.881000-07:00',
starting_trip: null,
ending_trip: null,
enter_local_dt: null,
exit_local_dt: null,
raw_places: [null, null],
enter_ts: 1437578093.881,
exit_ts: 1437578093.881,
};
// using parse/stringify to deep copy & populate data
let tempMetaData = JSON.parse(JSON.stringify(mockMetaData));
tempMetaData.write_ts = 2;
tempMetaData.origin_key = '2';
export const mockMetaDataTwo = tempMetaData;

export const mockUnprocessedTrip: UnprocessedTrip = {
_id: { $oid: 'mockUnprocessedTrip' },
additions: [],
confidence_threshold: 0.0,
distance: 1.0,
duration: 3.0,
end_fmt_time: '',
end_loc: { type: 'Point', coordinates: [] },
end_local_dt: null,
expectation: null,
inferred_labels: [],
key: 'mockUnprocessedTrip',
locations: [],
origin_key: '',
source: '',
start_local_dt: null,
start_ts: 0.1,
start_loc: { type: 'Point', coordinates: [] },
starting_trip: null,
user_input: null,
};

export const mockCompData: ServerResponse<CompositeTrip> = {
phone_data: [
{
data: {
_id: { $oid: 'mockDataOne' },
additions: [],
cleaned_section_summary: null,
cleaned_trip: null,
confidence_threshold: -1,
confirmed_trip: null,
distance: 777,
duration: 777,
end_confirmed_place: {
data: mockConfirmedPlaceData,
metadata: mockMetaData,
_id: { $oid: 'endConfirmedPlace' },
},
end_fmt_time: '2023-11-01T17:55:20.999397-07:00',
end_loc: {
type: 'Point',
coordinates: [-1, -1],
},
end_local_dt: null,
end_place: null,
end_ts: -1,
expectation: null,
expected_trip: null,
inferred_labels: [],
inferred_section_summary: {
count: {
CAR: 1,
WALKING: 1,
},
distance: {
CAR: 222,
WALKING: 222,
},
duration: {
CAR: 333,
WALKING: 333,
},
},
inferred_trip: null,
key: '12345',
locations: [
{
metadata: mockMetaData,
data: null,
},
],
origin_key: '',
raw_trip: null,
sections: [
{
metadata: mockMetaData,
data: null,
},
],
source: 'DwellSegmentationDistFilter',
start_confirmed_place: {
data: mockConfirmedPlaceData,
metadata: mockMetaData,
_id: { $oid: 'startConfirmedPlace' },
},
start_fmt_time: '2023-11-01T17:55:20.999397-07:00',
start_loc: {
type: 'Point',
coordinates: [-1, -1],
},
start_local_dt: null,
start_place: null,
start_ts: 1,
user_input: null,
},
metadata: mockMetaData,
},
],
};
// Setup for second mockData
let newPhoneData = JSON.parse(JSON.stringify(mockCompData.phone_data[0]));
newPhoneData.data._id.$oid = 'mockDataTwo';
newPhoneData.metadata = mockMetaDataTwo;
newPhoneData.data.start_confirmed_place.metadata = mockMetaDataTwo;
newPhoneData.data.start_confirmed_place._id.$oid = 'startConfirmedPlaceTwo';
newPhoneData.data.end_confirmed_place.metadata = mockMetaDataTwo;
newPhoneData.data.end_confirmed_place._id.$oid = 'endConfirmedPlaceTwo';
export const mockCompDataTwo = {
phone_data: [mockCompData.phone_data[0], newPhoneData],
};

export const mockTransitions: Array<BEMData<TripTransition>> = [
{
data: {
// mock of a startTransition
currstate: '',
transition: 'T_EXITED_GEOFENCE',
ts: 1,
},
metadata: mockMetaData,
},
{
data: {
// mock of an endTransition
currstate: '',
transition: 'T_TRIP_ENDED',
ts: 9999,
},
metadata: mockMetaData,
},
];

const mockFilterLocation: FilteredLocation = {
accuracy: 0.1,
altitude: 100,
elapsedRealtimeNanos: 10000,
filter: 'time',
fmt_time: '',
heading: 1.0,
latitude: 1.0,
loc: null,
local_dt: null,
longitude: -1.0,
sensed_speed: 0,
ts: 100,
};
let mockFilterLocationTwo = JSON.parse(JSON.stringify(mockFilterLocation));
mockFilterLocationTwo.ts = 900;
mockFilterLocationTwo.longitude = 200;
mockFilterLocationTwo.longitude = -200;

export const mockFilterLocations: Array<BEMData<FilteredLocation>> = [
{
data: mockFilterLocation,
metadata: mockMetaData,
},
{
data: mockFilterLocationTwo,
metadata: mockMetaDataTwo,
},
];

export const mockConfigModeOfStudy: AppConfig = {
server: null,
survey_info: {
'trip-labels': 'MULTILABEL',
surveys: null,
},
intro: {
mode_studied: 'sample_study',
},
};
export const mockConfigNoModeOfStudy: AppConfig = {
server: null,
survey_info: {
'trip-labels': 'MULTILABEL',
surveys: null,
},
intro: {},
};
export const mockConfigEnketo: AppConfig = {
server: null,
survey_info: {
'trip-labels': 'ENKETO',
surveys: {
TripConfirmSurvey: {
compatibleWith: 1.2,
formPath: null,
labelTemplate: null,
version: null,
},
},
},
};

// Used by jest.mocks() to return a various mocked objects.
export const fakeStartTsOne = -14576291;
export const fakeEndTsOne = -13885091;
export const fakeStartTsTwo = 1092844665;
export const fakeEndTsTwo = 1277049465;
7 changes: 5 additions & 2 deletions www/__tests__/diaryHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import {
modeColors,
} from '../js/diary/diaryHelper';

import initializedI18next from '../js/i18nextInit';
window['i18next'] = initializedI18next;

it('returns a formatted date', () => {
expect(getFormattedDate('2023-09-18T00:00:00-07:00')).toBe('Mon September 18, 2023');
expect(getFormattedDate('2023-09-18T00:00:00-07:00')).toBe('Mon, September 18, 2023');
expect(getFormattedDate('')).toBeUndefined();
expect(getFormattedDate('2023-09-18T00:00:00-07:00', '2023-09-21T00:00:00-07:00')).toBe(
'Mon September 18, 2023 - Thu September 21, 2023',
'Mon, September 18, 2023 - Thu, September 21, 2023',
);
});

Expand Down
6 changes: 3 additions & 3 deletions www/__tests__/enketoHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ it('loads the previous response to a given survey', () => {
*/
it('filters the survey responses by their name and version', () => {
//no response -> no filtered responses
expect(filterByNameAndVersion('TimeUseSurvey', [])).resolves.toStrictEqual([]);
expect(filterByNameAndVersion('TimeUseSurvey', [], fakeConfig)).toStrictEqual([]);

const response = [
{
Expand All @@ -294,7 +294,7 @@ it('filters the survey responses by their name and version', () => {
];

//one response -> that response
expect(filterByNameAndVersion('TimeUseSurvey', response)).resolves.toStrictEqual(response);
expect(filterByNameAndVersion('TimeUseSurvey', response, fakeConfig)).toStrictEqual(response);

const responses = [
{
Expand Down Expand Up @@ -336,5 +336,5 @@ it('filters the survey responses by their name and version', () => {
];

//several responses -> only the one that has a name match
expect(filterByNameAndVersion('TimeUseSurvey', responses)).resolves.toStrictEqual(response);
expect(filterByNameAndVersion('TimeUseSurvey', responses, fakeConfig)).toStrictEqual(response);
});
Loading

0 comments on commit ee32008

Please sign in to comment.