import { expect, test, vi } from 'vitest';
import { hydrateWorkflowReturnValue } from '@workflow/core/serialization';
import { jsonlines } from './jsonlines.mjs';
import { createFetcher, startServer } from './util.mjs';
export function hooks(world) {
    test('hooks', { timeout: 60_000 }, async () => {
        const server = await startServer({ world }).then(createFetcher);
        const token = Date.now().toString(36);
        const customData = `my-custom-data-${token}`;
        const result = await server.invoke('workflows/hooks.ts', 'collectWithHook', [token, customData]);
        expect(result.runId).toMatch(/^wrun_.+/);
        const readable = await server.getReadable(result.runId);
        const events = jsonlines(readable);
        const hookCreated = Promise.withResolvers();
        const hookResumedEvents = [];
        (async () => {
            for await (const obj of events) {
                if (obj?.event === 'hookCreated') {
                    hookCreated.resolve();
                    continue;
                }
                if (obj?.event === 'hookResumed') {
                    hookResumedEvents.push(obj.payload);
                    continue;
                }
                console.log('unhandled event', obj);
            }
        })().catch(() => {
            // ignore errors for reading stream
        });
        await hookCreated.promise;
        (async () => {
            await server.resumeHook(token, {
                data: 'first payload',
            });
            await server.resumeHook(token, {
                data: 'second payload',
            });
            await server.resumeHook(token, {
                data: 'third payload',
                done: true,
            });
        })();
        const run = await vi.waitFor(async () => {
            const run = await server.getRun(result.runId);
            expect(run).toMatchObject({
                status: 'completed',
            });
            return run;
        }, {
            interval: 200,
            timeout: 59_000,
        });
        const output = await hydrateWorkflowReturnValue(run.output, run.runId, undefined);
        expect(output).toEqual({
            collected: [
                {
                    data: 'first payload',
                    metadata: { customData },
                },
                {
                    data: 'second payload',
                    metadata: { customData },
                },
                {
                    data: 'third payload',
                    metadata: { customData },
                    done: true,
                },
            ],
        });
    });
}
//# sourceMappingURL=hooks.mjs.map