|
| 1 | +import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest"; |
| 2 | +import { Server } from "@hocuspocus/server"; |
| 3 | +import { HocuspocusProvider } from "@hocuspocus/provider"; |
| 4 | +import { HocuspocusProvider as HocuspocusProviderPrev } from "@hocuspocus/provider-prev"; |
| 5 | +import * as Y from "yjs"; |
| 6 | +import { http, passthrough } from "msw"; |
| 7 | +import { WebSocket } from "ws"; |
| 8 | +import { OpenProjectApi } from "../../src/extensions/openProjectApi"; |
| 9 | +import { createTestToken } from "../helpers/tokenHelper"; |
| 10 | +import { server as apiMock } from "../mocks/node"; |
| 11 | + |
| 12 | +// Proves a real @hocuspocus/provider completes the connect -> authenticate -> load -> |
| 13 | +// sync handshake against our server, exercising the actual wire protocol. The previous |
| 14 | +// provider major runs too, so the one-major skew the version-skew guard tolerates is a |
| 15 | +// standing assertion rather than a claim. The server boots in-process so the existing |
| 16 | +// msw mocks intercept its outbound Rails calls. |
| 17 | +const PORT = 9678; |
| 18 | +// Must equal the token's resource_url: onAuthenticate sets resourceUrl = documentName |
| 19 | +// and validates they match. createTestToken() defaults to this URL. |
| 20 | +const DOC_NAME = "https://test.api/api/v3/documents/1"; |
| 21 | + |
| 22 | +let hocuspocus: Server; |
| 23 | + |
| 24 | +beforeAll(async () => { |
| 25 | + hocuspocus = new Server({ port: PORT, quiet: true, extensions: [new OpenProjectApi()] }); |
| 26 | + await hocuspocus.listen(); |
| 27 | +}); |
| 28 | + |
| 29 | +afterAll(async () => { |
| 30 | + await hocuspocus?.destroy(); |
| 31 | +}); |
| 32 | + |
| 33 | +beforeEach(() => { |
| 34 | + // setup.ts runs msw with onUnhandledRequest:'error', which also intercepts the ws |
| 35 | + // client's HTTP upgrade. Let the upgrade reach the in-process server; Rails calls to |
| 36 | + // test.api stay mocked by the default handlers. |
| 37 | + apiMock.use(http.all(/127\.0\.0\.1:9678/, () => passthrough())); |
| 38 | +}); |
| 39 | + |
| 40 | +interface ProviderConfig { |
| 41 | + url: string; |
| 42 | + name: string; |
| 43 | + token: string; |
| 44 | + document: Y.Doc; |
| 45 | + // The `ws` client, not the global WebSocket: msw patches the global and would drop |
| 46 | + // the connection to our in-process server. |
| 47 | + WebSocketPolyfill: typeof WebSocket; |
| 48 | +} |
| 49 | +interface ProviderInstance { |
| 50 | + synced: boolean; |
| 51 | + destroy(): void; |
| 52 | +} |
| 53 | +type ProviderConstructor = new (config: ProviderConfig) => ProviderInstance; |
| 54 | + |
| 55 | +const providerMajors: ReadonlyArray<readonly [string, ProviderConstructor]> = [ |
| 56 | + ["current", HocuspocusProvider as unknown as ProviderConstructor], |
| 57 | + ["previous major", HocuspocusProviderPrev as unknown as ProviderConstructor], |
| 58 | +]; |
| 59 | + |
| 60 | +describe.each(providerMajors)("@hocuspocus/provider (%s) <-> server", (_label, Provider) => { |
| 61 | + it("connects, authenticates, and syncs", async () => { |
| 62 | + const provider = new Provider({ |
| 63 | + url: `ws://127.0.0.1:${PORT}`, |
| 64 | + name: DOC_NAME, |
| 65 | + token: createTestToken(), |
| 66 | + document: new Y.Doc(), |
| 67 | + WebSocketPolyfill: WebSocket, |
| 68 | + }); |
| 69 | + |
| 70 | + await expect.poll(() => provider.synced, { timeout: 10000 }).toBe(true); |
| 71 | + |
| 72 | + provider.destroy(); |
| 73 | + }); |
| 74 | +}); |
0 commit comments