form.test.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { expect, test } from "bun:test"
  2. import type { FormField, FormValue } from "@opencode-ai/client"
  3. import {
  4. formCustom,
  5. formDisplayValue,
  6. formInitialValues,
  7. formLabel,
  8. formRows,
  9. formSelected,
  10. formSetMultiselectCustom,
  11. formTextual,
  12. formToggleMultiselect,
  13. formValidateValue,
  14. isFormAnswerField,
  15. } from "../../src/util/form"
  16. import type { FormAnswerField } from "../../src/util/form"
  17. const option = { key: "choice", type: "string", options: [{ value: "one", label: "One" }], custom: true } satisfies FormField
  18. const selection = {
  19. key: "tags",
  20. type: "multiselect",
  21. options: [
  22. { value: "one", label: "One" },
  23. { value: "two", label: "Two" },
  24. ],
  25. custom: true,
  26. } satisfies FormAnswerField
  27. test("initializes configured and custom defaults", () => {
  28. expect(
  29. formInitialValues([
  30. { key: "mode", type: "string", options: [{ value: "fast", label: "Fast" }], default: "fast" },
  31. { ...option, key: "note", default: "detailed" },
  32. { ...option, key: "configured", default: "one" },
  33. { key: "count", type: "number", default: 0 },
  34. { key: "authorize", type: "external", url: "https://example.com" },
  35. ]),
  36. ).toEqual({
  37. answers: { mode: "fast", note: "detailed", configured: "one", count: 0 },
  38. custom: { note: "detailed" },
  39. })
  40. })
  41. test("validates every supported field constraint", () => {
  42. const validate = (field: FormAnswerField, value: FormValue | undefined, error: string | undefined) =>
  43. expect(formValidateValue(field, value)).toBe(error)
  44. const string = (extra: Partial<Extract<FormAnswerField, { type: "string" }>> = {}) =>
  45. ({ key: "value", type: "string", ...extra }) satisfies FormAnswerField
  46. const multi = (extra: Partial<Extract<FormAnswerField, { type: "multiselect" }>> = {}) =>
  47. ({ key: "value", type: "multiselect", options: [], ...extra }) satisfies FormAnswerField
  48. validate(string({ required: true }), undefined, "Answer required")
  49. validate(multi({ required: true }), [], "Select at least one option")
  50. validate(string(), true, "Expected text")
  51. validate(string({ minLength: 3 }), "ab", "Must be at least 3 characters")
  52. validate(string({ maxLength: 2 }), "abc", "Must be at most 2 characters")
  53. validate(string({ pattern: "^a+$" }), "bbb", "Must match pattern: ^a+$")
  54. validate(string({ pattern: "[" }), "value", "Invalid pattern: [")
  55. validate(string({ format: "email" }), "invalid", "Expected an email address")
  56. validate(string({ format: "uri" }), "not a URL", "Expected a URL")
  57. validate(string({ format: "date" }), "2025-02-29", "Expected a date (YYYY-MM-DD)")
  58. validate(string({ format: "date-time" }), "not a date", "Expected a date and time")
  59. validate(string({ options: [{ value: "yes", label: "Yes" }] }), "no", "Select an available option")
  60. validate({ key: "value", type: "number" }, Number.NaN, "Expected a number")
  61. validate({ key: "value", type: "integer" }, 1.5, "Expected an integer")
  62. validate({ key: "value", type: "number", minimum: 2 }, 1, "Must be at least 2")
  63. validate({ key: "value", type: "number", maximum: 2 }, 3, "Must be at most 2")
  64. validate({ key: "value", type: "boolean" }, "yes", "Expected yes or no")
  65. validate(multi(), "yes", "Expected selections")
  66. validate(multi({ minItems: 2 }), ["one"], "Select at least 2")
  67. validate(multi({ maxItems: 1 }), ["one", "two"], "Select at most 1")
  68. validate(multi({ options: [{ value: "one", label: "One" }] }), ["two"], "Select only available options")
  69. validate(multi({ custom: true }), ["custom"], undefined)
  70. })
  71. test("shares field classification, rows, selection, and display", () => {
  72. const text = { key: "name", type: "string", title: "Name" } satisfies FormField
  73. const external = { key: "authorize", type: "external", url: "https://example.com" } satisfies FormField
  74. expect([isFormAnswerField(text), isFormAnswerField(external)]).toEqual([true, false])
  75. expect([formLabel(text), formLabel(external)]).toEqual(["Name", "https://example.com"])
  76. expect([formTextual(text), formTextual(option), formCustom(option)]).toEqual([true, false, true])
  77. expect(formRows({ key: "value", type: "boolean" })).toEqual([
  78. { value: true, label: "Yes" },
  79. { value: false, label: "No" },
  80. ])
  81. expect(formRows({ ...option, options: [{ value: "one", label: "One", description: "First" }] })).toEqual([
  82. { value: "one", label: "One", description: "First" },
  83. ])
  84. expect(formRows({ key: "value", type: "number" })).toEqual([])
  85. expect([formSelected(selection, "two"), formSelected(selection, "custom"), formSelected(selection, undefined)]).toEqual([
  86. 1, 2, 0,
  87. ])
  88. expect(formDisplayValue(selection, ["one", "custom"], "(none)")).toBe("One, custom")
  89. expect([formDisplayValue(selection, [], ""), formDisplayValue(selection, [], "(none)")]).toEqual(["", "(none)"])
  90. })
  91. test("updates multiselects without mutating their source", () => {
  92. const source = ["one", "custom"]
  93. expect(formToggleMultiselect(source, "one")).toEqual(["custom"])
  94. expect(formToggleMultiselect(source, "two")).toEqual(["one", "custom", "two"])
  95. expect(formSetMultiselectCustom(source, "custom", "replacement")).toEqual(["one", "replacement"])
  96. expect(source).toEqual(["one", "custom"])
  97. })