@arki/dot 0.1.3 → 0.2.0

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 (42) hide show
  1. package/README.md +427 -18
  2. package/dist/cli/index.d.ts +5 -0
  3. package/dist/cli/index.d.ts.map +1 -1
  4. package/dist/cli/index.js +21 -1
  5. package/dist/cli/index.js.map +1 -1
  6. package/dist/cli/render-graph.d.ts +36 -0
  7. package/dist/cli/render-graph.d.ts.map +1 -0
  8. package/dist/cli/render-graph.js +70 -0
  9. package/dist/cli/render-graph.js.map +1 -0
  10. package/dist/define-app.d.ts +10 -0
  11. package/dist/define-app.d.ts.map +1 -1
  12. package/dist/define-app.js +2 -0
  13. package/dist/define-app.js.map +1 -1
  14. package/dist/index.d.ts +4 -2
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +2 -1
  17. package/dist/index.js.map +1 -1
  18. package/dist/kernel/app-instance.d.ts +2 -0
  19. package/dist/kernel/app-instance.d.ts.map +1 -1
  20. package/dist/kernel/app-instance.js +67 -17
  21. package/dist/kernel/app-instance.js.map +1 -1
  22. package/dist/lifecycle.d.ts +2 -0
  23. package/dist/lifecycle.d.ts.map +1 -1
  24. package/dist/lifecycle.js +2 -0
  25. package/dist/lifecycle.js.map +1 -1
  26. package/dist/signals.d.ts +65 -0
  27. package/dist/signals.d.ts.map +1 -0
  28. package/dist/signals.js +97 -0
  29. package/dist/signals.js.map +1 -0
  30. package/dist/test-harness.d.ts +75 -1
  31. package/dist/test-harness.d.ts.map +1 -1
  32. package/dist/test-harness.js +71 -0
  33. package/dist/test-harness.js.map +1 -1
  34. package/package.json +1 -1
  35. package/src/cli/index.ts +36 -2
  36. package/src/cli/render-graph.ts +88 -0
  37. package/src/define-app.ts +13 -0
  38. package/src/index.ts +5 -2
  39. package/src/kernel/app-instance.ts +113 -51
  40. package/src/lifecycle.ts +2 -0
  41. package/src/signals.ts +134 -0
  42. package/src/test-harness.ts +140 -1
@@ -26,7 +26,7 @@
26
26
  */
27
27
 
28
28
  import type { DotApp, DotAppBuilder } from './define-app.js';
29
- import type { AnyPip, EmptyShape, ServiceRecord } from './pip-contract.js';
29
+ import type { AnyPip, EmptyShape, Pip, ServiceRecord, Token } from './pip-contract.js';
30
30
  import { defineApp } from './define-app.js';
31
31
 
32
32
  export type TestAppOptions = {
@@ -70,3 +70,142 @@ export async function bootTestApp<TServices extends ServiceRecord = EmptyShape>(
70
70
  ): Promise<DotApp<TServices>> {
71
71
  return testApp<TServices>(pips, options).boot();
72
72
  }
73
+
74
+ /**
75
+ * Rest-tuple guard for {@link TestPipBuilder.boot} — the same trick the app
76
+ * builder's `UseGuard` uses. While any need is still unprovided, `boot()`
77
+ * demands an impossible second argument, so the call site fails with
78
+ * "Expected 2 arguments, but got 1" and the error payload names the
79
+ * missing wire keys and their types.
80
+ */
81
+ type TestPipBootGuard<TRemaining extends ServiceRecord> = keyof TRemaining extends never
82
+ ? readonly []
83
+ : readonly [
84
+ needs: {
85
+ readonly 'DOT-TEST: pip needs still unprovided — call .provide() for each': {
86
+ readonly [K in keyof TRemaining]: TRemaining[K];
87
+ };
88
+ },
89
+ ];
90
+
91
+ /**
92
+ * Typed unit-test builder for a single pip — see {@link testPip}.
93
+ *
94
+ * `TRemaining` tracks the wire keys not yet covered by `.provide()`;
95
+ * `TServices` accumulates what the booted app will expose (the fakes plus
96
+ * the pip's own provides).
97
+ */
98
+ export type TestPipBuilder<TRemaining extends ServiceRecord, TServices extends ServiceRecord> = {
99
+ /**
100
+ * Satisfy one need directly with a fake. Token form — the token names
101
+ * the wire key and carries the type:
102
+ *
103
+ * ```ts
104
+ * testPip(reports).provide(Db, fakeDb)
105
+ * ```
106
+ */
107
+ provide<K extends keyof TRemaining & string, T extends TRemaining[K]>(
108
+ token: Token<T, K>,
109
+ value: T,
110
+ ): TestPipBuilder<Omit<TRemaining, K>, TServices & Readonly<Record<K, T>>>;
111
+ /**
112
+ * Satisfy one need directly with a fake. Key form — for anonymous
113
+ * `service<T>()` needs, where the wire key is the property name:
114
+ *
115
+ * ```ts
116
+ * testPip(billing).provide('db', fakeDb)
117
+ * ```
118
+ */
119
+ provide<K extends keyof TRemaining & string>(
120
+ key: K,
121
+ value: TRemaining[K],
122
+ ): TestPipBuilder<Omit<TRemaining, K>, TServices & Readonly<Record<K, TRemaining[K]>>>;
123
+ /** Boot the pip against the provided fakes. Compile error until every need is provided. */
124
+ boot(...guard: TestPipBootGuard<TRemaining>): Promise<DotApp<TServices>>;
125
+ /** Boot + start. Same guard as {@link TestPipBuilder.boot}. */
126
+ start(...guard: TestPipBootGuard<TRemaining>): Promise<DotApp<TServices>>;
127
+ };
128
+
129
+ /** Internal accumulating state for {@link testPip}. */
130
+ type TestPipState = {
131
+ readonly pip: AnyPip;
132
+ readonly fakes: Readonly<Record<string, unknown>>;
133
+ readonly options: TestAppOptions;
134
+ };
135
+
136
+ /**
137
+ * Unit-test a single pip with **typed overrides** — the compile-checked
138
+ * counterpart to {@link testApp}'s erased arrays.
139
+ *
140
+ * Each `.provide()` satisfies one of the pip's needs directly (no real
141
+ * provider pip, no dependency chain) and removes it from the builder's
142
+ * remaining-needs type. `boot()` only compiles once every need is covered,
143
+ * and a fake of the wrong shape fails at the `.provide()` call site:
144
+ *
145
+ * ```ts
146
+ * import { testPip } from '@arki/dot/test-harness';
147
+ *
148
+ * const app = await testPip(catalog)
149
+ * .provide(Db, fakeDb) // token need
150
+ * .provide('cache', fakeKv) // anonymous need — wire key is the alias
151
+ * .boot();
152
+ *
153
+ * expect(app.services.catalog.list()).toEqual([]);
154
+ * await app.dispose();
155
+ * ```
156
+ *
157
+ * `service.lazy<T>()` needs accept either a plain `T` fake (the kernel
158
+ * lifts it) or a `Lazy<T>` handle (`lazyOf(value)` is handy here).
159
+ * Lifecycle semantics are the real kernel's — the fakes are published by a
160
+ * synthetic first pip, so reverse-order teardown and lazy auto-dispose
161
+ * behave exactly as in production.
162
+ */
163
+ export function testPip<TNeeds extends ServiceRecord, TProvides extends ServiceRecord>(
164
+ pip: Pip<TNeeds, TProvides>,
165
+ options: TestAppOptions = {},
166
+ ): TestPipBuilder<TNeeds, TProvides> {
167
+ // Erasure seam — same boundary as `makeBuilder` in define-app.ts: the
168
+ // runtime impl is untyped, the type parameters do all the guarding.
169
+ return makeTestPipBuilder({ pip: pip as AnyPip, fakes: {}, options }) as TestPipBuilder<TNeeds, TProvides>;
170
+ }
171
+
172
+ /**
173
+ * Erased implementation behind {@link testPip}. Mirrors `makeBuilder` in
174
+ * `define-app.ts`: the runtime is untyped, the single cast at the return
175
+ * is the seam, and the type parameters do all the guarding.
176
+ */
177
+ function makeTestPipBuilder(state: TestPipState): TestPipBuilder<ServiceRecord, ServiceRecord> {
178
+ const bootApp = async (): Promise<DotApp<ServiceRecord>> => {
179
+ const pips: AnyPip[] = [];
180
+ if (Object.keys(state.fakes).length > 0) {
181
+ const fakes = { ...state.fakes };
182
+ pips.push({
183
+ name: `test:fakes(${state.pip.name})`,
184
+ needs: {},
185
+ renames: {},
186
+ hooks: { boot: () => fakes },
187
+ });
188
+ }
189
+ pips.push(state.pip);
190
+ return testApp<ServiceRecord>(pips, {
191
+ name: state.options.name ?? `test:${state.pip.name}`,
192
+ ...(state.options.config === undefined ? {} : { config: state.options.config }),
193
+ }).boot();
194
+ };
195
+
196
+ const impl = {
197
+ provide(tokenOrKey: Token<unknown, string> | string, value: unknown) {
198
+ const key = typeof tokenOrKey === 'string' ? tokenOrKey : tokenOrKey.key;
199
+ return makeTestPipBuilder({ ...state, fakes: { ...state.fakes, [key]: value } });
200
+ },
201
+ async boot(..._guard: readonly unknown[]) {
202
+ return bootApp();
203
+ },
204
+ async start(..._guard: readonly unknown[]) {
205
+ const app = await bootApp();
206
+ await app.start();
207
+ return app;
208
+ },
209
+ };
210
+ return impl as TestPipBuilder<ServiceRecord, ServiceRecord>;
211
+ }