@vllnt/convex-flags 0.1.0 → 0.1.1-canary.4efa5b8
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/CHANGELOG.md +113 -0
- package/README.md +512 -114
- package/dist/client/index.d.ts +2 -0
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +2 -0
- package/dist/client/index.js.map +1 -1
- package/dist/component/convex.config.d.ts +1 -0
- package/dist/component/convex.config.d.ts.map +1 -1
- package/dist/component/convex.config.js +1 -0
- package/dist/component/convex.config.js.map +1 -1
- package/dist/component/mutations.d.ts.map +1 -1
- package/dist/component/mutations.js +77 -23
- package/dist/component/mutations.js.map +1 -1
- package/dist/shared.d.ts +10 -0
- package/dist/shared.d.ts.map +1 -1
- package/dist/shared.js +17 -1
- package/dist/shared.js.map +1 -1
- package/docs/API.md +593 -0
- package/llms.txt +74 -0
- package/package.json +53 -28
- package/src/client/index.ts +2 -0
- package/src/component/convex.config.ts +1 -0
- package/src/component/mutations.ts +107 -28
- package/src/shared.ts +19 -1
- package/src/react/index.test.ts +0 -52
- package/src/status-backward-compat.test.ts +0 -51
package/package.json
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vllnt/convex-flags",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.1.1-canary.4efa5b8",
|
|
4
|
+
"description": "Convex component for feature flags in TypeScript: kill switches, variants, attribute targeting, percentage rollouts, per-subject overrides and React hooks.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
|
-
"packageManager": "pnpm@
|
|
7
|
+
"packageManager": "pnpm@10.34.8",
|
|
8
8
|
"files": [
|
|
9
9
|
"dist",
|
|
10
|
-
"src"
|
|
10
|
+
"src",
|
|
11
|
+
"docs/API.md",
|
|
12
|
+
"CHANGELOG.md",
|
|
13
|
+
"llms.txt",
|
|
14
|
+
"!src/**/*.test.*",
|
|
15
|
+
"!src/**/*.spec.*"
|
|
11
16
|
],
|
|
12
17
|
"exports": {
|
|
13
18
|
"./package.json": "./package.json",
|
|
@@ -41,17 +46,15 @@
|
|
|
41
46
|
"build": "tsc --project ./tsconfig.build.json",
|
|
42
47
|
"build:codegen": "pnpm codegen && pnpm build",
|
|
43
48
|
"build:clean": "rm -rf dist *.tsbuildinfo && pnpm build:codegen",
|
|
44
|
-
"typecheck": "tsc --noEmit",
|
|
45
|
-
"typecheck:ci": "tsc --noEmit --project tsconfig.ci.json",
|
|
46
|
-
"
|
|
49
|
+
"typecheck": "tsc --noEmit --incremental --tsBuildInfoFile node_modules/.cache/tsc/typecheck.tsbuildinfo",
|
|
50
|
+
"typecheck:ci": "tsc --noEmit --incremental --tsBuildInfoFile node_modules/.cache/tsc/typecheck-ci.tsbuildinfo --project tsconfig.ci.json",
|
|
51
|
+
"typecheck:test": "tsc --noEmit --incremental --tsBuildInfoFile node_modules/.cache/tsc/typecheck-test.tsbuildinfo --project tsconfig.test.json",
|
|
52
|
+
"lint": "pnpm check:agents && eslint --cache --cache-location node_modules/.cache/eslint/ .",
|
|
47
53
|
"test": "vitest run --passWithNoTests",
|
|
48
54
|
"test:watch": "vitest --typecheck --clearScreen false",
|
|
49
55
|
"test:coverage": "vitest run --coverage --coverage.reporter=text",
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
"prepublishOnly": "npm whoami || npm login",
|
|
53
|
-
"alpha": "npm version prerelease --preid alpha && npm publish --tag alpha && git push --follow-tags",
|
|
54
|
-
"release": "npm version patch && npm publish && git push --follow-tags",
|
|
56
|
+
"preversion": "pnpm install --frozen-lockfile && pnpm build && pnpm test:coverage && pnpm typecheck",
|
|
57
|
+
"test:package": "node scripts/test-package.mjs",
|
|
55
58
|
"check:agents": "node -e \"const f=require('node:fs');if(!f.existsSync('AGENTS.md')||f.existsSync('CLAUDE.md')||f.existsSync('.claude'))process.exit(1)\"",
|
|
56
59
|
"codegen": "pnpm convex codegen --component-dir ./src/component"
|
|
57
60
|
},
|
|
@@ -67,24 +70,38 @@
|
|
|
67
70
|
"devDependencies": {
|
|
68
71
|
"@edge-runtime/vm": "^5.0.0",
|
|
69
72
|
"@testing-library/react": "^16.3.3",
|
|
70
|
-
"@types/react": "^19.
|
|
71
|
-
"@types/react-dom": "^19.
|
|
73
|
+
"@types/react": "^19.3.0",
|
|
74
|
+
"@types/react-dom": "^19.3.0",
|
|
72
75
|
"@typescript/native": "npm:typescript@7.0.2",
|
|
73
|
-
"@vitest/coverage-v8": "^
|
|
74
|
-
"@vllnt/eslint-config": "2.
|
|
75
|
-
"@vllnt/typescript": "
|
|
76
|
-
"convex": "^1.
|
|
77
|
-
"convex-test": "^0.0.
|
|
76
|
+
"@vitest/coverage-v8": "^5.1.1",
|
|
77
|
+
"@vllnt/eslint-config": "2.1.0",
|
|
78
|
+
"@vllnt/typescript": "3.0.0",
|
|
79
|
+
"convex": "^1.51.0",
|
|
80
|
+
"convex-test": "^0.0.73",
|
|
78
81
|
"eslint": "^9.39.5",
|
|
79
|
-
"jsdom": "^
|
|
80
|
-
"prettier": "^3.
|
|
81
|
-
"react": "^19.
|
|
82
|
-
"react-dom": "^19.
|
|
82
|
+
"jsdom": "^31.0.1",
|
|
83
|
+
"prettier": "^3.10.1",
|
|
84
|
+
"react": "^19.3.1",
|
|
85
|
+
"react-dom": "^19.3.1",
|
|
83
86
|
"typescript": "npm:@typescript/typescript6@^6.0.2",
|
|
84
|
-
"vite": "^8.
|
|
85
|
-
"vitest": "^
|
|
87
|
+
"vite": "^8.3.8",
|
|
88
|
+
"vitest": "^5.1.1"
|
|
89
|
+
},
|
|
90
|
+
"pnpm": {
|
|
91
|
+
"minimumReleaseAge": 10080,
|
|
92
|
+
"minimumReleaseAgeExclude": [
|
|
93
|
+
"@vllnt/*"
|
|
94
|
+
],
|
|
95
|
+
"ignoredBuiltDependencies": [
|
|
96
|
+
"esbuild"
|
|
97
|
+
],
|
|
98
|
+
"overrides": {
|
|
99
|
+
"minimatch@>=7.0.0 <11.0.1": "11.0.5",
|
|
100
|
+
"deepmerge-ts@<8.0.0": "8.0.0",
|
|
101
|
+
"handlebars@<4.7.10": "4.7.10"
|
|
102
|
+
}
|
|
86
103
|
},
|
|
87
|
-
"homepage": "https://
|
|
104
|
+
"homepage": "https://oss.vllnt.com/packages/convex-flags",
|
|
88
105
|
"bugs": {
|
|
89
106
|
"url": "https://github.com/vllnt/convex-flags/issues"
|
|
90
107
|
},
|
|
@@ -101,7 +118,7 @@
|
|
|
101
118
|
"url": "https://github.com/vllnt/convex-flags"
|
|
102
119
|
},
|
|
103
120
|
"engines": {
|
|
104
|
-
"node": ">=
|
|
121
|
+
"node": ">=20"
|
|
105
122
|
},
|
|
106
123
|
"publishConfig": {
|
|
107
124
|
"access": "public"
|
|
@@ -110,6 +127,14 @@
|
|
|
110
127
|
"keywords": [
|
|
111
128
|
"convex",
|
|
112
129
|
"convex-component",
|
|
113
|
-
"
|
|
130
|
+
"typescript",
|
|
131
|
+
"react",
|
|
132
|
+
"feature-flags",
|
|
133
|
+
"feature-toggles",
|
|
134
|
+
"kill-switch",
|
|
135
|
+
"rollout",
|
|
136
|
+
"percentage-rollout",
|
|
137
|
+
"targeting",
|
|
138
|
+
"variants"
|
|
114
139
|
]
|
|
115
140
|
}
|
package/src/client/index.ts
CHANGED
|
@@ -110,6 +110,8 @@ interface RunMutationCtx {
|
|
|
110
110
|
* const flags = new Flags(components.flags);
|
|
111
111
|
* if (await flags.isEnabled(ctx, "new-checkout", { subjectRef: userId })) { ... }
|
|
112
112
|
* ```
|
|
113
|
+
*
|
|
114
|
+
* @see https://oss.vllnt.com/packages/convex-flags
|
|
113
115
|
*/
|
|
114
116
|
export class Flags {
|
|
115
117
|
constructor(private readonly component: FlagsComponent) {}
|
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
import { v } from "convex/values";
|
|
1
|
+
import { ConvexError, v, type Infer } from "convex/values";
|
|
2
2
|
import { mutation } from "./_generated/server.js";
|
|
3
3
|
import type { Doc } from "./_generated/dataModel.js";
|
|
4
4
|
import type { MutationCtx } from "./_generated/server.js";
|
|
5
5
|
import { rollout, rule, variant, variantValue } from "./validators.js";
|
|
6
6
|
|
|
7
|
+
type Rollout = Infer<typeof rollout>;
|
|
8
|
+
type Rule = Infer<typeof rule>;
|
|
9
|
+
|
|
7
10
|
async function findByKey(ctx: MutationCtx, key: string): Promise<Doc<"flags"> | null> {
|
|
8
11
|
return await ctx.db
|
|
9
12
|
.query("flags")
|
|
@@ -11,6 +14,76 @@ async function findByKey(ctx: MutationCtx, key: string): Promise<Doc<"flags"> |
|
|
|
11
14
|
.unique();
|
|
12
15
|
}
|
|
13
16
|
|
|
17
|
+
function invalidArgument(message: string): never {
|
|
18
|
+
throw new ConvexError({ code: "INVALID_ARGUMENT", message });
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Reject empty identifiers: an empty key or subject ref can never be targeted
|
|
22
|
+
* meaningfully and would store an unreachable row. */
|
|
23
|
+
function assertNonEmpty(fields: Record<string, string>): void {
|
|
24
|
+
for (const [name, value] of Object.entries(fields)) {
|
|
25
|
+
if (value === "") {
|
|
26
|
+
invalidArgument(`${name} must be non-empty`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Reject rollout weights that cannot bucket: NaN, infinite, or negative. Zero
|
|
32
|
+
* is allowed (it excludes a split). */
|
|
33
|
+
function assertRollouts(
|
|
34
|
+
rollouts: Array<Rollout | undefined>,
|
|
35
|
+
rules: Rule[] | undefined,
|
|
36
|
+
): void {
|
|
37
|
+
const all = [...rollouts, ...(rules ?? []).map((r) => r.rollout)];
|
|
38
|
+
for (const r of all) {
|
|
39
|
+
for (const { weight } of r?.splits ?? []) {
|
|
40
|
+
if (!Number.isFinite(weight) || weight < 0) {
|
|
41
|
+
invalidArgument("rollout weights must be finite and non-negative");
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function sameValue(a: unknown, b: unknown): boolean {
|
|
48
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
49
|
+
return a.length === b.length && a.every((item, i) => sameValue(item, b[i]));
|
|
50
|
+
}
|
|
51
|
+
if (isRecord(a) && isRecord(b)) {
|
|
52
|
+
const keys = new Set([...Object.keys(a), ...Object.keys(b)]);
|
|
53
|
+
return [...keys].every((k) => sameValue(a[k], b[k]));
|
|
54
|
+
}
|
|
55
|
+
return Object.is(a, b);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
59
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const FLAG_FIELDS = ["value", "description", "variants", "rules", "rollout"] as const;
|
|
63
|
+
|
|
64
|
+
function changedFields(
|
|
65
|
+
flag: Doc<"flags">,
|
|
66
|
+
fields: Partial<Pick<Doc<"flags">, "value" | "description" | "variants" | "rules" | "rollout">>,
|
|
67
|
+
): Partial<Doc<"flags">> {
|
|
68
|
+
const patch: Partial<Doc<"flags">> = {};
|
|
69
|
+
for (const name of FLAG_FIELDS) {
|
|
70
|
+
if (name in fields && !sameValue(flag[name], fields[name])) {
|
|
71
|
+
Object.assign(patch, { [name]: fields[name] });
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return patch;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async function patchIfChanged(
|
|
78
|
+
ctx: MutationCtx,
|
|
79
|
+
flag: Doc<"flags">,
|
|
80
|
+
patch: Partial<Doc<"flags">>,
|
|
81
|
+
): Promise<void> {
|
|
82
|
+
if (Object.keys(patch).length > 0) {
|
|
83
|
+
await ctx.db.patch("flags", flag._id, { ...patch, updatedAt: Date.now() });
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
14
87
|
/** Throws the component's single code-tagged error. Returns `never` so callers
|
|
15
88
|
* narrow the flag to non-null after the guard. */
|
|
16
89
|
function flagNotFound(): never {
|
|
@@ -32,17 +105,22 @@ export const define = mutation({
|
|
|
32
105
|
},
|
|
33
106
|
returns: v.id("flags"),
|
|
34
107
|
handler: async (ctx, args) => {
|
|
108
|
+
assertNonEmpty({ key: args.key });
|
|
109
|
+
assertRollouts([args.rollout], args.rules);
|
|
35
110
|
const now = Date.now();
|
|
36
111
|
const existing = await findByKey(ctx, args.key);
|
|
37
112
|
if (existing !== null) {
|
|
38
|
-
await
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
113
|
+
await patchIfChanged(
|
|
114
|
+
ctx,
|
|
115
|
+
existing,
|
|
116
|
+
changedFields(existing, {
|
|
117
|
+
value: args.value,
|
|
118
|
+
description: args.description,
|
|
119
|
+
variants: args.variants,
|
|
120
|
+
rules: args.rules,
|
|
121
|
+
rollout: args.rollout,
|
|
122
|
+
}),
|
|
123
|
+
);
|
|
46
124
|
return existing._id;
|
|
47
125
|
}
|
|
48
126
|
return await ctx.db.insert("flags", {
|
|
@@ -75,37 +153,30 @@ export const update = mutation({
|
|
|
75
153
|
},
|
|
76
154
|
returns: v.null(),
|
|
77
155
|
handler: async (ctx, args) => {
|
|
156
|
+
assertNonEmpty({ key: args.key });
|
|
157
|
+
assertRollouts([args.rollout], args.rules);
|
|
78
158
|
const flag = await findByKey(ctx, args.key);
|
|
79
159
|
if (flag === null) {
|
|
80
160
|
flagNotFound();
|
|
81
161
|
}
|
|
82
|
-
const
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
patch.description = args.description;
|
|
162
|
+
const fields: Parameters<typeof changedFields>[1] = {};
|
|
163
|
+
for (const name of FLAG_FIELDS) {
|
|
164
|
+
if (args[name] !== undefined) {
|
|
165
|
+
Object.assign(fields, { [name]: args[name] });
|
|
166
|
+
}
|
|
88
167
|
}
|
|
89
|
-
|
|
90
|
-
patch.variants = args.variants;
|
|
91
|
-
}
|
|
92
|
-
if (args.rules !== undefined) {
|
|
93
|
-
patch.rules = args.rules;
|
|
94
|
-
}
|
|
95
|
-
if (args.rollout !== undefined) {
|
|
96
|
-
patch.rollout = args.rollout;
|
|
97
|
-
}
|
|
98
|
-
await ctx.db.patch("flags", flag._id, patch);
|
|
168
|
+
await patchIfChanged(ctx, flag, changedFields(flag, fields));
|
|
99
169
|
return null;
|
|
100
170
|
},
|
|
101
171
|
});
|
|
102
172
|
|
|
103
173
|
async function setValue(ctx: MutationCtx, key: string, value: boolean): Promise<void> {
|
|
174
|
+
assertNonEmpty({ key });
|
|
104
175
|
const flag = await findByKey(ctx, key);
|
|
105
176
|
if (flag === null) {
|
|
106
177
|
flagNotFound();
|
|
107
178
|
}
|
|
108
|
-
await ctx
|
|
179
|
+
await patchIfChanged(ctx, flag, changedFields(flag, { value }));
|
|
109
180
|
}
|
|
110
181
|
|
|
111
182
|
/** Turn a boolean flag on (set its value to `true`). */
|
|
@@ -133,11 +204,14 @@ async function setStatus(
|
|
|
133
204
|
key: string,
|
|
134
205
|
status: "active" | "archived",
|
|
135
206
|
): Promise<void> {
|
|
207
|
+
assertNonEmpty({ key });
|
|
136
208
|
const flag = await findByKey(ctx, key);
|
|
137
209
|
if (flag === null) {
|
|
138
210
|
flagNotFound();
|
|
139
211
|
}
|
|
140
|
-
|
|
212
|
+
if (flag.status !== status) {
|
|
213
|
+
await ctx.db.patch("flags", flag._id, { status, updatedAt: Date.now() });
|
|
214
|
+
}
|
|
141
215
|
}
|
|
142
216
|
|
|
143
217
|
/** Archive a flag: reversible retirement. Evaluation skips targeting and serves
|
|
@@ -166,6 +240,7 @@ export const remove = mutation({
|
|
|
166
240
|
args: { key: v.string() },
|
|
167
241
|
returns: v.null(),
|
|
168
242
|
handler: async (ctx, args) => {
|
|
243
|
+
assertNonEmpty({ key: args.key });
|
|
169
244
|
const flag = await findByKey(ctx, args.key);
|
|
170
245
|
if (flag === null) {
|
|
171
246
|
flagNotFound();
|
|
@@ -187,6 +262,7 @@ export const setOverride = mutation({
|
|
|
187
262
|
args: { key: v.string(), subjectRef: v.string(), value: variantValue },
|
|
188
263
|
returns: v.null(),
|
|
189
264
|
handler: async (ctx, args) => {
|
|
265
|
+
assertNonEmpty({ key: args.key, subjectRef: args.subjectRef });
|
|
190
266
|
const flag = await findByKey(ctx, args.key);
|
|
191
267
|
if (flag === null) {
|
|
192
268
|
flagNotFound();
|
|
@@ -198,7 +274,9 @@ export const setOverride = mutation({
|
|
|
198
274
|
)
|
|
199
275
|
.unique();
|
|
200
276
|
if (existing !== null) {
|
|
201
|
-
|
|
277
|
+
if (!Object.is(existing.value, args.value)) {
|
|
278
|
+
await ctx.db.patch("overrides", existing._id, { value: args.value });
|
|
279
|
+
}
|
|
202
280
|
} else {
|
|
203
281
|
await ctx.db.insert("overrides", {
|
|
204
282
|
key: args.key,
|
|
@@ -216,6 +294,7 @@ export const clearOverride = mutation({
|
|
|
216
294
|
args: { key: v.string(), subjectRef: v.string() },
|
|
217
295
|
returns: v.null(),
|
|
218
296
|
handler: async (ctx, args) => {
|
|
297
|
+
assertNonEmpty({ key: args.key, subjectRef: args.subjectRef });
|
|
219
298
|
const existing = await ctx.db
|
|
220
299
|
.query("overrides")
|
|
221
300
|
.withIndex("by_key_subject", (q) =>
|
package/src/shared.ts
CHANGED
|
@@ -165,9 +165,22 @@ export function hashString(input: string): number {
|
|
|
165
165
|
return hash >>> 0;
|
|
166
166
|
}
|
|
167
167
|
|
|
168
|
+
/** Bucket count used when a rollout has a fractional weight. */
|
|
169
|
+
const FRACTIONAL_RESOLUTION = 1_000_000;
|
|
170
|
+
|
|
168
171
|
/**
|
|
169
172
|
* Deterministically pick a split's value by hashing `flagKey:subject` across the
|
|
170
173
|
* cumulative weights. Returns `null` when no split has positive weight.
|
|
174
|
+
*
|
|
175
|
+
* All-integer weights bucket by `hash % total` (the original scheme, kept so
|
|
176
|
+
* existing assignments never move). An infinite total, from a legacy `Infinity`
|
|
177
|
+
* weight stored before weights were validated, also takes that path, even beside
|
|
178
|
+
* a decimal weight, so it keeps its 0.1.0 result. Otherwise any fractional
|
|
179
|
+
* weight maps the hash to one of `FRACTIONAL_RESOLUTION` slots scaled into
|
|
180
|
+
* `[0, total)` instead: `hash % total` would send every subject to the first
|
|
181
|
+
* split when, say, `0.5 + 0.5` sums to `1`.
|
|
182
|
+
* A modulo (not `hash / 2 ** 32`) keeps subjects that differ only in their last
|
|
183
|
+
* characters spread out, as FNV-1a's high bits barely move for them.
|
|
171
184
|
*/
|
|
172
185
|
export function pickSplit(
|
|
173
186
|
flagKey: string,
|
|
@@ -179,7 +192,12 @@ export function pickSplit(
|
|
|
179
192
|
if (total === 0) {
|
|
180
193
|
return null;
|
|
181
194
|
}
|
|
182
|
-
const
|
|
195
|
+
const hash = hashString(`${flagKey}:${subject}`);
|
|
196
|
+
const integral =
|
|
197
|
+
!Number.isFinite(total) || positive.every((split) => Number.isInteger(split.weight));
|
|
198
|
+
const point = integral
|
|
199
|
+
? hash % total
|
|
200
|
+
: ((hash % FRACTIONAL_RESOLUTION) / FRACTIONAL_RESOLUTION) * total;
|
|
183
201
|
let cumulative = 0;
|
|
184
202
|
let chosen: VariantValue | null = null;
|
|
185
203
|
for (const split of positive) {
|
package/src/react/index.test.ts
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
// @vitest-environment jsdom
|
|
2
|
-
import { renderHook } from "@testing-library/react";
|
|
3
|
-
import { makeFunctionReference } from "convex/server";
|
|
4
|
-
import { beforeEach, expect, test, vi } from "vitest";
|
|
5
|
-
import { useFlag, useFlags, type AllRef, type EvaluateRef } from "./index.js";
|
|
6
|
-
|
|
7
|
-
const { useQuerySpy } = vi.hoisted(() => ({ useQuerySpy: vi.fn() }));
|
|
8
|
-
vi.mock("convex/react", () => ({ useQuery: useQuerySpy }));
|
|
9
|
-
|
|
10
|
-
const evalRef: EvaluateRef = makeFunctionReference<"query">("flags:evaluate");
|
|
11
|
-
const allRef: AllRef = makeFunctionReference<"query">("flags:all");
|
|
12
|
-
|
|
13
|
-
beforeEach(() => {
|
|
14
|
-
useQuerySpy.mockReset();
|
|
15
|
-
useQuerySpy.mockReturnValue({ value: true, reason: "flag" });
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
test("useFlag forwards key + context + default and returns the query result", () => {
|
|
19
|
-
const { result } = renderHook(() =>
|
|
20
|
-
useFlag(evalRef, "new-checkout", {
|
|
21
|
-
context: { subjectRef: "u1", attributes: { plan: "pro" } },
|
|
22
|
-
default: "off",
|
|
23
|
-
}),
|
|
24
|
-
);
|
|
25
|
-
expect(useQuerySpy).toHaveBeenCalledWith(evalRef, {
|
|
26
|
-
key: "new-checkout",
|
|
27
|
-
context: { subjectRef: "u1", attributes: { plan: "pro" } },
|
|
28
|
-
default: "off",
|
|
29
|
-
});
|
|
30
|
-
expect(result.current).toEqual({ value: true, reason: "flag" });
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
test("useFlag works with no options", () => {
|
|
34
|
-
renderHook(() => useFlag(evalRef, "k"));
|
|
35
|
-
expect(useQuerySpy).toHaveBeenCalledWith(evalRef, {
|
|
36
|
-
key: "k",
|
|
37
|
-
context: undefined,
|
|
38
|
-
default: undefined,
|
|
39
|
-
});
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
test("useFlags forwards context", () => {
|
|
43
|
-
useQuerySpy.mockReturnValue({ k: { value: true, reason: "flag" } });
|
|
44
|
-
const { result } = renderHook(() => useFlags(allRef, { subjectRef: "u1" }));
|
|
45
|
-
expect(useQuerySpy).toHaveBeenCalledWith(allRef, { context: { subjectRef: "u1" } });
|
|
46
|
-
expect(result.current).toEqual({ k: { value: true, reason: "flag" } });
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
test("useFlags works with no context", () => {
|
|
50
|
-
renderHook(() => useFlags(allRef));
|
|
51
|
-
expect(useQuerySpy).toHaveBeenCalledWith(allRef, { context: undefined });
|
|
52
|
-
});
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from "vitest";
|
|
2
|
-
import { flagFields } from "./component/validators.js";
|
|
3
|
-
import { evaluateFlag, withStatus } from "./shared.js";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Regression guard for #5 — a required `status` field broke deploys for any
|
|
7
|
-
* consumer with flag rows written before `status` existed. The expand fix makes
|
|
8
|
-
* `status` optional and reads an absent status as `"active"`.
|
|
9
|
-
*/
|
|
10
|
-
describe("status backward compatibility (#5)", () => {
|
|
11
|
-
test("the schema validator accepts a row without `status` (legacy data)", () => {
|
|
12
|
-
expect(flagFields.status.isOptional).toBe("optional");
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
test("a flag with no `status` is evaluated as active, not disabled", () => {
|
|
16
|
-
expect(evaluateFlag("legacy", { value: true }, {})).toEqual({
|
|
17
|
-
value: true,
|
|
18
|
-
reason: "flag",
|
|
19
|
-
});
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
test("a status-less flag still runs targeting (absent status !== archived)", () => {
|
|
23
|
-
expect(
|
|
24
|
-
evaluateFlag(
|
|
25
|
-
"legacy",
|
|
26
|
-
{ value: "BASE", rules: [{ conditions: [], value: "RULE" }] },
|
|
27
|
-
{},
|
|
28
|
-
),
|
|
29
|
-
).toEqual({ value: "RULE", reason: "rule" });
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
test("an explicitly archived flag is still disabled", () => {
|
|
33
|
-
expect(evaluateFlag("k", { value: "BASE", status: "archived" }, {})).toEqual({
|
|
34
|
-
value: "BASE",
|
|
35
|
-
reason: "disabled",
|
|
36
|
-
});
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
test("withStatus materializes an absent status as active for returned docs", () => {
|
|
40
|
-
const legacyRow: { key: string; value: boolean; status?: "active" | "archived" } = {
|
|
41
|
-
key: "legacy",
|
|
42
|
-
value: true,
|
|
43
|
-
};
|
|
44
|
-
expect(withStatus(legacyRow).status).toBe("active");
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
test("withStatus preserves an explicit status", () => {
|
|
48
|
-
const archivedRow = { key: "k", value: true, status: "archived" as const };
|
|
49
|
-
expect(withStatus(archivedRow).status).toBe("archived");
|
|
50
|
-
});
|
|
51
|
-
});
|