Skip to content

Commit

Permalink
test: add rule tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chronark committed Jan 9, 2023
1 parent 25958c9 commit 1dd5c6b
Show file tree
Hide file tree
Showing 7 changed files with 413 additions and 65 deletions.
4 changes: 2 additions & 2 deletions examples/nextjs/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export default function Example() {
<Text>ms</Text>
</Flex>
) : (
""
"-"
)}
</Block>
</Card>
Expand All @@ -149,7 +149,7 @@ export default function Example() {
<Text>ms</Text>
</Flex>
) : (
""
"-"
)}
</Block>
</Card>
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"author": "Andreas Thomas <[email protected]>",
"scripts": {
"build": "tsup",
"test": "jest"
"test": "jest --collect-coverage"
},
"devDependencies": {
"@faker-js/faker": "^7.6.0",
Expand Down
46 changes: 2 additions & 44 deletions packages/sdk/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,5 @@
import { useState } from "react";

export type EdgeFlagsConfig = {
/**
* The full to the edge function.
* TODO:
* @default "/api/edge-flags"
*/
url?: string;
};

// export class EdgeFlagsServerComponent {
// private url: URL;
// constructor(config?: EdgeFlagsConfig) {
// const host = process.env.VERCEL_URL ?? process.env.NEXT_PUBLIC_VERCEL_URL

// const baseUrl = host ? `https://${host}` : "http://localhost:3000"
// this.url = new URL("/api/edge-flags", baseUrl)
// }

// private async eval(flagName: string): Promise<boolean> {
// const url = new URL(this.url)
// params.set("flag", flagName)
// console.log({url})
// const res = await fetch(url)
// if (res.status !== 200) {
// throw new Error(await res.text())
// }
// const { value } = await res.json() as { value: boolean }
// return value

// }

// /**
// * Check if a boolean feature flag is enabled or not
// */
// public async isEnabled(flagName: string): Promise<boolean> {

// return await this.eval(flagName);
// }
// }

export type UseFlag = {
isLoading: boolean;
error: string | null;
Expand All @@ -64,7 +24,7 @@ export type UseFlag = {
};
};

export function useFlag(flag: string, attributes?: Record<string, string>): UseFlag {
export function useFlag(flag: string, attributes?: Record<string, string | number | boolean>): UseFlag {
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const [isEnabled, setIsEnabled] = useState<boolean | null>(null);
Expand All @@ -88,17 +48,15 @@ export function useFlag(flag: string, attributes?: Record<string, string>): UseF
attributes["_flag"] = flag;
if (attributes) {
for (const [k, v] of Object.entries(attributes)) {
params.set(k, v);
params.set(k, v.toString());
}
}
const res = await fetch(`/api/edge-flags?${params.toString()}`);
if (!res.ok) {
setError(await res.text());
return;
}
console.log(res.headers);
const json = (await res.json()) as { value: boolean };
console.log({ json });
setVercelCacheHit(res.headers.get("X-Vercel-Cache"));
setMemoryCacheHit(res.headers.get("X-Edge-Flags-Cache"));
setRedisLatency(parseInt(res.headers.get("X-Redis-Latency") ?? "-1"));
Expand Down
7 changes: 3 additions & 4 deletions packages/sdk/src/evaluation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ export function evaluate(flag: Flag, userPercentage: number, req: EvalRequest, d
if (debug) console.log(JSON.stringify({ evalRequest: req.eval }, null, 2));

for (const rule of flag.rules) {
const hit = new Rule(rule).evaluate(req);
if (debug) {
console.log("evaluating rule", rule, { hit });
}
const hit = new Rule(rule).match(req);
if (debug) console.log("matching rule", rule, { hit });

if (hit) {
if (debug) console.log("Returning", rule.value);

Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/flag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { rule } from "./rules";
export const flag = z.object({
// The name must be unique per tenant but is shared across environments
name: z.string(),
version: z.string(),
version: z.enum(["v1"]),
enabled: z.boolean(),
rules: z.array(rule),
environment: z.enum(["development", "preview", "production"]),
Expand Down
Loading

1 comment on commit 1dd5c6b

@vercel
Copy link

@vercel vercel bot commented on 1dd5c6b Jan 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.