|
| 1 | +/** |
| 2 | + * @jest-environment jsdom |
| 3 | + */ |
| 4 | + |
| 5 | +/* |
| 6 | + * Copyright Contributors to the OpenCue Project |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * you may not use this file except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | + |
| 21 | +import "@testing-library/jest-dom"; |
| 22 | +import { fireEvent, render, screen, waitFor, within } from "@testing-library/react"; |
| 23 | +import type { Frame } from "@/app/frames/frame-columns"; |
| 24 | +import { eatFrames, killFrames, retryFrames } from "@/app/utils/action_utils"; |
| 25 | +import { FrameRangeSelector } from "@/components/ui/frame-range-selector"; |
| 26 | + |
| 27 | +// Mock the action layer so the test asserts the selected subset that gets |
| 28 | +// handed off, not the network behavior (which has its own coverage). |
| 29 | +jest.mock("@/app/utils/action_utils", () => ({ |
| 30 | + retryFrames: jest.fn(), |
| 31 | + eatFrames: jest.fn(), |
| 32 | + killFrames: jest.fn(), |
| 33 | +})); |
| 34 | + |
| 35 | +// The safety flag hook reads localStorage + window events; stub it "enabled". |
| 36 | +jest.mock("@/app/utils/use_disable_job_interaction", () => ({ |
| 37 | + useDisableJobInteraction: () => ({ disabled: false, setDisabled: jest.fn(), toggle: jest.fn() }), |
| 38 | +})); |
| 39 | + |
| 40 | +function makeFrame(number: number, state = "DEAD"): Frame { |
| 41 | + return { |
| 42 | + id: `frame-${number}`, |
| 43 | + name: `${number}-layer`, |
| 44 | + layerName: "layer", |
| 45 | + number, |
| 46 | + state, |
| 47 | + retryCount: 0, |
| 48 | + exitStatus: 0, |
| 49 | + dispatchOrder: number, |
| 50 | + startTime: 0, |
| 51 | + stopTime: 0, |
| 52 | + maxRss: "0", |
| 53 | + usedMemory: "0", |
| 54 | + reservedMemory: "0", |
| 55 | + reservedGpuMemory: "0", |
| 56 | + lastResource: "/", |
| 57 | + checkpointState: "", |
| 58 | + checkpointCount: 0, |
| 59 | + totalCoreTime: 0, |
| 60 | + lluTime: 0, |
| 61 | + totalGpuTime: 0, |
| 62 | + maxGpuMemory: "0", |
| 63 | + usedGpuMemory: "0", |
| 64 | + frameStateDisplayOverride: "", |
| 65 | + }; |
| 66 | +} |
| 67 | + |
| 68 | +const FRAMES = [1, 2, 3, 4, 5].map((n) => makeFrame(n)); |
| 69 | + |
| 70 | +function cell(number: number): HTMLElement { |
| 71 | + const el = document.querySelector(`[data-frame-number="${number}"]`); |
| 72 | + if (!el) throw new Error(`cell #${number} not found`); |
| 73 | + return el as HTMLElement; |
| 74 | +} |
| 75 | + |
| 76 | +beforeEach(() => { |
| 77 | + jest.clearAllMocks(); |
| 78 | +}); |
| 79 | + |
| 80 | +describe("FrameRangeSelector", () => { |
| 81 | + it("drag selects a contiguous range and feeds it into Retry", async () => { |
| 82 | + render(<FrameRangeSelector frames={FRAMES} username="tester" />); |
| 83 | + |
| 84 | + // Drag from frame #2 to frame #4 -> selects {2,3,4}. |
| 85 | + fireEvent.mouseDown(cell(2)); |
| 86 | + fireEvent.mouseEnter(cell(3)); |
| 87 | + fireEvent.mouseEnter(cell(4)); |
| 88 | + fireEvent.mouseUp(window); |
| 89 | + |
| 90 | + expect(screen.getByText(/Selected 3 frames \(#2–#4\)/)).toBeInTheDocument(); |
| 91 | + |
| 92 | + fireEvent.click(screen.getByRole("button", { name: "Retry" })); |
| 93 | + |
| 94 | + // Confirm in the dialog (there are two "Retry" buttons now; pick the |
| 95 | + // one inside the dialog). |
| 96 | + const dialog = await screen.findByRole("dialog"); |
| 97 | + fireEvent.click(within(dialog).getByRole("button", { name: "Retry" })); |
| 98 | + |
| 99 | + await waitFor(() => expect(retryFrames).toHaveBeenCalledTimes(1)); |
| 100 | + const handed = (retryFrames as jest.Mock).mock.calls[0][0] as Frame[]; |
| 101 | + expect(handed.map((f) => f.number).sort((a, b) => a - b)).toEqual([2, 3, 4]); |
| 102 | + expect(eatFrames).not.toHaveBeenCalled(); |
| 103 | + expect(killFrames).not.toHaveBeenCalled(); |
| 104 | + }); |
| 105 | + |
| 106 | + it("shift-click extends the selection from the anchor", async () => { |
| 107 | + render(<FrameRangeSelector frames={FRAMES} username="tester" />); |
| 108 | + |
| 109 | + // Anchor at #2 (single click), then shift-click #5 -> {2,3,4,5}. |
| 110 | + fireEvent.mouseDown(cell(2)); |
| 111 | + fireEvent.mouseUp(window); |
| 112 | + fireEvent.mouseDown(cell(5), { shiftKey: true }); |
| 113 | + |
| 114 | + expect(screen.getByText(/Selected 4 frames \(#2–#5\)/)).toBeInTheDocument(); |
| 115 | + }); |
| 116 | + |
| 117 | + it("routes Kill through a destructive confirm with the selected subset", async () => { |
| 118 | + render(<FrameRangeSelector frames={FRAMES} username="tester" />); |
| 119 | + |
| 120 | + fireEvent.mouseDown(cell(1)); |
| 121 | + fireEvent.mouseEnter(cell(2)); |
| 122 | + fireEvent.mouseUp(window); |
| 123 | + |
| 124 | + fireEvent.click(screen.getByRole("button", { name: "Kill" })); |
| 125 | + const dialog = await screen.findByRole("dialog"); |
| 126 | + fireEvent.click(within(dialog).getByRole("button", { name: "Kill" })); |
| 127 | + |
| 128 | + await waitFor(() => expect(killFrames).toHaveBeenCalledTimes(1)); |
| 129 | + const [handed, username, reason] = (killFrames as jest.Mock).mock.calls[0]; |
| 130 | + expect((handed as Frame[]).map((f) => f.number)).toEqual([1, 2]); |
| 131 | + expect(username).toBe("tester"); |
| 132 | + expect(reason).toMatch(/frame range selector/i); |
| 133 | + }); |
| 134 | + |
| 135 | + it("Clear removes the current selection", () => { |
| 136 | + render(<FrameRangeSelector frames={FRAMES} username="tester" />); |
| 137 | + |
| 138 | + fireEvent.mouseDown(cell(1)); |
| 139 | + fireEvent.mouseEnter(cell(3)); |
| 140 | + fireEvent.mouseUp(window); |
| 141 | + expect(screen.getByText(/Selected 3 frames/)).toBeInTheDocument(); |
| 142 | + |
| 143 | + fireEvent.click(screen.getByRole("button", { name: "Clear" })); |
| 144 | + expect(screen.getByText(/Drag to select a range of 5 frames/)).toBeInTheDocument(); |
| 145 | + }); |
| 146 | +}); |
0 commit comments