Skip to content

Commit

Permalink
fix: Handle Restrictedlogs for request.vehicle.currentRouteSegment (#196
Browse files Browse the repository at this point in the history
)

* fix: handle vehicle attribute level restricted logs

* test: add test case for request.vehicle.currentRouteSegment
  • Loading branch information
regeter authored Dec 11, 2024
1 parent f563822 commit 156f32a
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 11 deletions.
4 changes: 4 additions & 0 deletions src/localStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ export function ensureCorrectFormat(data) {
if (restrictedLog[section][attr] !== undefined) {
log.jsonPayload[section][attr] = restrictedLog[section][attr];
}
if (restrictedLog[section].vehicle?.[attr] !== undefined) {
log.jsonPayload[section].vehicle[attr] =
restrictedLog[section].vehicle[attr];
}
});
}
});
Expand Down
88 changes: 77 additions & 11 deletions src/localStorage.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// localStorage.test.js
import fs from "fs";
import { parseJsonContent, removeEmptyObjects, ensureCorrectFormat } from "./localStorage";
import {
parseJsonContent,
removeEmptyObjects,
ensureCorrectFormat,
} from "./localStorage";

// Helper function to load test data
function loadTestData(filename) {
Expand Down Expand Up @@ -37,10 +41,10 @@ test("parseJsonContent throws error for invalid JSON", () => {
test("removeEmptyObjects removes empty nested objects", () => {
const input = {
a: {},
b: { c: {}, d: { e: 1 } }
b: { c: {}, d: { e: 1 } },
};
const expected = {
b: { d: { e: 1 } }
b: { d: { e: 1 } },
};
expect(removeEmptyObjects(input)).toStrictEqual(expected);
});
Expand All @@ -63,21 +67,83 @@ test("ensureCorrectFormat merges restricted attributes", () => {
jsonPayload: {
"@type": "type.googleapis.com/Normal",
request: {},
response: {}
}
response: {},
},
};

const restrictedLog = {
jsonPayload: {
"@type": "type.googleapis.com/Restricted",
parentInsertId: "parent",
request: {
waypoints: ["point1", "point2"]
}
}
waypoints: ["point1", "point2"],
},
},
};

const result = ensureCorrectFormat([parentLog, restrictedLog]);
expect(result.rawLogs[0].jsonPayload.request.waypoints).toStrictEqual([
"point1",
"point2",
]);
expect(result.rawLogs.length).toBe(1);
});

test("ensureCorrectFormat merges non top level vehicle restricted attributes", () => {
const parentLog = {
insertId: "parent",
jsonPayload: {
"@type": "type.googleapis.com/maps.fleetengine.v1.UpdateVehicleLog",
request: {
vehicle: {
name: "test-vehicle",
},
},
response: {},
},
};

const restrictedLog = {
jsonPayload: {
"@type":
"type.googleapis.com/maps.fleetengine.v1.UpdateVehicleRestrictedLog",
parentInsertId: "parent",
request: {
vehicle: {
currentRouteSegment: "testSegment123",
},
},
response: {
currentRouteSegment: "responseSegment456",
waypoints: [
{
encodedPathToWaypoint: "encodedPath789",
},
],
},
},
};

const result = ensureCorrectFormat([parentLog, restrictedLog]);
expect(result.rawLogs[0].jsonPayload.request.waypoints).toStrictEqual(["point1", "point2"]);

// Verify only one log remains (restricted log is filtered out)
expect(result.rawLogs.length).toBe(1);

// Verify vehicle attributes are merged correctly
expect(
result.rawLogs[0].jsonPayload.request.vehicle.currentRouteSegment
).toBe("testSegment123");

// Verify direct response attributes are merged
expect(result.rawLogs[0].jsonPayload.response.currentRouteSegment).toBe(
"responseSegment456"
);
expect(
result.rawLogs[0].jsonPayload.response.waypoints[0].encodedPathToWaypoint
).toBe("encodedPath789");

// Verify original vehicle attributes are preserved
expect(result.rawLogs[0].jsonPayload.request.vehicle.name).toBe(
"test-vehicle"
);
});

0 comments on commit 156f32a

Please sign in to comment.