t3-code-android-nightly/.repos/effect-smol/packages/effect/test/http/UrlParams.test.ts
Julius Marminge 6f9cea00ae
chore(refs): sync Effect and Alchemy references to 4.0.1 and beta.80 (#16170)
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 13:22:30 -07:00

72 lines
2.4 KiB
TypeScript

import { assert, describe, it } from "@effect/vitest"
import { assertNone, assertSome, deepStrictEqual } from "@effect/vitest/utils"
import { Schema } from "effect"
import { UrlParams } from "effect/http"
import { assertSuccess } from "../utils/assert.ts"
describe("UrlParams", () => {
describe("fromInput", () => {
it("coerces null to a string", () => {
deepStrictEqual(UrlParams.fromInput({ filter: null }).params, [["filter", "null"]])
})
})
describe("setAll", () => {
it("does not retain parameters when reusing UrlParams overrides", () => {
const overrides = UrlParams.fromInput({ sort: "name" })
UrlParams.setAll(UrlParams.fromInput({ page: 1 }), overrides)
assert.strictEqual(
UrlParams.toString(UrlParams.setAll(UrlParams.fromInput({ page: 2 }), overrides)),
"sort=name&page=2"
)
})
})
describe("Schema.UrlParams", () => {
it("round-trips ordered pairs with the serializer annotation", () => {
const iso = Schema.toIso(Schema.UrlParams)
const params = UrlParams.make([["a", "1"], ["b", "2"]])
assertSuccess(iso.getResult(params), [["a", "1"], ["b", "2"]])
assertSuccess(iso.replaceResult([["a", "1"], ["b", "3"]], params), UrlParams.make([["a", "1"], ["b", "3"]]))
})
})
it("getFirst and getLast return Option", () => {
const params = UrlParams.fromInput([[
"foo",
"a"
], [
"foo",
"b"
]])
assertSome(UrlParams.getFirst(params, "foo"), "a")
assertSome(UrlParams.getLast(params, "foo"), "b")
assertNone(UrlParams.getFirst(params, "bar"))
assertNone(UrlParams.getLast(params, "bar"))
})
it("JsonFromUrlParamsField applies a JSON reviver", () => {
const schema = Schema.JsonFromUrlParamsField("json", {
reviver: (key, value) => key === "value" ? "revived" : value
}).pipe(Schema.decodeTo(Schema.Struct({ value: Schema.String })))
deepStrictEqual(
Schema.decodeSync(schema)(UrlParams.fromInput({ json: "{\"value\":\"original\"}" })),
{ value: "revived" }
)
})
it("RecordFromUrlParams preserves single and repeated values", () => {
const params = UrlParams.make([["a", "1"], ["a", "2"], ["b", "3"]])
deepStrictEqual(Schema.decodeSync(Schema.RecordFromUrlParams)(params), {
a: ["1", "2"],
b: "3"
})
deepStrictEqual(
Schema.encodeSync(Schema.RecordFromUrlParams)({ a: ["1", "2"], b: "3" }),
params
)
})
})