Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixed tests
  • Loading branch information
Kira-Pilot committed Sep 21, 2022
commit 80c5fac4a0773a801a4e96b1f5014e4aea0ab952
1 change: 1 addition & 0 deletions site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"cronstrue": "2.11.0",
"dayjs": "1.11.4",
"emoji-mart": "^5.2.1",
"eventsourcemock": "^2.0.0",
"formik": "^2.2.9",
"front-matter": "4.0.2",
"history": "5.3.0",
Expand Down
1 change: 1 addition & 0 deletions site/src/@types/eventsourcemock.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module "eventsourcemock"
9 changes: 5 additions & 4 deletions site/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,14 @@ export const getWorkspace = async (
}

/**
*
* @param workspaceId
*
* @param workspaceId
* @returns An EventSource that emits workspace event objects (ServerSentEvent)
*/
export const watchWorkspace = (workspaceId: string): EventSource => {
return new EventSource(`${location.protocol}//${location.host}/api/v2/workspaces/${workspaceId}/watch`,
{ withCredentials: true }
return new EventSource(
`${location.protocol}//${location.host}/api/v2/workspaces/${workspaceId}/watch`,
{ withCredentials: true },
)
}

Expand Down
36 changes: 34 additions & 2 deletions site/src/pages/WorkspacePage/WorkspacePage.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable @typescript-eslint/no-floating-promises */
import { fireEvent, screen, waitFor, within } from "@testing-library/react"
import EventSource from "eventsourcemock"
import i18next from "i18next"
import { rest } from "msw"
import * as api from "../../api/api"
Expand All @@ -22,6 +23,7 @@ import {
MockWorkspaceAgentConnecting,
MockWorkspaceAgentDisconnected,
MockWorkspaceBuild,
MockWorkspaceResource2,
renderWithAuth,
} from "../../testHelpers/renderHelpers"
import { server } from "../../testHelpers/server"
Expand Down Expand Up @@ -70,6 +72,11 @@ const testStatus = async (ws: Workspace, label: string) => {

beforeEach(() => {
jest.resetAllMocks()

// mocking out EventSource for SSE
Object.defineProperty(window, "EventSource", {
value: EventSource,
})
})

describe("WorkspacePage", () => {
Expand Down Expand Up @@ -194,18 +201,43 @@ describe("WorkspacePage", () => {
describe("Resources", () => {
it("shows the status of each agent in each resource", async () => {
const getTemplateMock = jest.spyOn(api, "getTemplate").mockResolvedValueOnce(MockTemplate)

const workspaceWithResources = {
...MockWorkspace,
latest_build: {
...MockWorkspaceBuild,
resources: [
{
...MockWorkspaceResource2,
agents: [
MockWorkspaceAgent,
MockWorkspaceAgentDisconnected,
MockWorkspaceAgentConnecting,
],
},
],
},
}

server.use(
rest.get(`/api/v2/users/:username/workspace/:workspaceName`, (req, res, ctx) => {
return res(ctx.status(200), ctx.json(workspaceWithResources))
}),
)

renderWithAuth(<WorkspacePage />, {
route: `/@${MockWorkspace.owner_name}/${MockWorkspace.name}`,
path: "/@:username/:workspace",
})

const agent1Names = await screen.findAllByText(MockWorkspaceAgent.name)
expect(agent1Names.length).toEqual(2)
expect(agent1Names.length).toEqual(1)
const agent2Names = await screen.findAllByText(MockWorkspaceAgentDisconnected.name)
expect(agent2Names.length).toEqual(2)
const agent1Status = await screen.findAllByText(
DisplayAgentStatusLanguage[MockWorkspaceAgent.status],
)
expect(agent1Status.length).toEqual(4)
expect(agent1Status.length).toEqual(1)
const agentDisconnected = await screen.findAllByText(
DisplayAgentStatusLanguage[MockWorkspaceAgentDisconnected.status],
)
Expand Down
2 changes: 1 addition & 1 deletion site/src/testHelpers/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ export const MockWorkspaceAgentOutdated: TypesGen.WorkspaceAgent = {

export const MockWorkspaceAgentConnecting: TypesGen.WorkspaceAgent = {
...MockWorkspaceAgent,
id: "test-workspace-agent-2",
id: "test-workspace-agent-connecting",
name: "another-workspace-agent",
status: "connecting",
version: "",
Expand Down
37 changes: 18 additions & 19 deletions site/src/xServices/workspace/workspaceXService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Permissions = Record<keyof ReturnType<typeof permissionsToCheck>, boolean>

export interface WorkspaceContext {
// our server side events instance
eventSource?: EventSource,
eventSource?: EventSource
workspace?: TypesGen.Workspace
template?: TypesGen.Template
build?: TypesGen.WorkspaceBuild
Expand All @@ -40,12 +40,12 @@ export interface WorkspaceContext {
// permissions
permissions?: Permissions
checkPermissionsError?: Error | unknown
userId?: string,
userId?: string
}

export type WorkspaceEvent =
| { type: "GET_WORKSPACE"; workspaceName: string; username: string }
| { type: "REFRESH_WORKSPACE", data: TypesGen.ServerSentEvent["data"] }
| { type: "REFRESH_WORKSPACE"; data: TypesGen.ServerSentEvent["data"] }
| { type: "START" }
| { type: "STOP" }
| { type: "ASK_DELETE" }
Expand All @@ -54,9 +54,9 @@ export type WorkspaceEvent =
| { type: "UPDATE" }
| { type: "CANCEL" }
| { type: "LOAD_MORE_BUILDS" }
| { type: "CHECK_REFRESH_TIMELINE", data: TypesGen.ServerSentEvent["data"] }
| { type: "CHECK_REFRESH_TIMELINE"; data: TypesGen.ServerSentEvent["data"] }
| { type: "REFRESH_TIMELINE" }
| { type: "EVENT_SOURCE_ERROR", error: Error | unknown }
| { type: "EVENT_SOURCE_ERROR"; error: Error | unknown }

export const checks = {
readWorkspace: "readWorkspace",
Expand Down Expand Up @@ -114,7 +114,7 @@ export const workspaceMachine = createMachine(
}
listening: {
data: TypesGen.ServerSentEvent
},
}
getBuilds: {
data: TypesGen.WorkspaceBuild[]
}
Expand Down Expand Up @@ -203,32 +203,32 @@ export const workspaceMachine = createMachine(
initial: "gettingEvents",
states: {
gettingEvents: {
entry: ['clearRefreshWorkspaceWarning', 'initializeEventSource'],
entry: ["clearRefreshWorkspaceWarning", "initializeEventSource"],
exit: "closeEventSource",
invoke: {
src: "listening",
},
on: {
REFRESH_WORKSPACE: {
actions: ["refreshWorkspace"]
actions: ["refreshWorkspace"],
},
EVENT_SOURCE_ERROR: {
target: "error"
target: "error",
},
CHECK_REFRESH_TIMELINE: {
actions: ["refreshTimeline"]
}
actions: ["refreshTimeline"],
},
},
},
error: {
entry: "assignRefreshWorkspaceWarning",
after: {
"1000": {
target: 'gettingEvents'
}
}
}
}
target: "gettingEvents",
},
},
},
},
},
build: {
initial: "idle",
Expand Down Expand Up @@ -498,7 +498,7 @@ export const workspaceMachine = createMachine(
// SSE related actions
// open a new EventSource so we can stream SSE
initializeEventSource: assign({
eventSource: (context) => context.workspace && API.watchWorkspace(context.workspace.id)
eventSource: (context) => context.workspace && API.watchWorkspace(context.workspace.id),
}),
closeEventSource: (context) => context.eventSource && context.eventSource.close(),
refreshWorkspace: assign({
Expand Down Expand Up @@ -640,10 +640,9 @@ export const workspaceMachine = createMachine(

// handle any sse implementation exceptions
context.eventSource.onerror = () => {
context.eventSource && context.eventSource.close();
context.eventSource && context.eventSource.close()
send({ type: "EVENT_SOURCE_ERROR", error: "sse error" })
}

},
getBuilds: async (context) => {
if (context.workspace) {
Expand Down
5 changes: 5 additions & 0 deletions site/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7144,6 +7144,11 @@ events@^3.0.0, events@^3.2.0, events@^3.3.0:
resolved "https://registry.yarnpkg.com/events/-/events-3.3.0.tgz#31a95ad0a924e2d2c419a813aeb2c4e878ea7400"
integrity sha512-mQw+2fkQbALzQ7V0MY0IqdnXNOeTtP4r0lN9z7AAawCXgqea7bDii20AYrIBrFd/Hx0M2Ocz6S111CaFkUcb0Q==

eventsourcemock@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/eventsourcemock/-/eventsourcemock-2.0.0.tgz#83f66bc537e4909ef385bf84272e300737954ef0"
integrity sha512-tSmJnuE+h6A8/hLRg0usf1yL+Q8w01RQtmg0Uzgoxk/HIPZrIUeAr/A4es/8h1wNsoG8RdiESNQLTKiNwbSC3Q==

evp_bytestokey@^1.0.0, evp_bytestokey@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/evp_bytestokey/-/evp_bytestokey-1.0.3.tgz#7fcbdb198dc71959432efe13842684e0525acb02"
Expand Down