// Cell array element conversion tests: verifying that array elements are // correctly converted to cell links when the schema uses asCell. import { afterEach, beforeEach, describe, it } from "@std/testing/bdd"; import { expect } from "@std/expect"; import "@commonfabric/utils/equal-ignoring-symbols"; import { Writable } from "@commonfabric/api"; import { Identity } from "@commonfabric/identity"; import { StorageManager } from "@commonfabric/runner/storage/cache.deno"; import { isCell } from "../src/cell.ts"; import { JSONSchema } from "../src/builder/types.ts"; import { Runtime } from "../src/runtime.ts"; import { type IExtendedStorageTransaction } from "../src/storage/interface.ts"; const signer = await Identity.fromPassphrase("test operator"); const space = signer.did(); describe("Cell array element conversion", () => { let runtime: Runtime; let storageManager: ReturnType; let tx: IExtendedStorageTransaction; beforeEach(() => { storageManager = StorageManager.emulate({ as: signer }); runtime = new Runtime({ apiUrl: new URL(import.meta.url), storageManager, }); tx = runtime.edit(); }); afterEach(async () => { await tx.commit(); await runtime?.dispose(); await storageManager?.close(); }); it("creates links to the elements for non-link array items", async () => { const schema = { type: "array", items: { type: "object", properties: { foo: { type: "number" } }, asCell: ["cell"], }, } as const satisfies JSONSchema; const refCell = runtime.getCell<{ foo: number }[]>( space, "array-cell-contents", schema, tx, ); refCell.setRaw([{ foo: 1 }, { foo: 2 }, { foo: 3 }]); // Commit transaction to persist data await tx.commit(); await runtime.idle(); tx = runtime.edit(); const { ok: entry } = tx.read({ space, id: refCell.getAsNormalizedFullLink().id, path: ["value"], }); expect(entry?.value).toEqual([{ foo: 1 }, { foo: 2 }, { foo: 3 }]); tx = runtime.edit(); const result = refCell.get(); expect(Array.isArray(result)).toBe(true); if (Array.isArray(result)) { const first = result[0]; expect(isCell(first)).toBe(true); const firstCell = first as Writable<{ foo: number }>; const link = firstCell.getAsNormalizedFullLink(); expect(link.space).toBe(space); expect(typeof link.id).toBe("string"); expect(link.id.startsWith("of:")).toBe(true); expect(link.id.length).toBeGreaterThan(3); expect(link.path).toEqual(["0"]); expect(link.schema).toEqual( { type: "object", properties: { foo: { type: "number" } } }, ); } }); });