import { decodeSchema, encodeSchema } from "@noya-app/noya-schemas"; import { Type } from "@sinclair/typebox"; import { Value } from "@sinclair/typebox/value"; import { expect, it } from "bun:test"; import { StateManager } from ".."; it("throws if edit doesn't match schema", () => { const sm = new StateManager( { count: 0 }, { schema: Type.Object({ count: Type.Number() }), } ); expect(sm.state.count).toEqual(0); const errorObject = { // type: 41, // schema: { type: "number" }, path: "/count", value: "a", message: "Expected number", }; expect(() => { sm.edit((draft) => { draft.count = "a" as any; }); }).toThrow( `State does not match schema:\n${JSON.stringify(errorObject, null, 2)}` ); expect(sm.state.count).toEqual(0); }); it("serializes string schema", () => { const T = Type.String(); const T2 = decodeSchema(encodeSchema(T)) as typeof T; const value = Value.Decode(T2, "hi"); expect(value).toEqual("hi"); }); it("serializes object schema", () => { const T = Type.Object({ name: Type.String() }); const T2 = decodeSchema(encodeSchema(T)) as typeof T; const value = Value.Decode(T2, { name: "hi" }); expect(value).toEqual({ name: "hi" }); });