|
| 1 | +// Compile-time type tests for the `static adapter` property on Query, |
| 2 | +// Mutation, and TopicQuery. Validated by `tsc --noEmit` (via `check-types`). |
| 3 | +// Vitest's test glob is `*.test.ts`, so `.test-d.ts` files are not executed |
| 4 | +// at runtime. |
| 5 | + |
| 6 | +import { t } from '../typeDefs.js'; |
| 7 | +import { Query } from '../query.js'; |
| 8 | +import { Mutation } from '../mutation.js'; |
| 9 | +import { QueryAdapter } from '../QueryAdapter.js'; |
| 10 | +import { TopicQuery } from '../topic/TopicQuery.js'; |
| 11 | +import { TopicQueryAdapter } from '../topic/TopicQueryAdapter.js'; |
| 12 | +import { RESTQueryAdapter } from '../rest/RESTQueryAdapter.js'; |
| 13 | + |
| 14 | +// A realistic TopicQueryAdapter that requires construction arguments — |
| 15 | +// the shape of a real-world wallet/chain/websocket adapter. Before this |
| 16 | +// PR, assigning this to `static override adapter` required the cast |
| 17 | +// `as unknown as typeof TopicQueryAdapter`. |
| 18 | +class WebSocketTopicAdapter extends TopicQueryAdapter { |
| 19 | + constructor( |
| 20 | + private readonly url: string, |
| 21 | + private readonly token: string, |
| 22 | + ) { |
| 23 | + super(); |
| 24 | + } |
| 25 | + subscribe(_topic: string): void {} |
| 26 | + unsubscribe(_topic: string): void {} |
| 27 | +} |
| 28 | + |
| 29 | +// A plain QueryAdapter (not a TopicQueryAdapter) with a required |
| 30 | +// constructor arg. Used as a positive fixture for Query / Mutation and |
| 31 | +// as a negative fixture for TopicQuery. |
| 32 | +class AuthenticatedAdapter extends QueryAdapter { |
| 33 | + constructor(private readonly token: string) { |
| 34 | + super(); |
| 35 | + } |
| 36 | + async send(): Promise<unknown> { |
| 37 | + return undefined; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +class NotAnAdapter {} |
| 42 | + |
| 43 | +// ============================================================ |
| 44 | +// TopicQuery.adapter — generic constraint to TopicQueryAdapter subclasses |
| 45 | +// ============================================================ |
| 46 | + |
| 47 | +// Direct subclass — the standard pattern. |
| 48 | +class GetPrices extends TopicQuery { |
| 49 | + static override adapter = WebSocketTopicAdapter; |
| 50 | + |
| 51 | + topic = 'prices:live'; |
| 52 | + result = { |
| 53 | + price: t.number, |
| 54 | + timestamp: t.number, |
| 55 | + }; |
| 56 | +} |
| 57 | + |
| 58 | +// Shared abstract base — the pattern used in Fetchium's own test suite |
| 59 | +// (MockTopicQuery) — must also compile without a cast. |
| 60 | +abstract class WebSocketTopicQuery extends TopicQuery { |
| 61 | + static override adapter = WebSocketTopicAdapter; |
| 62 | +} |
| 63 | + |
| 64 | +// @ts-expect-error — RESTQueryAdapter is a QueryAdapter but not a TopicQueryAdapter |
| 65 | +class BadRestAdapter extends TopicQuery { |
| 66 | + static override adapter = RESTQueryAdapter; |
| 67 | + |
| 68 | + topic = 'bad-rest'; |
| 69 | + result = {}; |
| 70 | +} |
| 71 | + |
| 72 | +// @ts-expect-error — AuthenticatedAdapter is a QueryAdapter but not a TopicQueryAdapter |
| 73 | +class BadSiblingAdapter extends TopicQuery { |
| 74 | + static override adapter = AuthenticatedAdapter; |
| 75 | + |
| 76 | + topic = 'bad-sibling'; |
| 77 | + result = {}; |
| 78 | +} |
| 79 | + |
| 80 | +// @ts-expect-error — NotAnAdapter does not extend TopicQueryAdapter |
| 81 | +class BadRandomClass extends TopicQuery { |
| 82 | + static override adapter = NotAnAdapter; |
| 83 | + |
| 84 | + topic = 'bad-random'; |
| 85 | + result = {}; |
| 86 | +} |
| 87 | + |
| 88 | +// ============================================================ |
| 89 | +// Query.adapter — accepts any QueryAdapter subclass, rejects non-adapters |
| 90 | +// ============================================================ |
| 91 | + |
| 92 | +class FetchUser extends Query { |
| 93 | + static override adapter = AuthenticatedAdapter; |
| 94 | + |
| 95 | + params = { userId: t.string }; |
| 96 | + result = { |
| 97 | + id: t.string, |
| 98 | + name: t.string, |
| 99 | + }; |
| 100 | + |
| 101 | + getIdentityKey() { |
| 102 | + return 'fetch-user'; |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// @ts-expect-error — NotAnAdapter does not extend QueryAdapter |
| 107 | +class BadQuery extends Query { |
| 108 | + static override adapter = NotAnAdapter; |
| 109 | + |
| 110 | + result = {}; |
| 111 | + |
| 112 | + getIdentityKey() { |
| 113 | + return 'bad-query'; |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +// ============================================================ |
| 118 | +// Mutation.adapter — accepts any QueryAdapter subclass, rejects non-adapters |
| 119 | +// ============================================================ |
| 120 | + |
| 121 | +class UpdateUser extends Mutation { |
| 122 | + static override adapter = AuthenticatedAdapter; |
| 123 | + |
| 124 | + params = { userId: t.string, name: t.string }; |
| 125 | + |
| 126 | + getIdentityKey() { |
| 127 | + return 'update-user'; |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +// @ts-expect-error — NotAnAdapter does not extend QueryAdapter |
| 132 | +class BadMutation extends Mutation { |
| 133 | + static override adapter = NotAnAdapter; |
| 134 | + |
| 135 | + getIdentityKey() { |
| 136 | + return 'bad-mutation'; |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +// Prevent "declared but unused" diagnostics for the fixtures above. |
| 141 | +export type _AdapterTypeTests = |
| 142 | + | typeof GetPrices |
| 143 | + | typeof WebSocketTopicQuery |
| 144 | + | typeof BadRestAdapter |
| 145 | + | typeof BadSiblingAdapter |
| 146 | + | typeof BadRandomClass |
| 147 | + | typeof FetchUser |
| 148 | + | typeof BadQuery |
| 149 | + | typeof UpdateUser |
| 150 | + | typeof BadMutation; |
0 commit comments