Skip to content

Commit 7f64bfd

Browse files
authored
added unit test for setting correct paths for array changes (#1426)
* added unit test for setting correct paths for array changes * added unit test for root value rewriting for testing against regression
1 parent ea1730a commit 7f64bfd

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

packages/runner/test/data-updating.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,44 @@ describe("data-updating", () => {
305305
expect(changes[0].value).toBe(2);
306306
});
307307

308+
it("should generate correct paths when setting array length to 0", () => {
309+
const testCell = runtime.getCell<{ items: number[] }>(
310+
space,
311+
"normalizeAndDiff array length to zero",
312+
undefined,
313+
tx,
314+
);
315+
// Create array with 100 items
316+
const largeArray = Array.from({ length: 100 }, (_, i) => i);
317+
testCell.set({ items: largeArray });
318+
319+
// Now set length to 0 through the length property
320+
const lengthLink = testCell.key("items").key("length")
321+
.getAsNormalizedFullLink();
322+
const changes = normalizeAndDiff(runtime, tx, lengthLink, 0);
323+
324+
// Should have 101 changes total
325+
expect(changes.length).toBe(101);
326+
327+
// Find the length change
328+
const lengthChange = changes.find((c) =>
329+
c.location.path[c.location.path.length - 1] === "length"
330+
);
331+
expect(lengthChange).toBeDefined();
332+
expect(lengthChange!.value).toBe(0);
333+
334+
// Verify all elements are marked undefined with correct paths
335+
const elementChanges = changes.filter((c) =>
336+
c.location.path[c.location.path.length - 1] !== "length"
337+
);
338+
expect(elementChanges.length).toBe(100);
339+
340+
elementChanges.forEach((change, i) => {
341+
expect(change.location.path).toEqual(["items", i.toString()]);
342+
expect(change.value).toBe(undefined);
343+
});
344+
});
345+
308346
it("should handle array element changes", () => {
309347
const testCell = runtime.getCell<{ items: number[] }>(
310348
space,

packages/runner/test/storage-transaction-shim.test.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,94 @@ describe("URI Utils", () => {
560560
});
561561
});
562562

563+
describe("root value rewriting", () => {
564+
let storageManager: ReturnType<typeof StorageManager.emulate>;
565+
let runtime: Runtime;
566+
567+
beforeEach(() => {
568+
storageManager = StorageManager.emulate({ as: signer });
569+
runtime = new Runtime({
570+
storageManager,
571+
blobbyServerUrl: "http://localhost:8080",
572+
});
573+
});
574+
575+
afterEach(async () => {
576+
await runtime?.dispose();
577+
await storageManager?.close();
578+
});
579+
580+
it("should rewrite root writes with value property", () => {
581+
const transaction = runtime.edit();
582+
583+
// Write to empty path with object containing "value" property
584+
const writeResult = transaction.write({
585+
space,
586+
id: "of:test-root",
587+
type: "application/json",
588+
path: [],
589+
}, { value: { foo: "bar" } });
590+
591+
expect(writeResult.ok).toBeDefined();
592+
593+
// Should be able to read the value at path ["value"]
594+
const readResult = transaction.readOrThrow({
595+
space,
596+
id: "of:test-root",
597+
type: "application/json",
598+
path: ["value"],
599+
});
600+
601+
expect(readResult).toEqual({ foo: "bar" });
602+
});
603+
604+
it("should not rewrite non-empty paths", () => {
605+
const transaction = runtime.edit();
606+
607+
// First create a document
608+
transaction.write({
609+
space,
610+
id: "of:test-nested",
611+
type: "application/json",
612+
path: ["value"],
613+
}, {});
614+
615+
// Write to non-empty path with object containing "value" property
616+
transaction.write({
617+
space,
618+
id: "of:test-nested",
619+
type: "application/json",
620+
path: ["value", "nested"],
621+
}, { value: "should not be rewritten" });
622+
623+
// Should store the object as-is
624+
const readResult = transaction.readOrThrow({
625+
space,
626+
id: "of:test-nested",
627+
type: "application/json",
628+
path: ["value", "nested"],
629+
});
630+
631+
expect(readResult).toEqual({ value: "should not be rewritten" });
632+
});
633+
634+
it("should not rewrite non-objects", () => {
635+
const transaction = runtime.edit();
636+
637+
// Write non-object to empty path
638+
const writeResult = transaction.write({
639+
space,
640+
id: "of:test-string",
641+
type: "application/json",
642+
path: [],
643+
}, "plain string");
644+
645+
// Should get an error since path is empty and value is not rewritten
646+
expect(writeResult.error).toBeDefined();
647+
expect(writeResult.error?.name).toBe("NotFoundError");
648+
});
649+
});
650+
563651
describe("data: URI behaviors", () => {
564652
let runtime: Runtime;
565653
let storageManager: ReturnType<typeof StorageManager.emulate>;

0 commit comments

Comments
 (0)