@supatype/types 0.9.2 → 0.10.1
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.
- package/.turbo/turbo-build.log +1 -1
- package/.turbo/turbo-test.log +23 -15
- package/.turbo/turbo-typecheck.log +1 -1
- package/CHANGELOG.md +19 -1
- package/dist/auth-settings-groups.d.ts +216 -0
- package/dist/auth-settings-groups.d.ts.map +1 -0
- package/dist/auth-settings-groups.js +424 -0
- package/dist/auth-settings-groups.js.map +1 -0
- package/dist/auth-settings.d.ts +168 -0
- package/dist/auth-settings.d.ts.map +1 -0
- package/dist/auth-settings.js +501 -0
- package/dist/auth-settings.js.map +1 -0
- package/dist/function-secret-name.d.ts +14 -0
- package/dist/function-secret-name.d.ts.map +1 -0
- package/dist/function-secret-name.js +20 -0
- package/dist/function-secret-name.js.map +1 -0
- package/dist/index.d.ts +145 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/public-env.d.ts +70 -0
- package/dist/public-env.d.ts.map +1 -0
- package/dist/public-env.js +146 -0
- package/dist/public-env.js.map +1 -0
- package/dist/seed-contract.d.ts +103 -0
- package/dist/seed-contract.d.ts.map +1 -0
- package/dist/seed-contract.js +99 -0
- package/dist/seed-contract.js.map +1 -0
- package/package.json +1 -1
- package/src/auth-settings-groups.ts +604 -0
- package/src/auth-settings.ts +649 -0
- package/src/function-secret-name.ts +21 -0
- package/src/index.ts +176 -2
- package/src/public-env.ts +167 -0
- package/src/seed-contract.ts +167 -0
- package/tests/auth-settings-groups.test.ts +342 -0
- package/tests/auth-settings.test.ts +327 -0
- package/tests/bucket-access.test.ts +33 -1
- package/tests/channel.test.ts +94 -0
- package/tests/function-secret-name.test.ts +17 -0
- package/tests/public-env.test.ts +179 -0
- package/tests/seed-contract.test.ts +110 -0
- package/tests/standalone-build.test.ts +35 -0
- package/tsconfig.json +2 -1
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
import { describe, expectTypeOf, it } from "vitest"
|
|
2
|
-
import type {
|
|
2
|
+
import type {
|
|
3
|
+
All,
|
|
4
|
+
AuthUid,
|
|
5
|
+
BucketLoggedIn,
|
|
6
|
+
BucketOwner,
|
|
7
|
+
BucketPublic,
|
|
8
|
+
BucketStorageAccess,
|
|
9
|
+
Claim,
|
|
10
|
+
Eq,
|
|
11
|
+
Folder,
|
|
12
|
+
Literal,
|
|
13
|
+
} from "../src/index.js"
|
|
3
14
|
|
|
4
15
|
describe("BucketStorageAccess", () => {
|
|
5
16
|
it("has the four operations a model has, update included", () => {
|
|
@@ -18,3 +29,24 @@ describe("BucketStorageAccess", () => {
|
|
|
18
29
|
}>().toExtend<BucketStorageAccess>()
|
|
19
30
|
})
|
|
20
31
|
})
|
|
32
|
+
|
|
33
|
+
describe("bucket rules that name the caller and the path", () => {
|
|
34
|
+
it("accepts a per-user folder rule", () => {
|
|
35
|
+
expectTypeOf<{
|
|
36
|
+
create: Eq<Folder<1>, AuthUid>
|
|
37
|
+
update: Eq<Folder<1>, AuthUid>
|
|
38
|
+
}>().toExtend<BucketStorageAccess>()
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it("accepts a registered-only rule built from a claim", () => {
|
|
42
|
+
expectTypeOf<{
|
|
43
|
+
create: All<[BucketLoggedIn, Eq<Claim<"is_anonymous">, Literal<false>>]>
|
|
44
|
+
}>().toExtend<BucketStorageAccess>()
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it("takes a number as the folder position", () => {
|
|
48
|
+
// @ts-expect-error a folder position is a number
|
|
49
|
+
type Bad = Folder<"1">
|
|
50
|
+
expectTypeOf<Folder<2>>().not.toEqualTypeOf<Folder<1>>()
|
|
51
|
+
})
|
|
52
|
+
})
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { describe, expectTypeOf, it } from "vitest"
|
|
2
|
+
import type {
|
|
3
|
+
Any,
|
|
4
|
+
All,
|
|
5
|
+
Channel,
|
|
6
|
+
ChannelConfig,
|
|
7
|
+
ChannelParamNames,
|
|
8
|
+
Eq,
|
|
9
|
+
Event,
|
|
10
|
+
In,
|
|
11
|
+
Int,
|
|
12
|
+
LoggedIn,
|
|
13
|
+
Param,
|
|
14
|
+
Payload,
|
|
15
|
+
PayloadCheck,
|
|
16
|
+
Role,
|
|
17
|
+
Rows,
|
|
18
|
+
UUID,
|
|
19
|
+
} from "../src/index.js"
|
|
20
|
+
|
|
21
|
+
type Mine = Rows<"lot_watchers", "lot_id", Eq<"user_id", Param<"id">>>
|
|
22
|
+
|
|
23
|
+
describe("ChannelParamNames", () => {
|
|
24
|
+
it("reads the names out of a pattern", () => {
|
|
25
|
+
expectTypeOf<ChannelParamNames<"lot:{id}">>().toEqualTypeOf<"id">()
|
|
26
|
+
expectTypeOf<ChannelParamNames<"room:{org}:{slug}:chat">>().toEqualTypeOf<"org" | "slug">()
|
|
27
|
+
expectTypeOf<ChannelParamNames<"lobby">>().toEqualTypeOf<never>()
|
|
28
|
+
expectTypeOf<ChannelParamNames<"org:{org}:*">>().toEqualTypeOf<"org">()
|
|
29
|
+
})
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
describe("Channel", () => {
|
|
33
|
+
it("accepts a declared channel with parameters", () => {
|
|
34
|
+
type Lot = Channel<"lot:{id}", {
|
|
35
|
+
params: { id: UUID }
|
|
36
|
+
access: {
|
|
37
|
+
join: Any<[In<"id", Mine>, Role<"auctioneer">]>
|
|
38
|
+
send: All<[Event<"bid">, Payload<"amount", { kind: "number"; min: 1 }>]>
|
|
39
|
+
}
|
|
40
|
+
}>
|
|
41
|
+
expectTypeOf<Lot>().not.toBeNever()
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
it("accepts a channel with no parameters", () => {
|
|
45
|
+
expectTypeOf<Channel<"lobby", { access: { join: LoggedIn } }>>().not.toBeNever()
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it("takes UUID, string and Int parameters", () => {
|
|
49
|
+
expectTypeOf<{ a: UUID; b: string; c: Int }>().toExtend<NonNullable<ChannelConfig<"x:{a}:{b}:{c}">["params"]>>()
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it("refuses a params key the pattern does not name", () => {
|
|
53
|
+
// @ts-expect-error `nope` is not a parameter of the pattern
|
|
54
|
+
type Bad = Channel<"lot:{id}", { params: { id: UUID; nope: UUID }; access: { join: LoggedIn } }>
|
|
55
|
+
void expectTypeOf<Bad>()
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
it("refuses a parameter the pattern names but params leaves out", () => {
|
|
59
|
+
// @ts-expect-error `id` has no type
|
|
60
|
+
type Bad = Channel<"lot:{id}", { params: {}; access: { join: LoggedIn } }>
|
|
61
|
+
void expectTypeOf<Bad>()
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
it("refuses a parameter of any other type", () => {
|
|
65
|
+
// @ts-expect-error a boolean cannot sit in a channel name
|
|
66
|
+
type Bad = Channel<"lot:{id}", { params: { id: boolean }; access: { join: LoggedIn } }>
|
|
67
|
+
void expectTypeOf<Bad>()
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
it("refuses a config with no access", () => {
|
|
71
|
+
// @ts-expect-error access is required
|
|
72
|
+
type Bad = Channel<"lobby", {}>
|
|
73
|
+
void expectTypeOf<Bad>()
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
it("refuses Event<> in join and track, where it means nothing", () => {
|
|
77
|
+
// @ts-expect-error Event is only valid inside send
|
|
78
|
+
type Bad = Channel<"lobby", { access: { join: Event<"x"> } }>
|
|
79
|
+
void expectTypeOf<Bad>()
|
|
80
|
+
})
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
describe("Payload checks", () => {
|
|
84
|
+
it("are the four kinds the server can evaluate", () => {
|
|
85
|
+
expectTypeOf<PayloadCheck["kind"]>().toEqualTypeOf<"string" | "number" | "boolean" | "required">()
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
it("carry only the bounds that kind has", () => {
|
|
89
|
+
expectTypeOf<{ kind: "number"; min: 1; max: 9 }>().toExtend<PayloadCheck>()
|
|
90
|
+
expectTypeOf<{ kind: "string"; maxLength: 80 }>().toExtend<PayloadCheck>()
|
|
91
|
+
expectTypeOf<{ kind: "boolean" }>().toExtend<PayloadCheck>()
|
|
92
|
+
expectTypeOf<{ kind: "required" }>().toExtend<PayloadCheck>()
|
|
93
|
+
})
|
|
94
|
+
})
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest"
|
|
2
|
+
import { FUNCTION_SECRET_NAME_PATTERN, functionSecretNameProblem, isFunctionSecretName } from "../src/index.js"
|
|
3
|
+
|
|
4
|
+
describe("function secret names", () => {
|
|
5
|
+
it.each(["API_KEY", "_private", "a", "Stripe2", "__x__"])("accepts %s", (name) => {
|
|
6
|
+
expect(isFunctionSecretName(name)).toBe(true)
|
|
7
|
+
expect(functionSecretNameProblem(name)).toBeUndefined()
|
|
8
|
+
expect(FUNCTION_SECRET_NAME_PATTERN.test(name)).toBe(true)
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
it.each(["", "1KEY", "MY-KEY", "MY KEY", "a.b", "naïve", "KEY\n"])("refuses %j with a message that names it", (name) => {
|
|
12
|
+
expect(isFunctionSecretName(name)).toBe(false)
|
|
13
|
+
expect(functionSecretNameProblem(name)).toBe(
|
|
14
|
+
`"${name}" is not a valid secret name: use letters, digits and underscore, and do not start with a digit.`,
|
|
15
|
+
)
|
|
16
|
+
})
|
|
17
|
+
})
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest"
|
|
2
|
+
import {
|
|
3
|
+
FRONTEND_PUBLIC_PREFIXES,
|
|
4
|
+
PUBLIC_ENV_LIMITS,
|
|
5
|
+
prefixedPublicEnv,
|
|
6
|
+
publicEnvFor,
|
|
7
|
+
publicEnvProblems,
|
|
8
|
+
secretLookingReason,
|
|
9
|
+
publicEnvRefusal,
|
|
10
|
+
ENVIRONMENT_NAME,
|
|
11
|
+
PUBLIC_ENV_SOURCES,
|
|
12
|
+
PUBLIC_ENV_SOURCE_LABELS,
|
|
13
|
+
isPublicEnvDeclaration,
|
|
14
|
+
isPublicEnvEntry,
|
|
15
|
+
} from "../src/index.js"
|
|
16
|
+
|
|
17
|
+
const jwt = (claims: Record<string, unknown>): string =>
|
|
18
|
+
["e30", Buffer.from(JSON.stringify(claims)).toString("base64url"), "sig"].join(".")
|
|
19
|
+
|
|
20
|
+
describe("publicEnvProblems: names", () => {
|
|
21
|
+
it("accepts upper-case names with digits and underscores", () => {
|
|
22
|
+
expect(publicEnvProblems({ STRIPE_PUBLISHABLE_KEY: "pk_live_1", MAPS_2: "x" })).toEqual([])
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
it.each(["stripe_key", "1KEY", "KEY-NAME", "KEY NAME", "", "KEY.X"])("refuses the name %j", (name) => {
|
|
26
|
+
const problems = publicEnvProblems({ [name]: "x" })
|
|
27
|
+
expect(problems).toHaveLength(1)
|
|
28
|
+
expect(problems[0]?.name).toBe(name)
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
it("refuses a name longer than the limit", () => {
|
|
32
|
+
expect(publicEnvProblems({ ["A".repeat(PUBLIC_ENV_LIMITS.nameBytes + 1)]: "x" })).toHaveLength(1)
|
|
33
|
+
expect(publicEnvProblems({ ["A".repeat(PUBLIC_ENV_LIMITS.nameBytes)]: "x" })).toEqual([])
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
it("refuses a name that already carries a framework prefix, and the names Supatype's own values use", () => {
|
|
37
|
+
for (const name of ["VITE_X", "PUBLIC_X", "NEXT_PUBLIC_X", "EXPO_PUBLIC_X", "SUPATYPE_URL", "SUPATYPE_ANON_KEY", "SUPATYPE_ANYTHING"]) {
|
|
38
|
+
expect(publicEnvProblems({ [name]: "x" }), name).toHaveLength(1)
|
|
39
|
+
}
|
|
40
|
+
})
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
describe("publicEnvProblems: sizes", () => {
|
|
44
|
+
it("refuses a value over the limit, counted in bytes", () => {
|
|
45
|
+
expect(publicEnvProblems({ A: "x".repeat(PUBLIC_ENV_LIMITS.valueBytes) })).toEqual([])
|
|
46
|
+
expect(publicEnvProblems({ A: "x".repeat(PUBLIC_ENV_LIMITS.valueBytes + 1) })).toHaveLength(1)
|
|
47
|
+
expect(publicEnvProblems({ A: "é".repeat(PUBLIC_ENV_LIMITS.valueBytes / 2 + 1) })).toHaveLength(1)
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
it("refuses more names than the limit", () => {
|
|
51
|
+
const many = Object.fromEntries(Array.from({ length: PUBLIC_ENV_LIMITS.names + 1 }, (_, i) => [`K${i}`, "x"]))
|
|
52
|
+
const problems = publicEnvProblems(many)
|
|
53
|
+
expect(problems.some((p) => p.name === "")).toBe(true)
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
it("refuses a total over the limit", () => {
|
|
57
|
+
const big = Object.fromEntries(Array.from({ length: 10 }, (_, i) => [`K${i}`, "x".repeat(PUBLIC_ENV_LIMITS.valueBytes)]))
|
|
58
|
+
expect(PUBLIC_ENV_LIMITS.valueBytes * 10).toBeGreaterThan(PUBLIC_ENV_LIMITS.totalBytes)
|
|
59
|
+
expect(publicEnvProblems(big).some((p) => p.name === "")).toBe(true)
|
|
60
|
+
})
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
describe("secretLookingReason", () => {
|
|
64
|
+
it.each([
|
|
65
|
+
["a Stripe secret key", "sk_live_51Habcdefghijkl"],
|
|
66
|
+
["a Stripe test secret key", "sk_test_51Habcdefghijkl"],
|
|
67
|
+
["a restricted key", "rk_live_51Habcdefghijkl"],
|
|
68
|
+
["a PEM private key", "-----BEGIN PRIVATE KEY-----\nMIIE\n-----END PRIVATE KEY-----"],
|
|
69
|
+
["an RSA private key", "-----BEGIN RSA PRIVATE KEY-----\nMIIE"],
|
|
70
|
+
["an OpenSSH private key", "-----BEGIN OPENSSH PRIVATE KEY-----"],
|
|
71
|
+
["a service role token", jwt({ role: "service_role" })],
|
|
72
|
+
])("refuses %s", (_label, value) => {
|
|
73
|
+
expect(secretLookingReason(value)).toBeDefined()
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
it.each([
|
|
77
|
+
["a publishable key", "pk_live_51Habcdefghijkl"],
|
|
78
|
+
["an anon token", jwt({ role: "anon" })],
|
|
79
|
+
["an address", "https://api.example.com"],
|
|
80
|
+
["a word that only contains sk_", "risk_factor_high"],
|
|
81
|
+
["a public key block", "-----BEGIN PUBLIC KEY-----\nMIIB"],
|
|
82
|
+
])("accepts %s", (_label, value) => {
|
|
83
|
+
expect(secretLookingReason(value)).toBeUndefined()
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
it("refuses a value equal to a secret it was told about", () => {
|
|
87
|
+
expect(secretLookingReason("abc123secret", ["abc123secret"])).toBeDefined()
|
|
88
|
+
expect(secretLookingReason("abc123secret", ["other"])).toBeUndefined()
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
it("never echoes the value in the reason", () => {
|
|
92
|
+
const reason = secretLookingReason("sk_live_51Habcdefghijkl")
|
|
93
|
+
expect(reason).not.toContain("51Habcdefghijkl")
|
|
94
|
+
})
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
describe("publicEnvProblems: secrets", () => {
|
|
98
|
+
it("names the key, never the value", () => {
|
|
99
|
+
const problems = publicEnvProblems({ STRIPE: "sk_live_51Habcdefghijkl" })
|
|
100
|
+
expect(problems).toHaveLength(1)
|
|
101
|
+
expect(problems[0]?.name).toBe("STRIPE")
|
|
102
|
+
expect(JSON.stringify(problems)).not.toContain("51Habcdefghijkl")
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
it("reports every problem at once", () => {
|
|
106
|
+
expect(publicEnvProblems({ bad: "x", STRIPE: "sk_live_51Habcdefghijkl" })).toHaveLength(2)
|
|
107
|
+
})
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
describe("publicEnvFor", () => {
|
|
111
|
+
it("gives a plain string to every environment", () => {
|
|
112
|
+
expect(publicEnvFor({ A: "1" }, "production")).toEqual({ A: "1" })
|
|
113
|
+
expect(publicEnvFor({ A: "1" }, "staging")).toEqual({ A: "1" })
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
it("picks the environment's entry from a map", () => {
|
|
117
|
+
const declared = { A: { production: "p", staging: "s" }, B: "all" }
|
|
118
|
+
expect(publicEnvFor(declared, "production")).toEqual({ A: "p", B: "all" })
|
|
119
|
+
expect(publicEnvFor(declared, "staging")).toEqual({ A: "s", B: "all" })
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
it("leaves a name out of an environment its map does not name", () => {
|
|
123
|
+
expect(publicEnvFor({ A: { production: "p" } }, "staging")).toEqual({})
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
it("is empty for no declaration", () => {
|
|
127
|
+
expect(publicEnvFor(undefined, "production")).toEqual({})
|
|
128
|
+
})
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
describe("prefixedPublicEnv", () => {
|
|
132
|
+
it("puts each value under every framework's prefix", () => {
|
|
133
|
+
const out = prefixedPublicEnv({ MAPS_KEY: "m" })
|
|
134
|
+
for (const prefix of FRONTEND_PUBLIC_PREFIXES) expect(out[`${prefix}MAPS_KEY`]).toBe("m")
|
|
135
|
+
expect(Object.keys(out)).toHaveLength(FRONTEND_PUBLIC_PREFIXES.length)
|
|
136
|
+
})
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
describe("publicEnvRefusal", () => {
|
|
140
|
+
const problems = [{ name: "A", reason: "is bad" }, { name: "", reason: "too many" }]
|
|
141
|
+
it("says each problem by name, and the set's own as such", () => {
|
|
142
|
+
expect(publicEnvRefusal(problems)).toBe("A is bad; The public values: too many")
|
|
143
|
+
})
|
|
144
|
+
it("joins with the separator it is given", () => {
|
|
145
|
+
expect(publicEnvRefusal(problems, " | ")).toBe("A is bad | The public values: too many")
|
|
146
|
+
})
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
describe("where a value is set", () => {
|
|
150
|
+
it("is the config or Studio, each with its label", () => {
|
|
151
|
+
expect(PUBLIC_ENV_SOURCES).toEqual(["config", "studio"])
|
|
152
|
+
expect(PUBLIC_ENV_SOURCE_LABELS).toEqual({ config: "set in supatype.config.ts", studio: "set in Studio" })
|
|
153
|
+
})
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
describe("isPublicEnvEntry", () => {
|
|
157
|
+
it("accepts a name, a value and a source", () => {
|
|
158
|
+
expect(isPublicEnvEntry({ name: "A", value: "1", source: "config" })).toBe(true)
|
|
159
|
+
expect(isPublicEnvEntry({ name: "A", value: "1", source: "studio" })).toBe(true)
|
|
160
|
+
})
|
|
161
|
+
it.each([null, "A", [], {}, { name: "A", value: "1" }, { name: "A", value: 1, source: "config" }, { name: "A", value: "1", source: "env" }])("refuses %j", (value) => {
|
|
162
|
+
expect(isPublicEnvEntry(value)).toBe(false)
|
|
163
|
+
})
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
describe("ENVIRONMENT_NAME", () => {
|
|
167
|
+
it.each(["production", "staging", "a", "qa-1", "x_y"])("accepts %s", (name) => expect(ENVIRONMENT_NAME.test(name)).toBe(true))
|
|
168
|
+
it.each(["", "Prod", "-a", "a b", "a".repeat(64)])("refuses %j", (name) => expect(ENVIRONMENT_NAME.test(name)).toBe(false))
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
describe("isPublicEnvDeclaration", () => {
|
|
172
|
+
it("accepts a string for every environment and a map of environment to string", () => {
|
|
173
|
+
expect(isPublicEnvDeclaration({ A: "1", B: { production: "p", staging: "s" } })).toBe(true)
|
|
174
|
+
expect(isPublicEnvDeclaration({})).toBe(true)
|
|
175
|
+
})
|
|
176
|
+
it.each([null, "A=1", [], { A: 1 }, { A: null }, { A: { production: 1 } }, { A: { "Not Valid": "x" } }, { A: ["x"] }])("refuses %j", (value) => {
|
|
177
|
+
expect(isPublicEnvDeclaration(value)).toBe(false)
|
|
178
|
+
})
|
|
179
|
+
})
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest"
|
|
2
|
+
import {
|
|
3
|
+
REMOTE_SEED_FEATURE,
|
|
4
|
+
REMOTE_SEED_MAX_FILES,
|
|
5
|
+
REMOTE_SEED_MESSAGES,
|
|
6
|
+
REMOTE_SEED_REFUSALS,
|
|
7
|
+
formatRemoteSeedRefusal,
|
|
8
|
+
isRemoteSeedRefusal,
|
|
9
|
+
parseRemoteSeedRequest,
|
|
10
|
+
} from "../src/index.js"
|
|
11
|
+
|
|
12
|
+
const VALID = {
|
|
13
|
+
ast: { models: [] },
|
|
14
|
+
irDocuments: ['{"file":"seed.ts","operations":[]}'],
|
|
15
|
+
atomic: false,
|
|
16
|
+
runOnce: true,
|
|
17
|
+
confirmProduction: false,
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
describe("parseRemoteSeedRequest", () => {
|
|
21
|
+
it("accepts a well formed body and keeps the documents as the exact strings sent", () => {
|
|
22
|
+
const parsed = parseRemoteSeedRequest({ ...VALID, environment: "staging", pushedFrom: "ci" })
|
|
23
|
+
expect(parsed).toEqual({ ...VALID, environment: "staging", pushedFrom: "ci" })
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
it("leaves optional fields out when they were not sent", () => {
|
|
27
|
+
expect(parseRemoteSeedRequest(VALID)).toEqual(VALID)
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it.each([
|
|
31
|
+
["not an object", []],
|
|
32
|
+
["no ast", { ...VALID, ast: undefined }],
|
|
33
|
+
["no documents", { ...VALID, irDocuments: [] }],
|
|
34
|
+
["a document that is not a string", { ...VALID, irDocuments: [{ file: "seed.ts" }] }],
|
|
35
|
+
["a flag that is not a boolean", { ...VALID, confirmProduction: "yes" }],
|
|
36
|
+
["an empty environment", { ...VALID, environment: " " }],
|
|
37
|
+
["too many files", { ...VALID, irDocuments: Array.from({ length: REMOTE_SEED_MAX_FILES + 1 }, () => "{}") }],
|
|
38
|
+
])("refuses %s", (_label, body) => {
|
|
39
|
+
expect(parseRemoteSeedRequest(body)).toHaveProperty("invalid")
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
it("names the capability feature both control planes list", () => {
|
|
43
|
+
expect(REMOTE_SEED_FEATURE).toBe("seed")
|
|
44
|
+
})
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
describe("the remote seed message table", () => {
|
|
48
|
+
it("has one message for every refusal code", () => {
|
|
49
|
+
for (const code of REMOTE_SEED_REFUSALS) {
|
|
50
|
+
if (code === "validation_error") continue
|
|
51
|
+
expect(REMOTE_SEED_MESSAGES).toHaveProperty(code)
|
|
52
|
+
}
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
it("names the environment and the count in the schema_not_pushed message", () => {
|
|
56
|
+
const said = REMOTE_SEED_MESSAGES.schema_not_pushed({ environment: "staging", changes: 3 })
|
|
57
|
+
expect(said).toContain("staging")
|
|
58
|
+
expect(said).toContain("3 change(s)")
|
|
59
|
+
expect(said).toContain("supatype push --env staging")
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
it("names both environments in the environment_mismatch message", () => {
|
|
63
|
+
const said = REMOTE_SEED_MESSAGES.environment_mismatch({ requested: "staging", target: "production" })
|
|
64
|
+
expect(said).toContain('"staging"')
|
|
65
|
+
expect(said).toContain('"production"')
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
it("names the plan, the seed and the most that plan takes in the seed_too_large_for_plan message", () => {
|
|
69
|
+
const said = REMOTE_SEED_MESSAGES.seed_too_large_for_plan({ plan: "free", seedBytes: 9 * 1024 * 1024, maxBytes: 3 * 1024 * 1024 })
|
|
70
|
+
expect(said).toContain("free plan")
|
|
71
|
+
expect(said).toContain("9 MB")
|
|
72
|
+
expect(said).toContain("3 MB")
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
it("tells a non-production seed that staging and preview run at Free size, and does not say to move to a larger plan", () => {
|
|
76
|
+
const said = REMOTE_SEED_MESSAGES.seed_too_large_for_plan({ plan: "pro", seedBytes: 9 * 1024 * 1024, maxBytes: 8 * 1024 * 1024, nonProduction: true })
|
|
77
|
+
expect(said).toMatch(/staging and preview/i)
|
|
78
|
+
expect(said).toContain("Free size")
|
|
79
|
+
expect(said).toContain("9 MB")
|
|
80
|
+
expect(said).toContain("8 MB")
|
|
81
|
+
expect(said).toMatch(/seed production/i)
|
|
82
|
+
expect(said).not.toMatch(/larger plan/i)
|
|
83
|
+
expect(said).not.toMatch(/pro plan/i)
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
it("keeps the production seed_too_large_for_plan wording when nonProduction is false", () => {
|
|
87
|
+
const said = REMOTE_SEED_MESSAGES.seed_too_large_for_plan({ plan: "free", seedBytes: 9 * 1024 * 1024, maxBytes: 3 * 1024 * 1024, nonProduction: false })
|
|
88
|
+
expect(said).toContain("larger plan")
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
it("answers a full upload budget with a message that says to try again", () => {
|
|
92
|
+
expect(REMOTE_SEED_MESSAGES.server_busy()).toMatch(/try again|send .* again/i)
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
it("lists the codes a control plane that runs seeds as Jobs may refuse with", () => {
|
|
96
|
+
expect(REMOTE_SEED_REFUSALS).toContain("seed_too_large_for_plan")
|
|
97
|
+
expect(REMOTE_SEED_REFUSALS).toContain("server_busy")
|
|
98
|
+
expect(isRemoteSeedRefusal("server_busy")).toBe(true)
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
it("prints a refusal as [code] message", () => {
|
|
102
|
+
expect(formatRemoteSeedRefusal("seed_busy", "Another seed is running.")).toBe("[seed_busy] Another seed is running.")
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
it("tells a refusal code from any other text", () => {
|
|
106
|
+
expect(isRemoteSeedRefusal("seed_busy")).toBe(true)
|
|
107
|
+
expect(isRemoteSeedRefusal("forbidden")).toBe(false)
|
|
108
|
+
expect(isRemoteSeedRefusal(undefined)).toBe(false)
|
|
109
|
+
})
|
|
110
|
+
})
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process"
|
|
2
|
+
import { mkdtempSync, rmSync, writeFileSync } from "node:fs"
|
|
3
|
+
import { createRequire } from "node:module"
|
|
4
|
+
import { tmpdir } from "node:os"
|
|
5
|
+
import { dirname, join, resolve } from "node:path"
|
|
6
|
+
import { fileURLToPath } from "node:url"
|
|
7
|
+
import { describe, expect, it } from "vitest"
|
|
8
|
+
|
|
9
|
+
const here = dirname(fileURLToPath(import.meta.url))
|
|
10
|
+
const packageTsconfig = resolve(here, "..", "tsconfig.json")
|
|
11
|
+
const tsc = createRequire(import.meta.url).resolve("typescript/bin/tsc")
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The Cloud control plane image builds this package in a bare directory with no node_modules, so no
|
|
15
|
+
* `@types/node`. A name the package takes from the Node typings alone then fails that build with
|
|
16
|
+
* TS2304, while every build inside the monorepo, where the typings are hoisted, passes.
|
|
17
|
+
*/
|
|
18
|
+
describe("@supatype/types builds with no ambient typings", () => {
|
|
19
|
+
it("typechecks with `types` empty", () => {
|
|
20
|
+
const dir = mkdtempSync(join(tmpdir(), "types-standalone-"))
|
|
21
|
+
try {
|
|
22
|
+
const config = join(dir, "tsconfig.json")
|
|
23
|
+
writeFileSync(config, JSON.stringify({ extends: packageTsconfig, compilerOptions: { types: [], noEmit: true, composite: false, incremental: false }, files: [resolve(here, "..", "src", "index.ts")] }))
|
|
24
|
+
let output = ""
|
|
25
|
+
try {
|
|
26
|
+
execFileSync(process.execPath, [tsc, "--project", config], { encoding: "utf8", stdio: "pipe" })
|
|
27
|
+
} catch (error) {
|
|
28
|
+
output = error instanceof Error && "stdout" in error ? String(error.stdout) : String(error)
|
|
29
|
+
}
|
|
30
|
+
expect(output).toBe("")
|
|
31
|
+
} finally {
|
|
32
|
+
rmSync(dir, { recursive: true, force: true })
|
|
33
|
+
}
|
|
34
|
+
}, 120_000)
|
|
35
|
+
})
|