-
Notifications
You must be signed in to change notification settings - Fork 901
feat(site): add download logs option #13466
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 1 commit
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
051217a
Download agent logs
BrunoQuaresma c9af899
Download workspace logs
BrunoQuaresma e479cf2
Use Logs instead of Agent Logs
BrunoQuaresma 9ccc9e1
Centralize items list
BrunoQuaresma 128f938
Init files with build logs
BrunoQuaresma 4d5c9cb
Add tests for the download logs dialog
BrunoQuaresma 11b528a
Add test to verify if download dialog is opening
BrunoQuaresma efcee2b
Add tests to download agent logs
BrunoQuaresma 895e942
Fix package.json
BrunoQuaresma 9ee7fd4
Commit Asher suggestions
BrunoQuaresma a71529a
Fix open download logs story
BrunoQuaresma f82a4e4
Fix dropdown arrow on light theme
BrunoQuaresma 967fc0b
Enable agent logs only when showing
BrunoQuaresma 73af195
Use web socket decorator and remove specific props for storybook
BrunoQuaresma f145ead
Refactor useAgentLogs to optimize loading
BrunoQuaresma 5baa052
Add prefetch back
BrunoQuaresma 40d2a65
Only fetch logs when clicking on download
BrunoQuaresma 35368ee
Add useAgentLogs test
BrunoQuaresma 10b4b71
Use Michael refactoring of useAgentLogs
BrunoQuaresma 8da2d28
Improve naming
BrunoQuaresma b54c72c
Improve naming
BrunoQuaresma 83f60dd
Fix hook usage
BrunoQuaresma 6f85490
allow pkg
sreya 270e04e
Rollback useAgentLogs implementation
BrunoQuaresma a6ab9fd
Add decorators to fix storybook
BrunoQuaresma c1f281c
Merge branch 'bq/download-logs' of https://github.com/coder/coder int…
BrunoQuaresma fab7d56
Apply Asher suggestions
BrunoQuaresma 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
Add useAgentLogs test
- Loading branch information
commit 35368ee06de19798e25bca43bb06bb9b5fd2c01a
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
137 changes: 137 additions & 0 deletions
137
site/src/modules/resources/AgentLogs/useAgentLogs.test.tsx
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,137 @@ | ||
import { act, renderHook, waitFor } from "@testing-library/react"; | ||
import WS from "jest-websocket-mock"; | ||
import { type QueryClient, QueryClientProvider } from "react-query"; | ||
import { API } from "api/api"; | ||
import * as APIModule from "api/api"; | ||
import { agentLogsKey } from "api/queries/workspaces"; | ||
import type { | ||
WorkspaceAgentLifecycle, | ||
WorkspaceAgentLog, | ||
} from "api/typesGenerated"; | ||
import { MockWorkspace, MockWorkspaceAgent } from "testHelpers/entities"; | ||
import { createTestQueryClient } from "testHelpers/renderHelpers"; | ||
import { type UseAgentLogsOptions, useAgentLogs } from "./useAgentLogs"; | ||
|
||
afterEach(() => { | ||
WS.clean(); | ||
}); | ||
|
||
describe("useAgentLogs", () => { | ||
it("should not fetch logs if disabled", async () => { | ||
const queryClient = createTestQueryClient(); | ||
const fetchSpy = jest.spyOn(API, "getWorkspaceAgentLogs"); | ||
const wsSpy = jest.spyOn(APIModule, "watchWorkspaceAgentLogs"); | ||
renderUseAgentLogs(queryClient, "ready", { enabled: false }); | ||
expect(fetchSpy).not.toHaveBeenCalled(); | ||
expect(wsSpy).not.toHaveBeenCalled(); | ||
BrunoQuaresma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
|
||
it("should return existing logs without network calls", async () => { | ||
const queryClient = createTestQueryClient(); | ||
queryClient.setQueryData( | ||
agentLogsKey(MockWorkspace.id, MockWorkspaceAgent.id), | ||
generateLogs(5), | ||
); | ||
const fetchSpy = jest.spyOn(API, "getWorkspaceAgentLogs"); | ||
const wsSpy = jest.spyOn(APIModule, "watchWorkspaceAgentLogs"); | ||
const { result } = renderUseAgentLogs(queryClient, "ready"); | ||
await waitFor(() => { | ||
expect(result.current).toHaveLength(5); | ||
}); | ||
expect(fetchSpy).not.toHaveBeenCalled(); | ||
expect(wsSpy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should fetch logs when empty", async () => { | ||
BrunoQuaresma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const queryClient = createTestQueryClient(); | ||
const fetchSpy = jest | ||
.spyOn(API, "getWorkspaceAgentLogs") | ||
.mockResolvedValueOnce(generateLogs(5)); | ||
const wsSpy = jest.spyOn(APIModule, "watchWorkspaceAgentLogs"); | ||
const { result } = renderUseAgentLogs(queryClient, "ready"); | ||
await waitFor(() => { | ||
expect(result.current).toHaveLength(5); | ||
}); | ||
expect(fetchSpy).toHaveBeenCalledWith(MockWorkspaceAgent.id); | ||
expect(wsSpy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should fetch logs and connect to websocket when agent is starting", async () => { | ||
const queryClient = createTestQueryClient(); | ||
const logs = generateLogs(5); | ||
const fetchSpy = jest | ||
.spyOn(API, "getWorkspaceAgentLogs") | ||
.mockResolvedValueOnce(logs); | ||
const wsSpy = jest.spyOn(APIModule, "watchWorkspaceAgentLogs"); | ||
new WS( | ||
`ws://localhost/api/v2/workspaceagents/${ | ||
MockWorkspaceAgent.id | ||
}/logs?follow&after=${logs[logs.length - 1].id}`, | ||
); | ||
const { result } = renderUseAgentLogs(queryClient, "starting"); | ||
await waitFor(() => { | ||
expect(result.current).toHaveLength(5); | ||
}); | ||
expect(fetchSpy).toHaveBeenCalledWith(MockWorkspaceAgent.id); | ||
expect(wsSpy).toHaveBeenCalledWith(MockWorkspaceAgent.id, { | ||
after: logs[logs.length - 1].id, | ||
onMessage: expect.any(Function), | ||
onError: expect.any(Function), | ||
}); | ||
}); | ||
|
||
it("update logs from websocket messages", async () => { | ||
const queryClient = createTestQueryClient(); | ||
const logs = generateLogs(5); | ||
jest.spyOn(API, "getWorkspaceAgentLogs").mockResolvedValueOnce(logs); | ||
const server = new WS( | ||
`ws://localhost/api/v2/workspaceagents/${ | ||
MockWorkspaceAgent.id | ||
}/logs?follow&after=${logs[logs.length - 1].id}`, | ||
); | ||
const { result } = renderUseAgentLogs(queryClient, "starting"); | ||
await waitFor(() => { | ||
expect(result.current).toHaveLength(5); | ||
}); | ||
await server.connected; | ||
BrunoQuaresma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
act(() => { | ||
server.send(JSON.stringify(generateLogs(3))); | ||
}); | ||
await waitFor(() => { | ||
expect(result.current).toHaveLength(8); | ||
}); | ||
}); | ||
}); | ||
|
||
function renderUseAgentLogs( | ||
queryClient: QueryClient, | ||
lifeCycleState: WorkspaceAgentLifecycle, | ||
options?: UseAgentLogsOptions, | ||
) { | ||
return renderHook( | ||
() => | ||
useAgentLogs( | ||
MockWorkspace.id, | ||
MockWorkspaceAgent.id, | ||
lifeCycleState, | ||
options, | ||
), | ||
{ | ||
wrapper: ({ children }) => ( | ||
<QueryClientProvider client={queryClient}> | ||
{children} | ||
</QueryClientProvider> | ||
), | ||
}, | ||
); | ||
} | ||
|
||
function generateLogs(count: number): WorkspaceAgentLog[] { | ||
return Array.from({ length: count }, (_, i) => ({ | ||
id: i, | ||
created_at: new Date().toISOString(), | ||
level: "info", | ||
output: `Log ${i}`, | ||
source_id: "", | ||
})); | ||
} |
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.