npm run format

This commit is contained in:
Benjamin Lu
2025-08-05 09:57:28 -04:00
parent 50feb27339
commit c53f197de2
129 changed files with 10575 additions and 7230 deletions

View File

@@ -1,20 +1,30 @@
import type { MovingInputLink } from "@/lib/litegraph/src/canvas/MovingInputLink"
import type { LinkNetwork } from "@/lib/litegraph/src/interfaces"
import type { ISlotType } from "@/lib/litegraph/src/interfaces"
import { test as baseTest, describe, expect, vi } from 'vitest'
import { describe, expect, test as baseTest, vi } from "vitest"
import { LinkConnector } from "@/lib/litegraph/src/canvas/LinkConnector"
import { ToInputRenderLink } from "@/lib/litegraph/src/canvas/ToInputRenderLink"
import { LGraph, LGraphNode, LLink, Reroute, type RerouteId } from "@/lib/litegraph/src/litegraph"
import { LinkDirection } from "@/lib/litegraph/src/types/globalEnums"
import { LinkConnector } from '@/lib/litegraph/src/canvas/LinkConnector'
import type { MovingInputLink } from '@/lib/litegraph/src/canvas/MovingInputLink'
import { ToInputRenderLink } from '@/lib/litegraph/src/canvas/ToInputRenderLink'
import type { LinkNetwork } from '@/lib/litegraph/src/interfaces'
import type { ISlotType } from '@/lib/litegraph/src/interfaces'
import {
LGraph,
LGraphNode,
LLink,
Reroute,
type RerouteId
} from '@/lib/litegraph/src/litegraph'
import { LinkDirection } from '@/lib/litegraph/src/types/globalEnums'
interface TestContext {
network: LinkNetwork & { add(node: LGraphNode): void }
connector: LinkConnector
setConnectingLinks: ReturnType<typeof vi.fn>
createTestNode: (id: number, slotType?: ISlotType) => LGraphNode
createTestLink: (id: number, sourceId: number, targetId: number, slotType?: ISlotType) => LLink
createTestLink: (
id: number,
sourceId: number,
targetId: number,
slotType?: ISlotType
) => LLink
}
const test = baseTest.extend<TestContext>({
@@ -34,13 +44,17 @@ const test = baseTest.extend<TestContext>({
return link
},
removeFloatingLink: (link: LLink) => floatingLinks.delete(link.id),
getReroute: ((id: RerouteId | null | undefined) => id == null ? undefined : reroutes.get(id)) as LinkNetwork["getReroute"],
getReroute: ((id: RerouteId | null | undefined) =>
id == null ? undefined : reroutes.get(id)) as LinkNetwork['getReroute'],
removeReroute: (id: number) => reroutes.delete(id),
add: (node: LGraphNode) => graph.add(node),
add: (node: LGraphNode) => graph.add(node)
})
},
setConnectingLinks: async ({}, use: (mock: ReturnType<typeof vi.fn>) => Promise<void>) => {
setConnectingLinks: async (
{},
use: (mock: ReturnType<typeof vi.fn>) => Promise<void>
) => {
const mock = vi.fn()
await use(mock)
},
@@ -51,32 +65,34 @@ const test = baseTest.extend<TestContext>({
createTestNode: async ({ network }, use) => {
await use((id: number): LGraphNode => {
const node = new LGraphNode("test")
const node = new LGraphNode('test')
node.id = id
network.add(node)
return node
})
},
createTestLink: async ({ network }, use) => {
await use((
id: number,
sourceId: number,
targetId: number,
slotType: ISlotType = "number",
): LLink => {
const link = new LLink(id, slotType, sourceId, 0, targetId, 0)
network.links.set(link.id, link)
return link
})
},
await use(
(
id: number,
sourceId: number,
targetId: number,
slotType: ISlotType = 'number'
): LLink => {
const link = new LLink(id, slotType, sourceId, 0, targetId, 0)
network.links.set(link.id, link)
return link
}
)
}
})
describe("LinkConnector", () => {
test("should initialize with default state", ({ connector }) => {
describe('LinkConnector', () => {
test('should initialize with default state', ({ connector }) => {
expect(connector.state).toEqual({
connectingTo: undefined,
multi: false,
draggingExistingLinks: false,
draggingExistingLinks: false
})
expect(connector.renderLinks).toEqual([])
expect(connector.inputLinks).toEqual([])
@@ -84,14 +100,18 @@ describe("LinkConnector", () => {
expect(connector.hiddenReroutes.size).toBe(0)
})
describe("Moving Input Links", () => {
test("should handle moving input links", ({ network, connector, createTestNode }) => {
describe('Moving Input Links', () => {
test('should handle moving input links', ({
network,
connector,
createTestNode
}) => {
const sourceNode = createTestNode(1)
const targetNode = createTestNode(2)
const slotType: ISlotType = "number"
sourceNode.addOutput("out", slotType)
targetNode.addInput("in", slotType)
const slotType: ISlotType = 'number'
sourceNode.addOutput('out', slotType)
targetNode.addInput('in', slotType)
const link = new LLink(1, slotType, 1, 0, 2, 0)
network.links.set(link.id, link)
@@ -99,29 +119,36 @@ describe("LinkConnector", () => {
connector.moveInputLink(network, targetNode.inputs[0])
expect(connector.state.connectingTo).toBe("input")
expect(connector.state.connectingTo).toBe('input')
expect(connector.state.draggingExistingLinks).toBe(true)
expect(connector.inputLinks).toContain(link)
expect(link._dragging).toBe(true)
})
test("should not move input link if already connecting", ({ connector, network }) => {
connector.state.connectingTo = "input"
test('should not move input link if already connecting', ({
connector,
network
}) => {
connector.state.connectingTo = 'input'
expect(() => {
connector.moveInputLink(network, { link: 1 } as any)
}).toThrow("Already dragging links.")
}).toThrow('Already dragging links.')
})
})
describe("Moving Output Links", () => {
test("should handle moving output links", ({ network, connector, createTestNode }) => {
describe('Moving Output Links', () => {
test('should handle moving output links', ({
network,
connector,
createTestNode
}) => {
const sourceNode = createTestNode(1)
const targetNode = createTestNode(2)
const slotType: ISlotType = "number"
sourceNode.addOutput("out", slotType)
targetNode.addInput("in", slotType)
const slotType: ISlotType = 'number'
sourceNode.addOutput('out', slotType)
targetNode.addInput('in', slotType)
const link = new LLink(1, slotType, 1, 0, 2, 0)
network.links.set(link.id, link)
@@ -129,55 +156,71 @@ describe("LinkConnector", () => {
connector.moveOutputLink(network, sourceNode.outputs[0])
expect(connector.state.connectingTo).toBe("output")
expect(connector.state.connectingTo).toBe('output')
expect(connector.state.draggingExistingLinks).toBe(true)
expect(connector.state.multi).toBe(true)
expect(connector.outputLinks).toContain(link)
expect(link._dragging).toBe(true)
})
test("should not move output link if already connecting", ({ connector, network }) => {
connector.state.connectingTo = "output"
test('should not move output link if already connecting', ({
connector,
network
}) => {
connector.state.connectingTo = 'output'
expect(() => {
connector.moveOutputLink(network, { links: [1] } as any)
}).toThrow("Already dragging links.")
}).toThrow('Already dragging links.')
})
})
describe("Dragging New Links", () => {
test("should handle dragging new link from output", ({ network, connector, createTestNode }) => {
describe('Dragging New Links', () => {
test('should handle dragging new link from output', ({
network,
connector,
createTestNode
}) => {
const sourceNode = createTestNode(1)
const slotType: ISlotType = "number"
sourceNode.addOutput("out", slotType)
const slotType: ISlotType = 'number'
sourceNode.addOutput('out', slotType)
connector.dragNewFromOutput(network, sourceNode, sourceNode.outputs[0])
expect(connector.state.connectingTo).toBe("input")
expect(connector.state.connectingTo).toBe('input')
expect(connector.renderLinks.length).toBe(1)
expect(connector.state.draggingExistingLinks).toBe(false)
})
test("should handle dragging new link from input", ({ network, connector, createTestNode }) => {
test('should handle dragging new link from input', ({
network,
connector,
createTestNode
}) => {
const targetNode = createTestNode(1)
const slotType: ISlotType = "number"
targetNode.addInput("in", slotType)
const slotType: ISlotType = 'number'
targetNode.addInput('in', slotType)
connector.dragNewFromInput(network, targetNode, targetNode.inputs[0])
expect(connector.state.connectingTo).toBe("output")
expect(connector.state.connectingTo).toBe('output')
expect(connector.renderLinks.length).toBe(1)
expect(connector.state.draggingExistingLinks).toBe(false)
})
})
describe("Dragging from reroutes", () => {
test("should handle dragging from reroutes", ({ network, connector, createTestNode, createTestLink }) => {
describe('Dragging from reroutes', () => {
test('should handle dragging from reroutes', ({
network,
connector,
createTestNode,
createTestLink
}) => {
const originNode = createTestNode(1)
const targetNode = createTestNode(2)
const output = originNode.addOutput("out", "number")
targetNode.addInput("in", "number")
const output = originNode.addOutput('out', 'number')
targetNode.addInput('in', 'number')
const link = createTestLink(1, 1, 2)
const reroute = new Reroute(1, network, [0, 0], undefined, [link.id])
@@ -186,13 +229,13 @@ describe("LinkConnector", () => {
connector.dragFromReroute(network, reroute)
expect(connector.state.connectingTo).toBe("input")
expect(connector.state.connectingTo).toBe('input')
expect(connector.state.draggingExistingLinks).toBe(false)
expect(connector.renderLinks.length).toBe(1)
const renderLink = connector.renderLinks[0]
expect(renderLink instanceof ToInputRenderLink).toBe(true)
expect(renderLink.toType).toEqual("input")
expect(renderLink.toType).toEqual('input')
expect(renderLink.node).toEqual(originNode)
expect(renderLink.fromSlot).toEqual(output)
expect(renderLink.fromReroute).toEqual(reroute)
@@ -201,13 +244,13 @@ describe("LinkConnector", () => {
})
})
describe("Reset", () => {
test("should reset state and clear links", ({ network, connector }) => {
connector.state.connectingTo = "input"
describe('Reset', () => {
test('should reset state and clear links', ({ network, connector }) => {
connector.state.connectingTo = 'input'
connector.state.multi = true
connector.state.draggingExistingLinks = true
const link = new LLink(1, "number", 1, 0, 2, 0)
const link = new LLink(1, 'number', 1, 0, 2, 0)
link._dragging = true
connector.inputLinks.push(link)
@@ -221,7 +264,7 @@ describe("LinkConnector", () => {
expect(connector.state).toEqual({
connectingTo: undefined,
multi: false,
draggingExistingLinks: false,
draggingExistingLinks: false
})
expect(connector.renderLinks).toEqual([])
expect(connector.inputLinks).toEqual([])
@@ -232,37 +275,40 @@ describe("LinkConnector", () => {
})
})
describe("Event Handling", () => {
test("should handle event listeners until reset", ({ connector, createTestNode }) => {
describe('Event Handling', () => {
test('should handle event listeners until reset', ({
connector,
createTestNode
}) => {
const listener = vi.fn()
connector.listenUntilReset("input-moved", listener)
connector.listenUntilReset('input-moved', listener)
const sourceNode = createTestNode(1)
const mockRenderLink = {
node: sourceNode,
fromSlot: { name: "out", type: "number" },
fromSlot: { name: 'out', type: 'number' },
fromPos: [0, 0],
fromDirection: LinkDirection.RIGHT,
toType: "input",
link: new LLink(1, "number", 1, 0, 2, 0),
toType: 'input',
link: new LLink(1, 'number', 1, 0, 2, 0)
} as MovingInputLink
connector.events.dispatch("input-moved", mockRenderLink)
connector.events.dispatch('input-moved', mockRenderLink)
expect(listener).toHaveBeenCalled()
connector.reset()
connector.events.dispatch("input-moved", mockRenderLink)
connector.events.dispatch('input-moved', mockRenderLink)
expect(listener).toHaveBeenCalledTimes(1)
})
})
describe("Export", () => {
test("should export current state", ({ network, connector }) => {
connector.state.connectingTo = "input"
describe('Export', () => {
test('should export current state', ({ network, connector }) => {
connector.state.connectingTo = 'input'
connector.state.multi = true
const link = new LLink(1, "number", 1, 0, 2, 0)
const link = new LLink(1, 'number', 1, 0, 2, 0)
connector.inputLinks.push(link)
const exported = connector.export(network)