Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/web_build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ jobs:

- name: Run all tests
working-directory: ./web
run: npm run test --workspace=tests
run: npm run test --workspace=lib
3 changes: 2 additions & 1 deletion web/lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"service": true
},
"test": {
"command": "node --test --enable-source-maps --test-reporter spec dist/src/0.8/tests/**/*.test.js",
"command": "node --test --enable-source-maps --test-reporter spec dist/src/0.8/*.test.js",
"dependencies": [
"build"
]
Expand Down Expand Up @@ -94,6 +94,7 @@
"homepage": "https://github.com/google/A2UI/tree/main/web#readme",
"devDependencies": {
"@types/markdown-it": "^14.1.2",
"@types/node": "^24.10.1",
"typescript": "^5.8.3",
"wireit": "^0.15.0-pre.2"
},
Expand Down
5 changes: 5 additions & 0 deletions web/lib/src/0.8/data/guards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,13 @@ import {
ResolvedText,
ResolvedTextField,
ResolvedVideo,
ValueMap,
} from "../types/types";

export function isValueMap(value: unknown): value is ValueMap {
return isObject(value) && "key" in value;
}

export function isPath(key: string, value: unknown): value is string {
return key === "path" && typeof value === "string";
}
Expand Down
9 changes: 8 additions & 1 deletion web/lib/src/0.8/data/model-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import {
SurfaceID,
SurfaceUpdateMessage,
ModelProcessor,
ValueMap,
DataObject,
} from "../types/types";
import {
isComponentArrayReference,
Expand All @@ -52,6 +54,7 @@ import {
isResolvedText,
isResolvedTextField,
isResolvedVideo,
isValueMap,
} from "./guards.js";

/**
Expand Down Expand Up @@ -423,7 +426,11 @@ export class A2UIModelProcessor implements ModelProcessor {
#handleDataModelUpdate(message: DataModelUpdate, surfaceId: SurfaceID): void {
const surface = this.#getOrCreateSurface(surfaceId);
const path = message.path ?? "/";
this.#setDataByPath(surface.dataModel, path, message.contents);
this.#setDataByPath(
surface.dataModel,
path,
message.contents
);
this.#rebuildComponentTree(surface);
}

Expand Down
43 changes: 22 additions & 21 deletions web/tests/0.8/model.test.ts → web/lib/src/0.8/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import assert from "node:assert";
import { describe, it, beforeEach } from "node:test";
import { v0_8 } from "@a2ui/web-lib";
import {DataMap, DataValue} from "./types/types";

// Helper function to strip reactivity for clean comparisons.
const toPlainObject = (value: unknown): ReturnType<typeof JSON.parse> => {
Expand Down Expand Up @@ -87,8 +88,8 @@ describe("A2UIModelProcessor", () => {

const defaultSurface = surfaces.get("@default");
assert.ok(defaultSurface, "Default surface should exist");
assert.strictEqual(defaultSurface.rootComponentId, "comp-a");
assert.deepStrictEqual(defaultSurface.styles, { color: "blue" });
assert.strictEqual(defaultSurface!.rootComponentId, "comp-a");
assert.deepStrictEqual(defaultSurface!.styles, { color: "blue" });
});

it("should handle `surfaceUpdate` by adding components", () => {
Expand All @@ -112,8 +113,8 @@ describe("A2UIModelProcessor", () => {
if (!surface) {
assert.fail("No default surface");
}
assert.strictEqual(surface.components.size, 1);
assert.ok(surface.components.has("comp-a"));
assert.strictEqual(surface!.components.size, 1);
assert.ok(surface!.components.has("comp-a"));
});

it("should handle `deleteSurface`", () => {
Expand Down Expand Up @@ -290,10 +291,7 @@ describe("A2UIModelProcessor", () => {
);

// Check that it's a Map and has the first item.
assert.ok(
messagesData instanceof Map,
"Data at /messages should be a Map"
);
assertIsDataMap(messagesData);
assert.strictEqual(messagesData.size, 1);
assert.strictEqual(messagesData.get(key1), message1);

Expand Down Expand Up @@ -322,13 +320,10 @@ describe("A2UIModelProcessor", () => {
);

// 4. Check that the Map was additively updated and now has both items.
assert.ok(
messagesData instanceof Map,
"Data at /messages should still be a Map"
);
assertIsDataMap(messagesData);
assert.strictEqual(messagesData.size, 2, "Map should have 2 items total");
assert.strictEqual(
messagesData.get(key1),
(messagesData as DataMap).get(key1),
message1,
"First item correct"
);
Expand Down Expand Up @@ -1311,26 +1306,32 @@ describe("A2UIModelProcessor", () => {
assert.ok(surfaceA && surfaceB, "Both surfaces should exist");

// Check Surface A
assert.strictEqual(surfaceA.components.size, 1);
assert.ok(surfaceA.components.has("comp-a"));
assert.deepStrictEqual(toPlainObject(surfaceA.dataModel), {
assert.ok(surfaceA, "Surface A exists.");
assert.strictEqual(surfaceA!.components.size, 1);
assert.ok(surfaceA!.components.has("comp-a"));
assert.deepStrictEqual(toPlainObject(surfaceA!.dataModel), {
name: "Surface A Data",
});
assert.deepStrictEqual(
toPlainObject(surfaceA.componentTree).properties.text,
toPlainObject(surfaceA!.componentTree).properties.text,
{ path: "/name" }
);

// Check Surface B
assert.strictEqual(surfaceB.components.size, 1);
assert.ok(surfaceB.components.has("comp-b"));
assert.deepStrictEqual(toPlainObject(surfaceB.dataModel), {
assert.ok(surfaceB, "Surface B exists.");
assert.strictEqual(surfaceB!.components.size, 1);
assert.ok(surfaceB!.components.has("comp-b"));
assert.deepStrictEqual(toPlainObject(surfaceB!.dataModel), {
name: "Surface B Data",
});
assert.deepStrictEqual(
toPlainObject(surfaceB.componentTree).properties.text,
toPlainObject(surfaceB!.componentTree).properties.text,
{ path: "/name" }
);
});
});
});

function assertIsDataMap(obj: DataValue): asserts obj is DataMap {
assert.ok(obj instanceof Map, `Data should be a DataMap`);
}
24 changes: 11 additions & 13 deletions web/lib/src/0.8/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,19 +263,17 @@ export interface SurfaceUpdateMessage {
export interface DataModelUpdate {
surfaceId: string;
path?: string;
contents: {
key: string;
valueString?: string /** May be JSON */;
valueNumber?: number;
valueBoolean?: boolean;

valueMap?: {
key: string;
valueString?: string /** May be JSON */;
valueNumber?: number;
valueBoolean?: boolean;
}[];
}[];
contents: ValueMap[];
}

// ValueMap is a type of DataObject for passing to the data model.
export type ValueMap = DataObject & {
key: string;
/** May be JSON */
valueString?: string;
valueNumber?: number;
valueBoolean?: boolean;
valueMap?: ValueMap[];
}

export interface DeleteSurfaceMessage {
Expand Down
2 changes: 1 addition & 1 deletion web/lib/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"noUnusedLocals": false,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"types": []
"types": ["node"]
},
"include": ["src/**/*.ts"]
}
Loading