@gaganref/confect-test 10.3.1-fork.88 → 10.3.1-fork.89

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.
Files changed (46) hide show
  1. package/dist/internal/Attempt.d.ts +77 -0
  2. package/dist/internal/Attempt.d.ts.map +1 -0
  3. package/dist/internal/Attempt.js +154 -0
  4. package/dist/internal/Attempt.js.map +1 -0
  5. package/dist/internal/ConvexTest.d.ts +34 -0
  6. package/dist/internal/ConvexTest.d.ts.map +1 -0
  7. package/dist/internal/ConvexTest.js +59 -0
  8. package/dist/internal/ConvexTest.js.map +1 -0
  9. package/dist/internal/Execution.d.ts +167 -0
  10. package/dist/internal/Execution.d.ts.map +1 -0
  11. package/dist/internal/Execution.js +790 -0
  12. package/dist/internal/Execution.js.map +1 -0
  13. package/dist/internal/Globals.d.ts +11 -0
  14. package/dist/internal/Globals.d.ts.map +1 -0
  15. package/dist/internal/Globals.js +76 -0
  16. package/dist/internal/Globals.js.map +1 -0
  17. package/dist/internal/Kernel.d.ts +93 -0
  18. package/dist/internal/Kernel.d.ts.map +1 -0
  19. package/dist/internal/Kernel.js +91 -0
  20. package/dist/internal/Kernel.js.map +1 -0
  21. package/dist/internal/KernelQuery.d.ts +55 -0
  22. package/dist/internal/KernelQuery.d.ts.map +1 -0
  23. package/dist/internal/KernelQuery.js +205 -0
  24. package/dist/internal/KernelQuery.js.map +1 -0
  25. package/dist/internal/KernelRefusal.d.ts +77 -0
  26. package/dist/internal/KernelRefusal.d.ts.map +1 -0
  27. package/dist/internal/KernelRefusal.js +73 -0
  28. package/dist/internal/KernelRefusal.js.map +1 -0
  29. package/dist/internal/KernelValues.d.ts +136 -0
  30. package/dist/internal/KernelValues.d.ts.map +1 -0
  31. package/dist/internal/KernelValues.js +266 -0
  32. package/dist/internal/KernelValues.js.map +1 -0
  33. package/dist/internal/ReadSet.d.ts +55 -0
  34. package/dist/internal/ReadSet.d.ts.map +1 -0
  35. package/dist/internal/ReadSet.js +29 -0
  36. package/dist/internal/ReadSet.js.map +1 -0
  37. package/package.json +3 -3
  38. package/src/internal/Attempt.ts +185 -0
  39. package/src/internal/ConvexTest.ts +118 -0
  40. package/src/internal/Execution.ts +1430 -0
  41. package/src/internal/Globals.ts +93 -0
  42. package/src/internal/Kernel.ts +190 -0
  43. package/src/internal/KernelQuery.ts +405 -0
  44. package/src/internal/KernelRefusal.ts +88 -0
  45. package/src/internal/KernelValues.ts +725 -0
  46. package/src/internal/ReadSet.ts +108 -0
@@ -0,0 +1,118 @@
1
+ import type {
2
+ GenericSchema,
3
+ SchemaDefinition,
4
+ UserIdentity,
5
+ } from "convex/server";
6
+ import { componentsGeneric } from "convex/server";
7
+ import * as Execution from "./Execution.js";
8
+ import type * as Kernel from "./Kernel.js";
9
+ import * as KernelRefusal from "./KernelRefusal.js";
10
+
11
+ type Modules = Record<string, () => Promise<any>>;
12
+
13
+ /**
14
+ * What the kernel adds to convex-test's surface, typed. The shim itself returns
15
+ * `any`, as convex-test's callers expect.
16
+ */
17
+ export interface KernelControls {
18
+ readonly query: (reference: any, args?: any) => Promise<any>;
19
+ readonly mutation: (reference: any, args?: any) => Promise<any>;
20
+ readonly action: (reference: any, args?: any) => Promise<any>;
21
+ readonly prepare: (
22
+ reference: any,
23
+ args?: any,
24
+ ) => Promise<Execution.HeldAttempt>;
25
+ readonly apply: (id: string) => Promise<Execution.KernelApply>;
26
+ readonly held: () => ReadonlyArray<Execution.HeldAttempt>;
27
+ }
28
+
29
+ const isSchemaDefinition = (
30
+ value: unknown,
31
+ ): value is SchemaDefinition<GenericSchema, boolean> =>
32
+ value !== null &&
33
+ typeof value === "object" &&
34
+ "tables" in value &&
35
+ typeof (value as { tables: unknown }).tables === "object";
36
+
37
+ const accessor = (world: Execution.World, caller: Execution.Caller): any => ({
38
+ query: (reference: any, args?: any) => world.query(caller, reference, args),
39
+ mutation: (reference: any, args?: any) =>
40
+ world.mutation(caller, reference, args),
41
+ action: (reference: any, args?: any) => world.action(caller, reference, args),
42
+ prepare: (reference: any, args?: any) =>
43
+ world.prepare(caller, reference, args),
44
+ apply: (id: string) => world.apply(id),
45
+ held: () => world.held(),
46
+ run: (handler: (ctx: any) => any) => world.run(caller, "", handler),
47
+ runInComponent: (componentPath: string, handler: (ctx: any) => any) =>
48
+ world.run(caller, componentPath, handler),
49
+ withIdentity: (identity: Partial<UserIdentity>) =>
50
+ accessor(world, Execution.withIdentity(caller, identity)),
51
+ withRequestMetadata: (metadata: {
52
+ readonly ip?: string | null;
53
+ readonly userAgent?: string | null;
54
+ }) =>
55
+ accessor(world, {
56
+ ...caller,
57
+ ip: metadata.ip ?? null,
58
+ userAgent: metadata.userAgent ?? null,
59
+ }),
60
+ fetch: () => Promise.reject(KernelRefusal.rowPending("HTTP routes", 6)),
61
+ jwks: () => Promise.reject(KernelRefusal.rowPending("JWT verification", 6)),
62
+ finishInProgressScheduledFunctions: () =>
63
+ Promise.reject(KernelRefusal.rowPending("the scheduler", 5)),
64
+ finishAllScheduledFunctions: () =>
65
+ Promise.reject(KernelRefusal.rowPending("the scheduler", 5)),
66
+ });
67
+
68
+ /**
69
+ * The test kernel under convex-test's `convexTest` signature. The fork's kernel
70
+ * lane aliases `convex-test` to this module, so every suite that calls
71
+ * `convexTest` directly, and `TestConfect` itself, runs on the kernel.
72
+ *
73
+ * Beyond convex-test, `prepare` runs a mutation to its commit point and holds
74
+ * it, and `apply` validates and publishes a held attempt. `mutation` applies
75
+ * each attempt it prepares at once and prepares the next on a conflict, up to
76
+ * attempt 5. `runtime` supplies the clock and commit timestamps.
77
+ */
78
+ export function convexTest(
79
+ schemaOrOptions?:
80
+ | SchemaDefinition<GenericSchema, boolean>
81
+ | {
82
+ readonly schema?: SchemaDefinition<GenericSchema, boolean>;
83
+ readonly modules?: Modules;
84
+ readonly transactionLimits?: unknown;
85
+ readonly runtime?: Kernel.KernelRuntime;
86
+ },
87
+ modules?: Modules,
88
+ ): any {
89
+ let schema: SchemaDefinition<GenericSchema, boolean> | undefined;
90
+ let runtime: Kernel.KernelRuntime | undefined;
91
+ if (isSchemaDefinition(schemaOrOptions)) {
92
+ schema = schemaOrOptions;
93
+ } else if (schemaOrOptions !== undefined) {
94
+ if (schemaOrOptions.transactionLimits !== undefined) {
95
+ throw KernelRefusal.rowPending("transaction limits", 6);
96
+ }
97
+ schema = schemaOrOptions.schema;
98
+ modules = schemaOrOptions.modules;
99
+ runtime = schemaOrOptions.runtime;
100
+ }
101
+ if (modules === undefined) {
102
+ throw new Error(
103
+ "The test kernel needs the modules map: pass `import.meta.glob(...)` as the second argument.",
104
+ );
105
+ }
106
+ const world = new Execution.World(schema, modules, runtime);
107
+ return {
108
+ ...accessor(world, Execution.anonymous),
109
+ registerComponent: (
110
+ componentPath: string,
111
+ componentSchema: SchemaDefinition<GenericSchema, boolean>,
112
+ componentModules: Modules,
113
+ ) => {
114
+ world.register(componentPath, componentSchema, componentModules);
115
+ return (componentsGeneric() as any)[componentPath];
116
+ },
117
+ };
118
+ }