|
| 1 | +/* eslint-disable @typescript-eslint/no-unused-vars */ |
| 2 | +import { describe, expectTypeOf, it } from 'vitest'; |
| 3 | +import type { InitFlow, InternalErrorResponse, FlowNode, Updater } from './client.types.js'; |
| 4 | +import type { GenericError } from './error.types.js'; |
| 5 | +import type { ErrorNode, FailureNode, ContinueNode, StartNode, SuccessNode } from './node.types.js'; |
| 6 | + |
| 7 | +describe('Client Types', () => { |
| 8 | + it('should allow function returning error', async () => { |
| 9 | + // This test isn't excellent but without narrowing inside the function we cant |
| 10 | + // narrow the promise type enough without some help on the generic |
| 11 | + const withError = async (): Promise<InternalErrorResponse> => { |
| 12 | + const result = await Promise.resolve<InternalErrorResponse>({ |
| 13 | + error: { message: 'Test error', type: 'argument_error' as const }, |
| 14 | + type: 'internal_error' as const, |
| 15 | + }); |
| 16 | + return result; |
| 17 | + }; |
| 18 | + |
| 19 | + expectTypeOf(withError()).resolves.toMatchTypeOf<InternalErrorResponse>(); |
| 20 | + }); |
| 21 | + it('should allow function returning node types', () => { |
| 22 | + const withErrorNode: InitFlow = async () => ({ |
| 23 | + cache: { |
| 24 | + key: 'string', |
| 25 | + }, |
| 26 | + client: { |
| 27 | + status: 'error', |
| 28 | + }, |
| 29 | + error: { |
| 30 | + type: 'argument_error', |
| 31 | + status: 'failure', |
| 32 | + message: 'failed', |
| 33 | + }, |
| 34 | + httpStatus: 400, |
| 35 | + server: { |
| 36 | + status: 'error', |
| 37 | + }, |
| 38 | + status: 'error', |
| 39 | + }); |
| 40 | + |
| 41 | + const withFailureNode: InitFlow = async () => ({ |
| 42 | + cache: { |
| 43 | + key: '', |
| 44 | + }, |
| 45 | + client: { |
| 46 | + status: 'failure', |
| 47 | + }, |
| 48 | + error: { |
| 49 | + type: 'state_error', |
| 50 | + status: 'failure', |
| 51 | + message: 'failed', |
| 52 | + }, |
| 53 | + httpStatus: 404, |
| 54 | + server: null, |
| 55 | + status: 'failure', |
| 56 | + }); |
| 57 | + |
| 58 | + const withContinueNode: InitFlow = async () => ({ |
| 59 | + cache: { |
| 60 | + key: 'cachekey', |
| 61 | + }, |
| 62 | + client: { |
| 63 | + action: 'action', |
| 64 | + collectors: [], |
| 65 | + description: 'the description', |
| 66 | + name: 'continue_node_name', |
| 67 | + status: 'continue', |
| 68 | + }, |
| 69 | + error: null, |
| 70 | + httpStatus: 200, |
| 71 | + server: { |
| 72 | + eventName: 'continue_event', |
| 73 | + status: 'continue', |
| 74 | + }, |
| 75 | + status: 'continue', |
| 76 | + }); |
| 77 | + |
| 78 | + const withStartNode: InitFlow = async () => ({ |
| 79 | + cache: null, |
| 80 | + client: { |
| 81 | + status: 'start', |
| 82 | + }, |
| 83 | + error: null, |
| 84 | + server: { |
| 85 | + status: 'start', |
| 86 | + }, |
| 87 | + status: 'start', |
| 88 | + }); |
| 89 | + |
| 90 | + const withSuccessNode: InitFlow = async () => ({ |
| 91 | + cache: { |
| 92 | + key: 'key', |
| 93 | + }, |
| 94 | + client: { |
| 95 | + authorization: { |
| 96 | + code: 'code123412', |
| 97 | + state: 'code123213', |
| 98 | + }, |
| 99 | + status: 'success', |
| 100 | + }, |
| 101 | + error: null, |
| 102 | + httpStatus: 200, |
| 103 | + server: { |
| 104 | + eventName: 'success_event', |
| 105 | + id: 'theid', |
| 106 | + interactionId: '213123', |
| 107 | + interactionToken: '123213', |
| 108 | + status: 'success', |
| 109 | + }, |
| 110 | + status: 'success', |
| 111 | + }); |
| 112 | + |
| 113 | + // Test return types |
| 114 | + // @ts-expect-error - This is a problem because ErrorResponse does not have a discriminator that separates it from FlowNode (see `error` key in both types.) |
| 115 | + expectTypeOf(withErrorNode).returns.resolves.toMatchTypeOf<ErrorNode>(); |
| 116 | + // @ts-expect-error - This is a problem because ErrorResponse does not have a discriminator that separates it from FlowNode (see `error` key in both types.) |
| 117 | + expectTypeOf(withFailureNode).returns.resolves.toMatchTypeOf<FailureNode>(); |
| 118 | + // @ts-expect-error - This is a problem because ErrorResponse does not have a discriminator that separates it from FlowNode (see `error` key in both types.) |
| 119 | + expectTypeOf(withContinueNode).returns.resolves.toMatchTypeOf<ContinueNode>(); |
| 120 | + // @ts-expect-error - This is a problem because ErrorResponse does not have a discriminator that separates it from FlowNode (see `error` key in both types.) |
| 121 | + expectTypeOf(withStartNode).returns.resolves.toMatchTypeOf<StartNode>(); |
| 122 | + // @ts-expect-error - This is a problem because ErrorResponse does not have a discriminator that separates it from FlowNode (see `error` key in both types.) |
| 123 | + expectTypeOf(withSuccessNode).returns.resolves.toMatchTypeOf<SuccessNode>(); |
| 124 | + |
| 125 | + // Test that all are valid InitFlow types |
| 126 | + expectTypeOf(withErrorNode).toMatchTypeOf<InitFlow>(); |
| 127 | + expectTypeOf(withFailureNode).toMatchTypeOf<InitFlow>(); |
| 128 | + expectTypeOf(withContinueNode).toMatchTypeOf<InitFlow>(); |
| 129 | + expectTypeOf(withStartNode).toMatchTypeOf<InitFlow>(); |
| 130 | + expectTypeOf(withSuccessNode).toMatchTypeOf<InitFlow>(); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should enforce async function return type', () => { |
| 134 | + // @ts-expect-error - Should not allow non-promise return |
| 135 | + const invalid: InitFlow = () => ({ |
| 136 | + cache: { |
| 137 | + key: 'key', |
| 138 | + }, |
| 139 | + client: { |
| 140 | + action: 'continue', |
| 141 | + collectors: [], |
| 142 | + status: 'continue', |
| 143 | + }, |
| 144 | + error: null, |
| 145 | + httpStatus: 200, |
| 146 | + server: { |
| 147 | + status: 'continue', |
| 148 | + }, |
| 149 | + status: 'continue', |
| 150 | + }); |
| 151 | + |
| 152 | + expectTypeOf<InitFlow>().toBeFunction(); |
| 153 | + |
| 154 | + // @ts-expect-error - Should not allow non-promise return - we expect this to error |
| 155 | + expectTypeOf<InitFlow>().returns.toEqualTypeOf<InitFlow>; |
| 156 | + }); |
| 157 | +}); |
| 158 | + |
| 159 | +describe('Updater', () => { |
| 160 | + it('should accept string value and optional index', () => { |
| 161 | + const updater: Updater = (value: string, index?: number) => { |
| 162 | + return { |
| 163 | + error: { message: 'Invalid value', code: 'INVALID', type: 'state_error' }, |
| 164 | + type: 'internal_error', |
| 165 | + }; |
| 166 | + }; |
| 167 | + |
| 168 | + expectTypeOf(updater).parameter(0).toBeString(); |
| 169 | + expectTypeOf(updater).parameter(1).toBeNullable(); |
| 170 | + expectTypeOf(updater).parameter(1).toBeNullable(); |
| 171 | + }); |
| 172 | + |
| 173 | + it('should return error or null', () => { |
| 174 | + const withError: Updater = () => ({ |
| 175 | + error: { message: 'Invalid value', code: 'INVALID', type: 'state_error' }, |
| 176 | + type: 'internal_error', |
| 177 | + }); |
| 178 | + |
| 179 | + const withoutError: Updater = () => null; |
| 180 | + |
| 181 | + expectTypeOf(withError).returns.toMatchTypeOf<{ error: GenericError } | null>(); |
| 182 | + expectTypeOf(withoutError).returns.toMatchTypeOf<{ error: GenericError } | null>(); |
| 183 | + |
| 184 | + // Test both are valid Updater types |
| 185 | + expectTypeOf(withError).toMatchTypeOf<Updater>(); |
| 186 | + expectTypeOf(withoutError).toMatchTypeOf<Updater>(); |
| 187 | + }); |
| 188 | +}); |
0 commit comments