11import { describe , expect , it } from "vitest" ;
22import { Future , Result } from "@swan-io/boxed" ;
33import { z } from "zod" ;
4- import { declareActivitiesHandler } from "./handler.js" ;
4+ import {
5+ declareActivitiesHandler ,
6+ type ActivityError ,
7+ type BoxedActivityImplementations ,
8+ } from "./handler.js" ;
59import type { ContractDefinition } from "@temporal-contract/contract" ;
610
711describe ( "Worker-Boxed Package" , ( ) => {
@@ -61,11 +65,11 @@ describe("Worker-Boxed Package", () => {
6165
6266 // Valid input should work
6367 const result = await handler . activities [ "processPayment" ] ! ( { amount : 100 , currency : "USD" } ) ;
64- expect ( result . transactionId ) . toBe ( "tx-100" ) ;
68+ expect ( ( result as { transactionId : string } ) . transactionId ) . toBe ( "tx-100" ) ;
6569
6670 // Invalid input should throw
6771 await expect (
68- handler . activities [ "processPayment" ] ! ( { amount : "invalid" as any , currency : "USD" } ) ,
72+ handler . activities [ "processPayment" ] ! ( { amount : "invalid" as unknown , currency : "USD" } ) ,
6973 ) . rejects . toThrow ( ) ;
7074 } ) ;
7175
@@ -91,15 +95,19 @@ describe("Worker-Boxed Package", () => {
9195 } ) ;
9296
9397 const result = await handler . activities [ "fetchData" ] ! ( { id : "abc" } ) ;
94- expect ( result . data ) . toBe ( "data-abc" ) ;
95- expect ( result . timestamp ) . toBe ( 123 ) ;
98+ expect ( ( result as { data : string ; timestamp : number } ) . data ) . toBe ( "data-abc" ) ;
99+ expect ( ( result as { data : string ; timestamp : number } ) . timestamp ) . toBe ( 123 ) ;
96100
97101 // Invalid output should throw
98102 const badHandler = declareActivitiesHandler ( {
99103 contract,
100104 activities : {
101- fetchData : ( _args ) => {
102- return Future . value ( Result . Ok ( { data : "test" } as any ) ) ; // Missing timestamp
105+ fetchData : (
106+ _args ,
107+ ) : Future < Result < { data : string ; timestamp : number } , ActivityError > > => {
108+ return Future . value (
109+ Result . Ok ( { data : "test" } as unknown as { data : string ; timestamp : number } ) ,
110+ ) ; // Missing timestamp
103111 } ,
104112 } ,
105113 } ) ;
@@ -129,7 +137,7 @@ describe("Worker-Boxed Package", () => {
129137 } ) ;
130138
131139 const result = await handler . activities [ "successActivity" ] ! ( { value : "test" } ) ;
132- expect ( result . result ) . toBe ( "success-test" ) ;
140+ expect ( ( result as { result : string } ) . result ) . toBe ( "success-test" ) ;
133141 } ) ;
134142
135143 it ( "should handle Result.Error by throwing exception" , async ( ) => {
@@ -162,10 +170,14 @@ describe("Worker-Boxed Package", () => {
162170 try {
163171 await handler . activities [ "failingActivity" ] ! ( { value : "test" } ) ;
164172 expect . fail ( "Should have thrown an error" ) ;
165- } catch ( error : any ) {
166- expect ( error . message ) . toBe ( "Something went wrong" ) ;
167- expect ( error . code ) . toBe ( "ACTIVITY_FAILED" ) ;
168- expect ( error . details ) . toEqual ( { info : "additional details" } ) ;
173+ } catch ( error : unknown ) {
174+ expect ( ( error as Error & { code : string ; details ?: unknown } ) . message ) . toBe (
175+ "Something went wrong" ,
176+ ) ;
177+ expect ( ( error as Error & { code : string ; details ?: unknown } ) . code ) . toBe ( "ACTIVITY_FAILED" ) ;
178+ expect ( ( error as Error & { code : string ; details ?: unknown } ) . details ) . toEqual ( {
179+ info : "additional details" ,
180+ } ) ;
169181 }
170182 } ) ;
171183
@@ -185,7 +197,7 @@ describe("Worker-Boxed Package", () => {
185197 contract,
186198 activities : {
187199 asyncActivity : ( args ) => {
188- return Future . make < Result < { completed : boolean } , any > > ( ( resolve ) => {
200+ return Future . make < Result < { completed : boolean } , ActivityError > > ( ( resolve ) => {
189201 setTimeout ( ( ) => {
190202 resolve ( Result . Ok ( { completed : true } ) ) ;
191203 } , args . delay ) ;
@@ -195,7 +207,7 @@ describe("Worker-Boxed Package", () => {
195207 } ) ;
196208
197209 const result = await handler . activities [ "asyncActivity" ] ! ( { delay : 10 } ) ;
198- expect ( result . completed ) . toBe ( true ) ;
210+ expect ( ( result as { completed : boolean } ) . completed ) . toBe ( true ) ;
199211 } ) ;
200212
201213 it ( "should support workflow-specific activities" , async ( ) => {
@@ -224,8 +236,8 @@ describe("Worker-Boxed Package", () => {
224236 } ,
225237 } ) ;
226238
227- const result = await handler . activities [ "validateOrder" ] ! ( { orderId : "ORDER- 123" } ) ;
228- expect ( result . valid ) . toBe ( true ) ;
239+ const result = await handler . activities [ "validateOrder" ] ! ( { orderId : "123" , amount : 100 } ) ;
240+ expect ( ( result as { valid : boolean } ) . valid ) . toBe ( true ) ;
229241 } ) ;
230242
231243 it ( "should throw if activity definition is not found" , ( ) => {
@@ -239,8 +251,8 @@ describe("Worker-Boxed Package", () => {
239251 declareActivitiesHandler ( {
240252 contract,
241253 activities : {
242- unknownActivity : ( _args : any ) => Future . value ( Result . Ok ( { result : "test" } ) ) ,
243- } as any ,
254+ unknownActivity : ( _args : unknown ) => Future . value ( Result . Ok ( { result : "test" } ) ) ,
255+ } as BoxedActivityImplementations < typeof contract > ,
244256 } ) ;
245257 } ) . toThrow ( "Activity definition not found" ) ;
246258 } ) ;
0 commit comments