callbacks.test.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. import { describe, expect, test } from "bun:test"
  2. import { Effect, Schema } from "effect"
  3. import { CodeMode, Tool } from "../src/index.js"
  4. // Callback acceptance is one gate shared by array methods, sort, string replacers,
  5. // Array.from mappers, Map/Set/URLSearchParams forEach, and promise reactions:
  6. // interpreter functions, coercion/URI builtins, resolver capabilities, and built-in
  7. // method references are callable; tools and other opaque callables get a wrap hint.
  8. const run = (code: string) => Effect.runPromise(CodeMode.execute({ code, tools: {} }))
  9. const value = async (code: string) => {
  10. const result = await run(code)
  11. if (!result.ok) throw new Error(`expected success, got ${result.error.kind}: ${result.error.message}`)
  12. return result.value
  13. }
  14. const error = async (code: string) => {
  15. const result = await run(code)
  16. if (result.ok) throw new Error(`expected failure, got value ${JSON.stringify(result.value)}`)
  17. return result.error
  18. }
  19. const logsOf = async (code: string) => {
  20. const result = await run(code)
  21. if (!result.ok) throw new Error(`expected success, got ${result.error.kind}: ${result.error.message}`)
  22. return result.logs ?? []
  23. }
  24. const echo = Tool.make({
  25. description: "Echo the input",
  26. input: Schema.Struct({ id: Schema.Number }),
  27. output: Schema.Number,
  28. run: (input: { id: number }) => Effect.succeed(input.id),
  29. })
  30. const withTool = (code: string) => Effect.runPromise(CodeMode.make({ tools: { host: { echo } } }).execute(code))
  31. const toolError = async (code: string) => {
  32. const result = await withTool(code)
  33. if (result.ok) throw new Error(`expected failure, got value ${JSON.stringify(result.value)}`)
  34. return result.error
  35. }
  36. describe("built-in method references as callbacks", () => {
  37. test("map accepts Math methods", async () => {
  38. expect(await value(`return [-1, 2, -3].map(Math.abs)`)).toEqual([1, 2, 3])
  39. expect(await value(`return [1.5, 2.7].map(Math.floor)`)).toEqual([1, 2])
  40. })
  41. test("map(JSON.stringify) matches JS: the index replacer and array space are ignored", async () => {
  42. expect(await value(`return [{ a: 1 }, [2]].map(JSON.stringify)`)).toEqual(['{"a":1}', "[2]"])
  43. })
  44. test("map(Number.parseInt) reproduces the JS radix footgun", async () => {
  45. // parseInt("2", 1) is NaN in real JS; NaN serializes to null at the result boundary.
  46. expect(await value(`return ["1", "2"].map(Number.parseInt)`)).toEqual([1, null])
  47. })
  48. test("filter and find accept built-in predicates", async () => {
  49. expect(await value(`return [0, 1, NaN, 2].filter(Number.isInteger)`)).toEqual([0, 1, 2])
  50. expect(await value(`return [1.5, 3, 2.5].find(Number.isInteger)`)).toBe(3)
  51. })
  52. test("forEach(console.log) captures one log line per element", async () => {
  53. const logs = await logsOf(`["a", "b"].forEach(console.log); return null`)
  54. expect(logs).toHaveLength(2)
  55. expect(logs[0]).toContain("a")
  56. expect(logs[1]).toContain("b")
  57. })
  58. test("intrinsic method references keep their receiver, unlike detached JS methods", async () => {
  59. expect(await value(`return ["a", "z"].filter("abc".includes)`)).toEqual(["a"])
  60. })
  61. test("promise reactions accept built-in references", async () => {
  62. expect(await value(`return await Promise.resolve(-5).then(Math.abs)`)).toBe(5)
  63. const logs = await logsOf(`await Promise.resolve("done").then(console.log); return null`)
  64. expect(logs).toHaveLength(1)
  65. expect(logs[0]).toContain("done")
  66. })
  67. })
  68. describe("constructors callable without new, like JS", () => {
  69. test("Error constructors work as callbacks and direct calls", async () => {
  70. expect(await value(`return ["boom"].map(Error)[0].message`)).toBe("boom")
  71. expect(await value(`return TypeError("bad").name`)).toBe("TypeError")
  72. })
  73. test("error values stringify like JS Error.prototype.toString", async () => {
  74. expect(await value(`return String(TypeError("bad"))`)).toBe("TypeError: bad")
  75. expect(await value(`return String(Error(""))`)).toBe("Error")
  76. expect(await value(`return "x" + RangeError("oops")`)).toBe("xRangeError: oops")
  77. expect(await value(`return "a1b2".replace(/\\d/, Error)`)).toBe("aError: 1b2")
  78. })
  79. test("literal elisions are real holes, like JS", async () => {
  80. expect(await value(`return (0 in [, 1])`)).toBe(false)
  81. expect(await value(`return Object.keys([, 1, ,])`)).toEqual(["1"])
  82. expect(await value(`return [, 1, ,].filter(() => true).length`)).toBe(1)
  83. expect(await value(`return [, ,].every((x) => false)`)).toBe(true)
  84. })
  85. test("Array constructs from arguments or a length", async () => {
  86. expect(await value(`return Array(1, 2, 3)`)).toEqual([1, 2, 3])
  87. expect(await value(`return Array("3")`)).toEqual(["3"])
  88. expect(await value(`return Array(3).length`)).toBe(3)
  89. expect(await value(`return new Array(2).length`)).toBe(2)
  90. // Holes stay holes, like JS: map skips them (length preserved, normalized to
  91. // null at the host boundary), spread materializes undefined.
  92. expect(await value(`return Array(3).map((x) => 1)`)).toEqual([null, null, null])
  93. expect(await value(`return Array(3).map((x) => 1).length`)).toBe(3)
  94. expect(await value(`return [...Array(3)].map((_, i) => i)`)).toEqual([0, 1, 2])
  95. expect((await error(`return Array(-1)`)).message).toContain("Invalid array length")
  96. expect((await error(`return Array(1.5)`)).message).toContain("Invalid array length")
  97. })
  98. test("Object returns objects unchanged and rejects primitive wrappers", async () => {
  99. expect(await value(`return Object()`)).toEqual({})
  100. expect(await value(`const o = { a: 1 }; return Object(o) === o`)).toBe(true)
  101. expect((await error(`return Object(1)`)).message).toContain("wrapper objects are not supported")
  102. })
  103. test("Date() without new returns a deterministic ISO string and ignores arguments", async () => {
  104. expect(await value(`return /^\\d{4}-\\d{2}-\\d{2}T.*Z$/.test(Date(1000))`)).toBe(true)
  105. expect(await value(`return "abc".replace(RegExp("b"), "x")`)).toBe("axc")
  106. })
  107. test("map(Array) matches the JS 3-argument call", async () => {
  108. expect(await value(`return [7].map(Array)`)).toEqual([[7, 0, [7]]])
  109. })
  110. test("array length boundaries match JS", async () => {
  111. expect(await value(`return Array(4294967295).length`)).toBe(4294967295)
  112. const diagnostic = await error(`return Array(4294967296)`)
  113. expect(diagnostic.message).toContain("Invalid array length")
  114. expect((await error(`try { Array(-1) } catch (e) { throw Error(e.name) }`)).message).toContain("RangeError")
  115. })
  116. test("sort densifies trailing holes into undefined (documented divergence)", async () => {
  117. expect(await value(`return Array(2).sort().map(() => 1)`)).toEqual([1, 1])
  118. })
  119. test("returned sparse arrays normalize holes to null at the host boundary", async () => {
  120. expect(await value(`return Array(3)`)).toEqual([null, null, null])
  121. })
  122. test("RegExp with non-string flags throws a SyntaxError, like JS", async () => {
  123. expect((await error(`try { RegExp("a", 0) } catch (e) { throw Error(e.name) }`)).message).toContain("SyntaxError")
  124. })
  125. test("new-requiring constructors throw a TypeError when called", async () => {
  126. expect((await error(`return Map()`)).message).toContain("Constructor Map requires 'new'")
  127. expect((await error(`return [1].map(Set)`)).message).toContain("Constructor Set requires 'new'")
  128. expect((await error(`return Promise(() => 1)`)).message).toContain("Constructor Promise requires 'new'")
  129. // As a reaction handler the TypeError rejects the derived promise catchably, like JS.
  130. expect(await value(`return await Promise.resolve(1).then(Map).catch((e) => e.name)`)).toBe("TypeError")
  131. })
  132. })
  133. describe("sort accepts the unified callback set", () => {
  134. test("sort and toSorted take built-in comparators", async () => {
  135. expect(await value(`return [0, 1, 0].sort(Boolean)`)).toEqual([0, 0, 1])
  136. expect(await value(`return [0, 1, 0].toSorted(Boolean)`)).toEqual([0, 0, 1])
  137. })
  138. test("a non-callable comparator is rejected", async () => {
  139. expect((await error(`return [2, 1].sort(42)`)).message).toContain("Array.sort expects a function callback")
  140. expect((await error(`return [2, 1].toSorted(42)`)).message).toContain("Array.toSorted expects a function callback")
  141. })
  142. })
  143. describe("Array.from mapper", () => {
  144. test("maps with (value, index) over arrays, strings, and Sets", async () => {
  145. expect(await value(`return Array.from([1, 2, 3], (x) => x * 2)`)).toEqual([2, 4, 6])
  146. expect(await value(`return Array.from("ab", (c, i) => c + i)`)).toEqual(["a0", "b1"])
  147. expect(await value(`return Array.from(new Set([1, 2]), (x) => x * 10)`)).toEqual([10, 20])
  148. })
  149. test("accepts coercion builtins and an explicit undefined mapper", async () => {
  150. expect(await value(`return Array.from(["5", "7"], Number)`)).toEqual([5, 7])
  151. expect(await value(`return Array.from([1, 2], undefined)`)).toEqual([1, 2])
  152. })
  153. test("rejects a non-callable mapper", async () => {
  154. expect((await error(`return Array.from([1], 42)`)).message).toContain("Array.from expects a function callback")
  155. })
  156. })
  157. describe("thisArg is accepted and ignored, like JS arrows", () => {
  158. // CodeMode functions have no `this`, so a thisArg can never change behavior —
  159. // exactly like passing one alongside an arrow function in real JS.
  160. test("iteration methods and Array.from ignore a thisArg", async () => {
  161. expect(await value(`return [1, 2].map((x) => x * 2, {})`)).toEqual([2, 4])
  162. expect(await value(`return [1, 2].map((x) => x, undefined)`)).toEqual([1, 2])
  163. expect(await value(`return Array.from([1], (x) => x + 1, {})`)).toEqual([2])
  164. })
  165. test("Map, Set, and URLSearchParams forEach ignore a thisArg", async () => {
  166. expect(await value(`const o = []; new Map([["a", 1]]).forEach((v, k) => o.push(k), {}); return o`)).toEqual(["a"])
  167. expect(await value(`const o = []; new Set([1]).forEach((v) => o.push(v), "self"); return o`)).toEqual([1])
  168. expect(await value(`const o = []; new URLSearchParams("a=1").forEach((v) => o.push(v), 0); return o`)).toEqual([
  169. "1",
  170. ])
  171. })
  172. })
  173. describe("still-rejected callables get the wrap hint", () => {
  174. test("tool references as callbacks suggest an arrow wrapper", async () => {
  175. const diagnostic = await toolError(`return [1, 2].map(tools.host.echo)`)
  176. expect(diagnostic.message).toContain("wrap it in an arrow function")
  177. expect(await withTool(`return await Promise.all([1, 2].map((id) => tools.host.echo({ id })))`)).toMatchObject({
  178. ok: true,
  179. value: [1, 2],
  180. })
  181. })
  182. test("detached Promise statics as callbacks suggest an arrow wrapper", async () => {
  183. expect((await error(`return [1].map(Promise.resolve)`)).message).toContain("wrap it in an arrow function")
  184. })
  185. test("string replacers reject opaque callables with the wrap hint, not a type error", async () => {
  186. const diagnostic = await toolError(`return "abc".replace(/b/, tools.host.echo)`)
  187. expect(diagnostic.message).toContain("wrap it in an arrow function")
  188. expect(diagnostic.message).not.toContain("argument 2")
  189. })
  190. test("built-in references work as replacers", async () => {
  191. // Like real JS: JSON.stringify(match, offset, string) quotes the match.
  192. expect(await value(`return "abc".replace(/b/, JSON.stringify)`)).toBe('a"b"c')
  193. // Math methods stay strict about consumed arguments: a match string is not coerced.
  194. expect((await error(`return "3.7".replace(/\\d\\.\\d/, Math.floor)`)).message).toContain(
  195. "Math.floor expects number arguments",
  196. )
  197. })
  198. test("non-callables still get the plain callback error", async () => {
  199. expect((await error(`return [1].map(42)`)).message).toContain("Array.map expects a function callback")
  200. })
  201. test("promise handlers reject opaque callables with the wrap hint", async () => {
  202. const diagnostic = await toolError(`return await Promise.resolve(1).then(tools.host.echo)`)
  203. expect(diagnostic.message).toContain("Promise.prototype.then cannot use this callable as a handler")
  204. expect(diagnostic.message).toContain("wrap it in an arrow function")
  205. })
  206. test("callable JSON.stringify replacers are rejected, never silently ignored", async () => {
  207. expect((await error(`return JSON.stringify({ a: 1 }, Math.abs)`)).message).toContain(
  208. "JSON.stringify replacers are not supported",
  209. )
  210. expect((await toolError(`return JSON.stringify({ a: 1 }, tools.host.echo)`)).message).toContain(
  211. "JSON.stringify replacers are not supported",
  212. )
  213. })
  214. test("callable JSON.parse revivers are rejected, never silently ignored", async () => {
  215. expect((await error(`return JSON.parse('{"a":1}', (key, v) => 99)`)).message).toContain(
  216. "JSON.parse revivers are not supported",
  217. )
  218. expect(await value(`return JSON.parse('{"a":1}', undefined)`)).toEqual({ a: 1 })
  219. // A non-callable reviver is silently ignored, matching JS's IsCallable check.
  220. expect(await value(`return JSON.parse('{"a":1}', 42)`)).toEqual({ a: 1 })
  221. })
  222. })