@effect-app/vue 4.0.0-beta.187 → 4.0.0-beta.188

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.
@@ -1,9 +1,9 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
2
  import { expect, expectTypeOf, it } from "@effect/vitest"
3
- import { type Effect, S } from "effect-app"
3
+ import { S } from "effect-app"
4
4
  import { configureInvalidation, makeQueryKey } from "effect-app/client"
5
5
  import * as Exit from "effect/Exit"
6
- import { type ExportComplete, Something, SomethingElse, SomethingElseReq, SomethingReq, useClient, useExperimental } from "./stubs.js"
6
+ import { Something, SomethingElse, SomethingElseReq, SomethingReq, useClient, useExperimental } from "./stubs.js"
7
7
 
8
8
  const somethingInvalidationResources = {
9
9
  Something: {
@@ -273,25 +273,3 @@ it.skip("works", () => {
273
273
  projectedStruct
274
274
  })
275
275
  })
276
-
277
- it.skip("stream final type tests", () => {
278
- const { clientFor } = useClient()
279
- const client = clientFor(Something, undefined, somethingInvalidationResources)
280
-
281
- const execNoFinal = client.StreamWithoutFinal.mutateToResult()
282
- const execWithFinal = client.StreamWithFinal.mutateToResult()
283
-
284
- // Without `final`: execute input is {id: string} and resolves with void
285
- const _execNoFinalResult: ReturnType<typeof execNoFinal> = execNoFinal({ id: "test" })
286
- // @ts-expect-error result of execNoFinal should be void-typed, not ExportComplete
287
- const _badAssign: Effect.Effect<ExportComplete, never, never> = _execNoFinalResult
288
-
289
- // With `final: ExportComplete`: execute resolves with ExportComplete
290
- const _execWithFinalResult: ReturnType<typeof execWithFinal> = execWithFinal({ id: "test" })
291
- // Assignment should compile — result IS Effect<ExportComplete, ...>
292
- const _goodAssign: Effect.Effect<ExportComplete, never, never> = _execWithFinalResult
293
- void _execNoFinalResult
294
- void _execWithFinalResult
295
- void _goodAssign
296
- void _badAssign
297
- })
@@ -2,23 +2,13 @@
2
2
  * Runtime and type tests for the `final` schema on stream requests.
3
3
  *
4
4
  * The `final` option on a stream request schema lets callers model which type
5
- * the last emitted stream element is. When present, the execute effect returned
6
- * by `mutateToResult` resolves with that final value instead of `void`.
5
+ * the last emitted stream element is.
7
6
  */
8
7
  import { expect, it } from "@effect/vitest"
9
8
  import { Effect, S } from "effect-app"
10
9
  import * as Stream from "effect/Stream"
11
10
  import { asStreamResult } from "../src/mutate.js"
12
- import { ExportComplete, OperationProgress, Something, useClient } from "./stubs.js"
13
-
14
- const somethingInvalidationResources = {
15
- Something: {
16
- GetSomething2: Something.GetSomething2,
17
- GetSomething2WithDependencies: Something.GetSomething2WithDependencies,
18
- GetSomething3: Something.GetSomething3,
19
- GetSomething4: Something.GetSomething4
20
- }
21
- }
11
+ import { ExportComplete, OperationProgress, Something } from "./stubs.js"
22
12
 
23
13
  // ---------------------------------------------------------------------------
24
14
  // asStreamResult — low-level primitive, always returns void
@@ -39,43 +29,6 @@ it.live("asStreamResult returns void and updates ref with each element", () =>
39
29
  }
40
30
  }))
41
31
 
42
- // ---------------------------------------------------------------------------
43
- // mutateToResult with no `final` — execute resolves with void (type-level)
44
- // ---------------------------------------------------------------------------
45
-
46
- it.skip("mutateToResult without final: execute resolves void (type-level)", () => {
47
- const { clientFor } = useClient()
48
- const client = clientFor(Something, undefined, somethingInvalidationResources)
49
-
50
- const execute = client.StreamWithoutFinal.mutateToResult()
51
-
52
- // execute returns void — assigning to ExportComplete Effect should fail
53
- const result = execute({ id: "test" })
54
- // @ts-expect-error result should be void-typed, not ExportComplete
55
- const _bad: Effect.Effect<ExportComplete, never, never> = result
56
- void _bad
57
- })
58
-
59
- // ---------------------------------------------------------------------------
60
- // mutateToResult with `final` — execute resolves with Final type (type-level)
61
- // ---------------------------------------------------------------------------
62
-
63
- it.skip("mutateToResult with final: execute resolves with ExportComplete (type-level)", () => {
64
- const { clientFor } = useClient()
65
- const client = clientFor(Something, undefined, somethingInvalidationResources)
66
-
67
- const execute = client.StreamWithFinal.mutateToResult()
68
-
69
- // execute returns ExportComplete — assignment should compile cleanly
70
- const result = execute({ id: "test" })
71
- const _ok: Effect.Effect<ExportComplete, never, never> = result
72
- void _ok
73
- })
74
-
75
- // ---------------------------------------------------------------------------
76
- // Request class — final schema stored on class
77
- // ---------------------------------------------------------------------------
78
-
79
32
  it("stream request without final: .final is undefined", () => {
80
33
  const req = Something.StreamWithoutFinal
81
34
  expect((req as any).final).toBeUndefined()
@@ -1,6 +1,7 @@
1
1
  import { expect, it } from "@effect/vitest"
2
- import { Effect, Fiber } from "effect-app"
2
+ import { Deferred, Effect, Fiber } from "effect-app"
3
3
  import * as Stream from "effect/Stream"
4
+ import { CommanderStatic } from "../src/commander.js"
4
5
  import { AsyncResult } from "../src/lib.js"
5
6
  import { useExperimental } from "./stubs.js"
6
7
 
@@ -216,3 +217,220 @@ it.live("streamFn: generator form Stream.ensuring cleanup runs after stream ends
216
217
  expect(cmd.result.value).toBe(107)
217
218
  }
218
219
  }))
220
+
221
+ // ---------------------------------------------------------------------------
222
+ // Command.mapProgress — updates progress ref for each element
223
+ // ---------------------------------------------------------------------------
224
+
225
+ it.live("streamFn: Command.mapProgress updates progress ref for each stream element", () =>
226
+ Effect.gen(function*() {
227
+ const Command = useExperimental({ toasts: [] })
228
+
229
+ const cmd = Command.streamFn("test-map-progress")(
230
+ function*(_arg: void) {
231
+ return Stream.make(1, 2, 3).pipe(
232
+ CommanderStatic.mapProgress((r) =>
233
+ AsyncResult.isSuccess(r)
234
+ ? { text: `item-${r.value}`, percentage: r.value * 10 }
235
+ : undefined
236
+ )
237
+ )
238
+ }
239
+ )
240
+
241
+ // progress starts undefined (reactive unwraps the ref)
242
+ expect(cmd.progress).toBeUndefined()
243
+
244
+ yield* join(cmd.handle())
245
+
246
+ // after stream drains, last mapped progress value should be set
247
+ expect(cmd.progress).toEqual({ text: "item-3", percentage: 30 })
248
+ }))
249
+
250
+ // ---------------------------------------------------------------------------
251
+ // Command.updateProgress — imperative progress update from stream
252
+ // ---------------------------------------------------------------------------
253
+
254
+ it.live("streamFn: Command.updateProgress imperatively drives the progress ref", () =>
255
+ Effect.gen(function*() {
256
+ const Command = useExperimental({ toasts: [] })
257
+
258
+ const cmd = Command.streamFn("test-update-progress")(
259
+ function*(_arg: void) {
260
+ return Stream.make("a", "b").pipe(
261
+ Stream.tap((v) => CommanderStatic.updateProgress(`processing ${v}`))
262
+ )
263
+ }
264
+ )
265
+
266
+ expect(cmd.progress).toBeUndefined()
267
+ yield* join(cmd.handle())
268
+
269
+ expect(cmd.progress).toBe("processing b")
270
+ }))
271
+
272
+ // ---------------------------------------------------------------------------
273
+ // Command.withDefaultToastStream — in-progress (waiting) initial toast
274
+ // ---------------------------------------------------------------------------
275
+
276
+ it.live("withDefaultToastStream: shows info toast while stream is running", () =>
277
+ Effect.gen(function*() {
278
+ const toasts: any[] = []
279
+ const Command = useExperimental({ toasts, messages: { "handle.waiting": "{action} waiting…" } })
280
+
281
+ // Gate that lets us inspect toast state while the stream is paused mid-flight.
282
+ const streamPaused = yield* Deferred.make<void>()
283
+ const resume = yield* Deferred.make<void>()
284
+
285
+ const cmd = Command.streamFn("doWork")(
286
+ function*(_arg: void) {
287
+ return Stream.make(1).pipe(
288
+ Stream.tap(() =>
289
+ Effect.gen(function*() {
290
+ yield* Deferred.succeed(streamPaused, undefined)
291
+ yield* Deferred.await(resume)
292
+ })
293
+ )
294
+ )
295
+ },
296
+ Command.withDefaultToastStream()
297
+ )
298
+
299
+ const fiber = cmd.handle()
300
+
301
+ // Wait until the stream has emitted its first element (and paused).
302
+ yield* Deferred.await(streamPaused)
303
+
304
+ // The waiting info toast should exist before the stream finishes.
305
+ expect(toasts.some((t) => t.type === "info")).toBe(true)
306
+ const infoToast = toasts.find((t) => t.type === "info")
307
+ expect(infoToast.message).toContain("doWork")
308
+
309
+ // Let the stream finish.
310
+ yield* Deferred.succeed(resume, undefined)
311
+ yield* join(fiber)
312
+
313
+ // After completion the same toast slot is replaced with a success toast.
314
+ expect(toasts.some((t) => t.type === "success")).toBe(true)
315
+ }))
316
+
317
+ // ---------------------------------------------------------------------------
318
+ // Command.withDefaultToastStream — progress text/percent updates the toast
319
+ // ---------------------------------------------------------------------------
320
+
321
+ it.live("withDefaultToastStream: progress option updates waiting toast message", () =>
322
+ Effect.gen(function*() {
323
+ const toasts: any[] = []
324
+ const Command = useExperimental({
325
+ toasts,
326
+ messages: { "handle.waiting": "Working…", "handle.success": "{action} done" }
327
+ })
328
+
329
+ const progressSnapshots: string[] = []
330
+
331
+ const cmd = Command.streamFn("doWorkProgress")(
332
+ function*(_arg: void) {
333
+ return Stream.make(10, 50, 100).pipe(
334
+ Stream.tap((pct) =>
335
+ Effect.sync(() => {
336
+ progressSnapshots.push(`${pct}%`)
337
+ })
338
+ )
339
+ )
340
+ },
341
+ Command.withDefaultToastStream({
342
+ progress: (r) =>
343
+ AsyncResult.isSuccess(r)
344
+ ? { text: `${r.value}%`, percentage: r.value }
345
+ : undefined
346
+ })
347
+ )
348
+
349
+ yield* join(cmd.handle())
350
+
351
+ // All three stream elements were visited by the tap above
352
+ expect(progressSnapshots).toEqual(["10%", "50%", "100%"])
353
+
354
+ // cmd.progress reflects the last mapped value (reactive unwraps the ref)
355
+ expect(cmd.progress).toEqual({ text: "100%", percentage: 100 })
356
+
357
+ // A success toast should appear after the stream completes
358
+ expect(toasts.some((t) => t.type === "success")).toBe(true)
359
+ }))
360
+
361
+ // ---------------------------------------------------------------------------
362
+ // Command.withDefaultToastStream — failure shows warning/error toast
363
+ // ---------------------------------------------------------------------------
364
+
365
+ it.live("withDefaultToastStream: failure shows failure toast, not success toast", () =>
366
+ Effect.gen(function*() {
367
+ const toasts: any[] = []
368
+ const Command = useExperimental({ toasts })
369
+
370
+ class BoomError {
371
+ readonly _tag = "BoomError"
372
+ readonly message = "boom"
373
+ }
374
+
375
+ const cmd = Command.streamFn("doWorkFail")(
376
+ function*(_arg: void) {
377
+ return Stream.fail(new BoomError())
378
+ },
379
+ Command.withDefaultToastStream()
380
+ )
381
+
382
+ yield* join(cmd.handle())
383
+
384
+ // Typed errors → withDefaultToastStream calls toast.warning (level: "warn")
385
+ expect(toasts.some((t) => t.type === "warning" || t.type === "error")).toBe(true)
386
+ expect(toasts.some((t) => t.type === "success")).toBe(false)
387
+ }))
388
+
389
+ // ---------------------------------------------------------------------------
390
+ // Command.withDefaultToastStream — die (defect) shows error toast
391
+ // ---------------------------------------------------------------------------
392
+
393
+ it.live("withDefaultToastStream: die/defect shows error toast, not warning or success", () =>
394
+ Effect.gen(function*() {
395
+ const toasts: any[] = []
396
+ const Command = useExperimental({ toasts })
397
+
398
+ const cmd = Command.streamFn("doWorkDie")(
399
+ function*(_arg: void) {
400
+ // Stream.die produces a defect — Cause.findErrorOption returns Option.none()
401
+ // so defaultFailureMessageHandler returns a plain string → toast.error
402
+ return Stream.die(new Error("unexpected defect"))
403
+ },
404
+ Command.withDefaultToastStream()
405
+ )
406
+
407
+ yield* join(cmd.handle())
408
+
409
+ expect(toasts.some((t) => t.type === "error")).toBe(true)
410
+ expect(toasts.some((t) => t.type === "warning")).toBe(false)
411
+ expect(toasts.some((t) => t.type === "success")).toBe(false)
412
+ }))
413
+
414
+ // ---------------------------------------------------------------------------
415
+ // Command.withDefaultToastStream — success shows success toast
416
+ // ---------------------------------------------------------------------------
417
+
418
+ it.live("withDefaultToastStream: success shows success toast after stream drains", () =>
419
+ Effect.gen(function*() {
420
+ const toasts: any[] = []
421
+ const Command = useExperimental({ toasts, messages: { "handle.success": "{action} complete" } })
422
+
423
+ const cmd = Command.streamFn("doWorkSuccess")(
424
+ function*(_arg: void) {
425
+ return Stream.make(42)
426
+ },
427
+ Command.withDefaultToastStream()
428
+ )
429
+
430
+ yield* join(cmd.handle())
431
+
432
+ const successToast = toasts.find((t) => t.type === "success")
433
+ expect(successToast).toBeDefined()
434
+ expect(successToast.message).toContain("doWorkSuccess")
435
+ expect(toasts.some((t) => t.type === "error")).toBe(false)
436
+ }))