-
Notifications
You must be signed in to change notification settings - Fork 48
Polish CLI init: handle plugin replacement and fix formatting #591
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2f9c205
Polish CLI init: handle plugin replacement and fix formatting
huntercaron 0f4d85a
Fix code-link replacement cleanup
huntercaron 75f5068
Fix lint errors in sockets.test.ts
huntercaron 9164388
Close plugin with info toast when replaced by another connection
huntercaron 2b68506
Add break after closePlugin to satisfy no-fallthrough lint rule
huntercaron 8f46959
Simplify cleanupResources: drop redundant setActiveSocket(null) branch
huntercaron 9cfb8a6
Return from closePlugin instead of falling through switch case
huntercaron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| import { CLOSE_CODE_REPLACED, type ProjectInfo } from "@code-link/shared" | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest" | ||
| import { createSocketConnectionController } from "./sockets.ts" | ||
|
|
||
| vi.mock("framer-plugin", () => ({ | ||
| framer: { | ||
| getProjectInfo: vi.fn(() => Promise.resolve({ id: "project-id", name: "Project Name" })), | ||
| }, | ||
| })) | ||
|
|
||
| class MockEventTarget { | ||
| private listeners = new Map<string, Set<EventListener>>() | ||
|
|
||
| addEventListener = vi.fn((type: string, listener: EventListener) => { | ||
| const listenersForType = this.listeners.get(type) ?? new Set<EventListener>() | ||
| listenersForType.add(listener) | ||
| this.listeners.set(type, listenersForType) | ||
| }) | ||
|
|
||
| removeEventListener = vi.fn((type: string, listener: EventListener) => { | ||
| this.listeners.get(type)?.delete(listener) | ||
| }) | ||
|
|
||
| dispatch(type: string) { | ||
| for (const listener of this.listeners.get(type) ?? []) { | ||
| listener(new Event(type)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class MockWebSocket { | ||
| static readonly CONNECTING = 0 | ||
| static readonly OPEN = 1 | ||
| static readonly CLOSING = 2 | ||
| static readonly CLOSED = 3 | ||
| static instances: MockWebSocket[] = [] | ||
|
|
||
| readonly url: string | ||
| readyState = MockWebSocket.CONNECTING | ||
| onopen: ((event: Event) => void) | null = null | ||
| onclose: ((event: CloseEventLike) => void) | null = null | ||
| onerror: ((event: Event) => void) | null = null | ||
| onmessage: ((event: MessageEvent) => void) | null = null | ||
| send = vi.fn() | ||
|
|
||
| constructor(url: string) { | ||
| this.url = url | ||
| MockWebSocket.instances.push(this) | ||
| } | ||
|
|
||
| close(code = 1000, reason = "") { | ||
| this.readyState = MockWebSocket.CLOSED | ||
| this.onclose?.({ code, reason, wasClean: true }) | ||
| } | ||
|
|
||
| emitClose(code = 1000, reason = "") { | ||
| this.readyState = MockWebSocket.CLOSED | ||
| this.onclose?.({ code, reason, wasClean: true }) | ||
| } | ||
| } | ||
|
|
||
| interface CloseEventLike { | ||
| code: number | ||
| reason: string | ||
| wasClean: boolean | ||
| } | ||
|
|
||
| describe("createSocketConnectionController", () => { | ||
| const originalDocument = globalThis.document | ||
| const originalWindow = globalThis.window | ||
| const originalWebSocket = globalThis.WebSocket | ||
| let mockDocument: MockEventTarget & { visibilityState: DocumentVisibilityState } | ||
| let mockWindow: MockEventTarget | ||
|
|
||
| beforeEach(() => { | ||
| vi.useFakeTimers() | ||
| MockWebSocket.instances = [] | ||
|
|
||
| mockDocument = Object.assign(new MockEventTarget(), { | ||
| visibilityState: "visible" as DocumentVisibilityState, | ||
| }) | ||
| Object.defineProperty(mockDocument, "visibilityState", { | ||
| value: "visible", | ||
| configurable: true, | ||
| writable: true, | ||
| }) | ||
|
|
||
| mockWindow = new MockEventTarget() | ||
|
|
||
| globalThis.document = mockDocument as unknown as Document | ||
| globalThis.window = mockWindow as unknown as Window & typeof globalThis | ||
| globalThis.WebSocket = MockWebSocket as unknown as typeof WebSocket | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers() | ||
| vi.restoreAllMocks() | ||
| globalThis.document = originalDocument | ||
| globalThis.window = originalWindow | ||
| globalThis.WebSocket = originalWebSocket | ||
| }) | ||
|
|
||
| it("cleans up listeners and timers after a replaced close followed by stop", () => { | ||
| const setSocket = vi.fn() | ||
| const onReplaced = vi.fn() | ||
|
|
||
| const controller = createSocketConnectionController({ | ||
| project: { id: "project-id", name: "Project Name" } satisfies ProjectInfo, | ||
| setSocket, | ||
| onMessage: vi.fn(() => Promise.resolve()), | ||
| onConnected: vi.fn(), | ||
| onDisconnected: vi.fn(), | ||
| onReplaced, | ||
| }) | ||
|
|
||
| controller.start() | ||
|
|
||
| expect(mockDocument.addEventListener).toHaveBeenCalledWith("visibilitychange", expect.any(Function)) | ||
| expect(mockWindow.addEventListener).toHaveBeenCalledWith("focus", expect.any(Function)) | ||
| expect(MockWebSocket.instances).toHaveLength(1) | ||
|
|
||
| mockWindow.dispatch("focus") | ||
| expect(vi.getTimerCount()).toBe(2) | ||
|
|
||
| MockWebSocket.instances[0]?.emitClose(CLOSE_CODE_REPLACED, "replaced") | ||
| controller.stop() | ||
|
|
||
| expect(onReplaced).toHaveBeenCalledOnce() | ||
| expect(mockDocument.removeEventListener).toHaveBeenCalledWith("visibilitychange", expect.any(Function)) | ||
| expect(mockWindow.removeEventListener).toHaveBeenCalledWith("focus", expect.any(Function)) | ||
| expect(vi.getTimerCount()).toBe(0) | ||
| expect(setSocket).toHaveBeenLastCalledWith(null) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.