@ic-reactor/candid 3.0.12-beta.0 → 3.0.14-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,11 @@
1
1
  import { describe, it, expect } from "vitest"
2
2
  import { IDL } from "@icp-sdk/core/candid"
3
3
  import { Principal } from "@icp-sdk/core/principal"
4
- import { ArgumentFieldVisitor } from "./index"
4
+ import { FieldVisitor } from "./index"
5
5
  import * as z from "zod"
6
6
 
7
- describe("ArgumentFieldVisitor Schema Generation", () => {
8
- const visitor = new ArgumentFieldVisitor()
7
+ describe("FieldVisitor Schema Generation", () => {
8
+ const visitor = new FieldVisitor()
9
9
 
10
10
  // ════════════════════════════════════════════════════════════════════════
11
11
  // Primitive Types
@@ -62,6 +62,108 @@ describe("ArgumentFieldVisitor Schema Generation", () => {
62
62
  })
63
63
  })
64
64
 
65
+ // ════════════════════════════════════════════════════════════════════════
66
+ // Format Detection & Validation
67
+ // ════════════════════════════════════════════════════════════════════════
68
+
69
+ describe("Format Detection & Validation", () => {
70
+ describe("Text Formats", () => {
71
+ it("should detect email format", () => {
72
+ const field = visitor.visitText(IDL.Text, "user_email")
73
+
74
+ expect(field.format).toBe("email")
75
+ expect(field.inputProps).toMatchObject({
76
+ type: "email",
77
+ inputMode: "email",
78
+ })
79
+
80
+ // Valid email
81
+ expect(field.schema.parse("test@example.com")).toBe("test@example.com")
82
+ // Invalid email
83
+ expect(() => field.schema.parse("invalid-email")).toThrow()
84
+ })
85
+
86
+ it("should detect url format", () => {
87
+ const field = visitor.visitText(IDL.Text, "website_link")
88
+
89
+ expect(field.format).toBe("url")
90
+ expect(field.inputProps).toMatchObject({
91
+ type: "url",
92
+ inputMode: "url",
93
+ })
94
+
95
+ // Valid URL
96
+ expect(field.schema.parse("https://example.com")).toBe(
97
+ "https://example.com"
98
+ )
99
+ // Invalid URL
100
+ expect(() => field.schema.parse("not-a-url")).toThrow()
101
+ })
102
+
103
+ it("should detect uuid format", () => {
104
+ const field = visitor.visitText(IDL.Text, "transaction_uuid")
105
+
106
+ expect(field.format).toBe("uuid")
107
+
108
+ const validUuid = "123e4567-e89b-12d3-a456-426614174000"
109
+ expect(field.schema.parse(validUuid)).toBe(validUuid)
110
+
111
+ expect(() => field.schema.parse("invalid-uuid")).toThrow()
112
+ })
113
+
114
+ it("should detect ethereum address format", () => {
115
+ const field = visitor.visitText(IDL.Text, "eth_address")
116
+
117
+ expect(field.format).toBe("eth")
118
+ expect(field.inputProps.pattern).toContain("0x")
119
+ })
120
+
121
+ it("should fallback to plain text for unknown formats", () => {
122
+ const field = visitor.visitText(IDL.Text, "some_random_field")
123
+
124
+ expect(field.format).toBe("plain")
125
+ expect(field.inputProps).toMatchObject({
126
+ type: "text",
127
+ })
128
+ })
129
+ })
130
+
131
+ describe("Number Formats", () => {
132
+ it("should detect timestamp format", () => {
133
+ const field = visitor.visitInt(IDL.Int, "created_at")
134
+
135
+ expect(field.format).toBe("timestamp")
136
+ })
137
+
138
+ it("should detect cycle format", () => {
139
+ const field = visitor.visitNat(IDL.Nat, "cycles_balance")
140
+
141
+ expect(field.format).toBe("cycle")
142
+ })
143
+
144
+ it("should fallback to normal format", () => {
145
+ const field = visitor.visitNat(IDL.Nat, "quantity")
146
+
147
+ expect(field.format).toBe("plain")
148
+ })
149
+ })
150
+
151
+ describe("Principal Format", () => {
152
+ it("should detect principal format and set properties", () => {
153
+ const field = visitor.visitPrincipal(
154
+ IDL.Principal,
155
+ "controller_principal"
156
+ )
157
+
158
+ expect(field.format).toBe("principal")
159
+ expect(field.inputProps).toMatchObject({
160
+ minLength: 7,
161
+ maxLength: 64,
162
+ })
163
+ })
164
+ })
165
+ })
166
+
65
167
  // ════════════════════════════════════════════════════════════════════════
66
168
  // Compound Types
67
169
  // ════════════════════════════════════════════════════════════════════════
@@ -106,10 +208,16 @@ describe("ArgumentFieldVisitor Schema Generation", () => {
106
208
  )
107
209
  const schema = field.schema as z.ZodUnion<any>
108
210
 
109
- expect(schema.parse({ Ok: "Success" })).toEqual({ Ok: "Success" })
110
- expect(schema.parse({ Err: "Error" })).toEqual({ Err: "Error" })
211
+ expect(schema.parse({ _type: "Ok", Ok: "Success" })).toEqual({
212
+ _type: "Ok",
213
+ Ok: "Success",
214
+ })
215
+ expect(schema.parse({ _type: "Err", Err: "Error" })).toEqual({
216
+ _type: "Err",
217
+ Err: "Error",
218
+ })
111
219
 
112
- expect(() => schema.parse({ Other: "value" })).toThrow()
220
+ expect(() => schema.parse({ _type: "Other", Other: "value" })).toThrow()
113
221
  })
114
222
  })
115
223
 
@@ -179,12 +287,14 @@ describe("ArgumentFieldVisitor Schema Generation", () => {
179
287
  const schema = field.schema
180
288
 
181
289
  const validList = {
290
+ _type: "Cons",
182
291
  Cons: {
183
292
  head: "1",
184
293
  tail: {
294
+ _type: "Cons",
185
295
  Cons: {
186
296
  head: "2",
187
- tail: { Nil: null },
297
+ tail: { _type: "Nil" },
188
298
  },
189
299
  },
190
300
  },