From 3f3fbd81d08b8a16e523d46ab251fe01bb5dfeb1 Mon Sep 17 00:00:00 2001 From: Zaid Abdulla Date: Fri, 26 Jul 2019 08:38:41 -0700 Subject: [PATCH] Fix regression in device.unpack --- lib/device.ts | 8 ++++-- lib/session.ts | 2 +- test/device.ts | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 4 deletions(-) diff --git a/lib/device.ts b/lib/device.ts index e14ab4f..2c76bc6 100644 --- a/lib/device.ts +++ b/lib/device.ts @@ -140,7 +140,7 @@ export function unpack( if (deviceData.attributes.has(p, revision)) allMatches.push(p); for (let i = path.length - 1; i >= 0; --i) { - if (path.alias & (i << i)) { + if (path.alias & (1 << i)) { for (const [param, val] of path.segments[i] as [Path, string][]) { const p = wildcardPath.slice(0, i + 1).concat(param); const unpacked = unpack(deviceData, p, revision); @@ -156,11 +156,13 @@ export function unpack( ) { for (let m = 0; m < allMatches.length; ++m) { let k; + const match = allMatches[m]; + if (!match) continue; for (k = i; k >= 0; --k) - if (allMatches[m].segments[k] !== up.segments[k]) break; + if (match.segments[k] !== up.segments[k]) break; if (k < 0) { - filtered.push(allMatches[m]); + filtered.push(match); allMatches[m] = null; } } diff --git a/lib/session.ts b/lib/session.ts index 4f5dba4..7317598 100644 --- a/lib/session.ts +++ b/lib/session.ts @@ -2139,7 +2139,7 @@ function processInstances( defer: boolean ): void { parent = sessionContext.deviceData.paths.add(parent); - let instancesToCreate, instancesToDelete; + let instancesToCreate: InstanceSet, instancesToDelete: Set; if (parent.segments[0] === "Downloads") { if (parent.length !== 1) return; instancesToDelete = sessionContext.syncState.downloadsToDelete; diff --git a/test/device.ts b/test/device.ts index 59e603c..bc98488 100644 --- a/test/device.ts +++ b/test/device.ts @@ -1,6 +1,9 @@ import ava from "ava"; import Path from "../lib/common/path"; import * as device from "../lib/device"; +import PathSet from "../lib/common/path-set"; +import VersionedMap from "../lib/versioned-map"; +import { Attributes } from "../lib/types"; ava("getAliasDeclarations", t => { const path = Path.parse("a.[aa:10,bb.[aaa:100].cc:1].b"); @@ -18,3 +21,77 @@ ava("getAliasDeclarations", t => { t.is(d.defer, true); } }); + +ava("unpack", t => { + const now = Date.now(); + const deviceData = { + paths: new PathSet(), + timestamps: new VersionedMap(), + attributes: new VersionedMap(), + trackers: new Map(), + changes: new Set() + }; + + device.set(deviceData, Path.parse("a.1.b"), now, { + value: [now, ["b", "xsd:string"]] + }); + device.set(deviceData, Path.parse("a.1.c"), now, { + value: [now, ["c", "xsd:string"]] + }); + device.set(deviceData, Path.parse("a.1.a.1.a"), now, { + value: [now, ["", "xsd:string"]] + }); + device.set(deviceData, Path.parse("a.1.a.1.b"), now, { + value: [now, ["b1", "xsd:string"]] + }); + device.set(deviceData, Path.parse("a.1.a.1.c"), now, { + value: [now, ["c1", "xsd:string"]] + }); + device.set(deviceData, Path.parse("a.1.a.2.a"), now, { + value: [now, ["", "xsd:string"]] + }); + device.set(deviceData, Path.parse("a.1.a.2.b"), now, { + value: [now, ["b2", "xsd:string"]] + }); + device.set(deviceData, Path.parse("a.1.a.2.c"), now, { + value: [now, ["c2", "xsd:string"]] + }); + + device.set(deviceData, Path.parse("a.2.b"), now, { + value: [now, ["b", "xsd:string"]] + }); + device.set(deviceData, Path.parse("a.2.c"), now, { + value: [now, ["c", "xsd:string"]] + }); + device.set(deviceData, Path.parse("a.2.a.1.a"), now, { + value: [now, ["", "xsd:string"]] + }); + device.set(deviceData, Path.parse("a.2.a.1.b"), now, { + value: [now, ["b1", "xsd:string"]] + }); + device.set(deviceData, Path.parse("a.2.a.1.c"), now, { + value: [now, ["c1", "xsd:string"]] + }); + device.set(deviceData, Path.parse("a.2.a.2.a"), now, { + value: [now, ["", "xsd:string"]] + }); + device.set(deviceData, Path.parse("a.2.a.2.b"), now, { + value: [now, ["c1", "xsd:string"]] + }); + device.set(deviceData, Path.parse("a.2.a.2.c"), now, { + value: [now, ["b1", "xsd:string"]] + }); + + let unpacked: Path[]; + unpacked = device.unpack( + deviceData, + Path.parse("a.[b:b,c:c].a.[b:b1,c:c1].a") + ); + t.is(unpacked.length, 2); + t.is(unpacked[0].toString(), "a.1.a.1.a"); + t.is(unpacked[1].toString(), "a.2.a.1.a"); + + unpacked = device.unpack(deviceData, Path.parse("a.*.a.[b:c1,c:b1].a")); + t.is(unpacked.length, 1); + t.is(unpacked[0].toString(), "a.2.a.2.a"); +});