mirror of
https://github.com/Start9Labs/start-sdk.git
synced 2026-03-30 04:11:57 +00:00
chore: Wrapper Data Contract
This commit is contained in:
@@ -5,6 +5,10 @@ import { Value } from "../config/builder/value"
|
||||
import { Variants } from "../config/builder/variants"
|
||||
import { ValueSpec } from "../config/configTypes"
|
||||
import { Parser } from "ts-matches"
|
||||
import {
|
||||
createWrapperDataContract,
|
||||
neverWrapperDataContract,
|
||||
} from "../wrapperData/wrapperDataContract"
|
||||
|
||||
type test = unknown | { test: 5 }
|
||||
describe("builder tests", () => {
|
||||
@@ -299,7 +303,7 @@ describe("values", () => {
|
||||
utils: "utils",
|
||||
} as any
|
||||
test("toggle", async () => {
|
||||
const value = Value.dynamicToggle<{}>(async () => ({
|
||||
const value = Value.dynamicToggle(neverWrapperDataContract, async () => ({
|
||||
name: "Testing",
|
||||
description: null,
|
||||
warning: null,
|
||||
@@ -317,7 +321,7 @@ describe("values", () => {
|
||||
})
|
||||
})
|
||||
test("text", async () => {
|
||||
const value = Value.dynamicText(async () => ({
|
||||
const value = Value.dynamicText(neverWrapperDataContract, async () => ({
|
||||
name: "Testing",
|
||||
required: { default: null },
|
||||
}))
|
||||
@@ -333,7 +337,7 @@ describe("values", () => {
|
||||
})
|
||||
})
|
||||
test("text with default", async () => {
|
||||
const value = Value.dynamicText(async () => ({
|
||||
const value = Value.dynamicText(neverWrapperDataContract, async () => ({
|
||||
name: "Testing",
|
||||
required: { default: "this is a default value" },
|
||||
}))
|
||||
@@ -348,7 +352,7 @@ describe("values", () => {
|
||||
})
|
||||
})
|
||||
test("optional text", async () => {
|
||||
const value = Value.dynamicText(async () => ({
|
||||
const value = Value.dynamicText(neverWrapperDataContract, async () => ({
|
||||
name: "Testing",
|
||||
required: false,
|
||||
}))
|
||||
@@ -364,7 +368,7 @@ describe("values", () => {
|
||||
})
|
||||
})
|
||||
test("color", async () => {
|
||||
const value = Value.dynamicColor<null>(async () => ({
|
||||
const value = Value.dynamicColor(neverWrapperDataContract, async () => ({
|
||||
name: "Testing",
|
||||
required: false,
|
||||
description: null,
|
||||
@@ -383,17 +387,20 @@ describe("values", () => {
|
||||
})
|
||||
})
|
||||
test("datetime", async () => {
|
||||
const value = Value.dynamicDatetime<{ test: "a" }>(async ({ utils }) => {
|
||||
;async () => {
|
||||
;(await utils.getOwnWrapperData("/test").once()) satisfies "a"
|
||||
}
|
||||
const value = Value.dynamicDatetime(
|
||||
createWrapperDataContract<{ test: "a" }>(),
|
||||
async ({ utils }) => {
|
||||
;async () => {
|
||||
;(await utils.getOwnWrapperData("/test").once()) satisfies "a"
|
||||
}
|
||||
|
||||
return {
|
||||
name: "Testing",
|
||||
required: { default: null },
|
||||
inputmode: "date",
|
||||
}
|
||||
})
|
||||
return {
|
||||
name: "Testing",
|
||||
required: { default: null },
|
||||
inputmode: "date",
|
||||
}
|
||||
},
|
||||
)
|
||||
const validator = value.validator
|
||||
validator.unsafeCast("2021-01-01")
|
||||
validator.unsafeCast(null)
|
||||
@@ -408,15 +415,18 @@ describe("values", () => {
|
||||
})
|
||||
})
|
||||
test("textarea", async () => {
|
||||
const value = Value.dynamicTextarea(async () => ({
|
||||
name: "Testing",
|
||||
required: false,
|
||||
description: null,
|
||||
warning: null,
|
||||
minLength: null,
|
||||
maxLength: null,
|
||||
placeholder: null,
|
||||
}))
|
||||
const value = Value.dynamicTextarea(
|
||||
neverWrapperDataContract,
|
||||
async () => ({
|
||||
name: "Testing",
|
||||
required: false,
|
||||
description: null,
|
||||
warning: null,
|
||||
minLength: null,
|
||||
maxLength: null,
|
||||
placeholder: null,
|
||||
}),
|
||||
)
|
||||
const validator = value.validator
|
||||
validator.unsafeCast("test text")
|
||||
expect(() => validator.unsafeCast(null)).toThrowError()
|
||||
@@ -427,7 +437,7 @@ describe("values", () => {
|
||||
})
|
||||
})
|
||||
test("number", async () => {
|
||||
const value = Value.dynamicNumber(() => ({
|
||||
const value = Value.dynamicNumber(neverWrapperDataContract, () => ({
|
||||
name: "Testing",
|
||||
required: { default: null },
|
||||
integer: false,
|
||||
@@ -500,7 +510,9 @@ describe("values", () => {
|
||||
})
|
||||
describe("filtering", () => {
|
||||
test("union", async () => {
|
||||
const value = Value.filteredUnion(() => ["a", "c"])(
|
||||
const value = Value.filteredUnion(
|
||||
neverWrapperDataContract,
|
||||
() => ["a", "c"],
|
||||
{
|
||||
name: "Testing",
|
||||
required: { default: null },
|
||||
@@ -608,7 +620,7 @@ describe("Builder List", () => {
|
||||
describe("dynamic", () => {
|
||||
test("text", async () => {
|
||||
const value = Value.list(
|
||||
List.dynamicText(() => ({
|
||||
List.dynamicText(neverWrapperDataContract, () => ({
|
||||
name: "test",
|
||||
spec: { patterns: [] },
|
||||
})),
|
||||
@@ -626,7 +638,7 @@ describe("Builder List", () => {
|
||||
})
|
||||
test("number", async () => {
|
||||
const value = Value.list(
|
||||
List.dynamicNumber(() => ({
|
||||
List.dynamicNumber(neverWrapperDataContract, () => ({
|
||||
name: "test",
|
||||
spec: { integer: true },
|
||||
})),
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
import { Effects } from "../types"
|
||||
import { utils } from "../util"
|
||||
import { createMainUtils, utils } from "../util"
|
||||
import { createWrapperDataContract } from "../wrapperData/wrapperDataContract"
|
||||
|
||||
type WrapperType = {
|
||||
config: {
|
||||
someValue: "a" | "b"
|
||||
}
|
||||
}
|
||||
const wrapperDataContract = createWrapperDataContract<WrapperType>()
|
||||
const todo = <A>(): A => {
|
||||
throw new Error("not implemented")
|
||||
}
|
||||
@@ -13,23 +15,23 @@ const noop = () => {}
|
||||
describe("wrapperData", () => {
|
||||
test("types", async () => {
|
||||
;async () => {
|
||||
utils<WrapperType>(todo<Effects>()).setOwnWrapperData("/config", {
|
||||
utils(wrapperDataContract, todo<Effects>()).setOwnWrapperData("/config", {
|
||||
someValue: "a",
|
||||
})
|
||||
utils<WrapperType>(todo<Effects>()).setOwnWrapperData(
|
||||
utils(wrapperDataContract, todo<Effects>()).setOwnWrapperData(
|
||||
"/config/someValue",
|
||||
"b",
|
||||
)
|
||||
utils<WrapperType>(todo<Effects>()).setOwnWrapperData("", {
|
||||
utils(wrapperDataContract, todo<Effects>()).setOwnWrapperData("", {
|
||||
config: { someValue: "b" },
|
||||
})
|
||||
utils<WrapperType>(todo<Effects>()).setOwnWrapperData(
|
||||
utils(wrapperDataContract, todo<Effects>()).setOwnWrapperData(
|
||||
"/config/someValue",
|
||||
|
||||
// @ts-expect-error Type is wrong for the setting value
|
||||
5,
|
||||
)
|
||||
utils<WrapperType>(todo<Effects>()).setOwnWrapperData(
|
||||
utils(wrapperDataContract, todo<Effects>()).setOwnWrapperData(
|
||||
// @ts-expect-error Path is wrong
|
||||
"/config/someVae3lue",
|
||||
"someValue",
|
||||
@@ -50,47 +52,47 @@ describe("wrapperData", () => {
|
||||
path: "/config/some2Value",
|
||||
value: "a",
|
||||
})
|
||||
;(await utils<WrapperType, {}>(todo<Effects>())
|
||||
;(await createMainUtils(wrapperDataContract, todo<Effects>())
|
||||
.getOwnWrapperData("/config/someValue")
|
||||
.const()) satisfies string
|
||||
;(await utils<WrapperType, {}>(todo<Effects>())
|
||||
;(await createMainUtils(wrapperDataContract, todo<Effects>())
|
||||
.getOwnWrapperData("/config")
|
||||
.const()) satisfies WrapperType["config"]
|
||||
await utils<WrapperType, {}>(todo<Effects>())
|
||||
await createMainUtils(wrapperDataContract, todo<Effects>())
|
||||
// @ts-expect-error Path is wrong
|
||||
.getOwnWrapperData("/config/somdsfeValue")
|
||||
.const()
|
||||
/// ----------------- ERRORS -----------------
|
||||
|
||||
utils<WrapperType>(todo<Effects>()).setOwnWrapperData("", {
|
||||
utils(wrapperDataContract, todo<Effects>()).setOwnWrapperData("", {
|
||||
// @ts-expect-error Type is wrong for the setting value
|
||||
config: { someValue: "notInAOrB" },
|
||||
})
|
||||
utils<WrapperType>(todo<Effects>()).setOwnWrapperData(
|
||||
utils(wrapperDataContract, todo<Effects>()).setOwnWrapperData(
|
||||
"/config/someValue",
|
||||
// @ts-expect-error Type is wrong for the setting value
|
||||
"notInAOrB",
|
||||
)
|
||||
;(await utils<WrapperType>(todo<Effects>())
|
||||
;(await utils(wrapperDataContract, todo<Effects>())
|
||||
.getOwnWrapperData("/config/someValue")
|
||||
// @ts-expect-error Const should normally not be callable
|
||||
.const()) satisfies string
|
||||
;(await utils<WrapperType>(todo<Effects>())
|
||||
;(await utils(wrapperDataContract, todo<Effects>())
|
||||
.getOwnWrapperData("/config")
|
||||
// @ts-expect-error Const should normally not be callable
|
||||
.const()) satisfies WrapperType["config"]
|
||||
await utils<WrapperType>(todo<Effects>())
|
||||
await utils(wrapperDataContract, todo<Effects>())
|
||||
// @ts-expect-error Path is wrong
|
||||
.getOwnWrapperData("/config/somdsfeValue")
|
||||
// @ts-expect-error Const should normally not be callable
|
||||
.const()
|
||||
|
||||
///
|
||||
;(await utils<WrapperType>(todo<Effects>())
|
||||
;(await utils(wrapperDataContract, todo<Effects>())
|
||||
.getOwnWrapperData("/config/someValue")
|
||||
// @ts-expect-error satisfies type is wrong
|
||||
.const()) satisfies number
|
||||
;(await utils<WrapperType, {}>(todo<Effects>())
|
||||
;(await createMainUtils(wrapperDataContract, todo<Effects>())
|
||||
// @ts-expect-error Path is wrong
|
||||
.getOwnWrapperData("/config/")
|
||||
.const()) satisfies WrapperType["config"]
|
||||
|
||||
Reference in New Issue
Block a user