-
Notifications
You must be signed in to change notification settings - Fork 0
Show workers as their own labeled section in active sessions #159
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
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| import { afterEach, describe, expect, test } from "bun:test"; | ||
| import { join } from "node:path"; | ||
| import { tempCyrusHomeFixture } from "@cyrus/test/fixtures/cyrus-home"; | ||
| import { YAML } from "bun"; | ||
|
|
||
| const CLI = join(import.meta.dir, "../../cli.ts"); | ||
| const tempHome = tempCyrusHomeFixture(afterEach, "cyrus-rename-"); | ||
|
|
||
| async function seedConfig( | ||
| home: string, | ||
| config: { token?: string; id?: string; name?: string } | ||
| ): Promise<void> { | ||
| await Bun.write(join(home, "config.yml"), YAML.stringify(config)); | ||
| } | ||
|
|
||
| async function readConfig( | ||
| home: string | ||
| ): Promise<{ token?: string; id?: string; name?: string }> { | ||
| return YAML.parse(await Bun.file(join(home, "config.yml")).text()) as { | ||
| token?: string; | ||
| id?: string; | ||
| name?: string; | ||
| }; | ||
| } | ||
|
|
||
| async function runRename( | ||
| home: string, | ||
| name: string, | ||
| serverUrl: string | ||
| ): Promise<{ exitCode: number; stdout: string; stderr: string }> { | ||
| const proc = Bun.spawn(["bun", CLI, "rename", name], { | ||
| cwd: join(import.meta.dir, "../../.."), | ||
| env: { | ||
| ...process.env, | ||
| CYRUS_HOME: home, | ||
| CLI_PUBLIC_SERVER_URL: serverUrl, | ||
| }, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([ | ||
| new Response(proc.stdout).text(), | ||
| new Response(proc.stderr).text(), | ||
| proc.exited, | ||
| ]); | ||
| return { exitCode, stdout, stderr }; | ||
| } | ||
|
|
||
| describe("cyrusd rename", () => { | ||
| test("pushes the new name to the server and updates local config on success", async () => { | ||
| const home = await tempHome(); | ||
| await seedConfig(home, { token: "test-token", name: "old-name" }); | ||
|
|
||
| using server = Bun.serve({ | ||
| port: 0, | ||
| fetch(req) { | ||
| const url = new URL(req.url); | ||
| if (url.pathname === "/api/auth/update-session") { | ||
| return Response.json({ session: { workerName: "new-name" } }); | ||
| } | ||
| return new Response("not found", { status: 404 }); | ||
| }, | ||
| }); | ||
|
|
||
| const result = await runRename(home, "new-name", server.url.toString()); | ||
|
|
||
| expect(result.exitCode).toBe(0); | ||
| expect(await readConfig(home)).toMatchObject({ name: "new-name" }); | ||
| }); | ||
|
|
||
| test("fails and leaves local config untouched when the server rejects the update", async () => { | ||
| const home = await tempHome(); | ||
| await seedConfig(home, { token: "test-token", name: "old-name" }); | ||
|
|
||
| using server = Bun.serve({ | ||
| port: 0, | ||
| fetch(req) { | ||
| const url = new URL(req.url); | ||
| if (url.pathname === "/api/auth/update-session") { | ||
| return Response.json( | ||
| { message: "Internal Server Error" }, | ||
| { status: 500 } | ||
| ); | ||
| } | ||
| return new Response("not found", { status: 404 }); | ||
| }, | ||
| }); | ||
|
|
||
| const result = await runRename(home, "new-name", server.url.toString()); | ||
|
|
||
| expect(result.exitCode).toBe(1); | ||
| expect(await readConfig(home)).toMatchObject({ name: "old-name" }); | ||
| }); | ||
|
|
||
| test("fails without contacting the server when not logged in", async () => { | ||
| const home = await tempHome(); | ||
|
|
||
| using server = Bun.serve({ | ||
| port: 0, | ||
| fetch() { | ||
| throw new Error("should not be called"); | ||
| }, | ||
| }); | ||
|
|
||
| const result = await runRename(home, "new-name", server.url.toString()); | ||
|
|
||
| expect(result.exitCode).toBe(1); | ||
| expect(result.stderr + result.stdout).toContain("logged in"); | ||
| }); | ||
| }); |
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 |
|---|---|---|
| @@ -1,16 +1,39 @@ | ||
| import { wsTicketClientPlugin } from "@soorya-u/better-auth-ws-ticket/client"; | ||
| import { createAuthClient } from "better-auth/client"; | ||
| import { deviceAuthorizationClient } from "better-auth/client/plugins"; | ||
| import { | ||
| deviceAuthorizationClient, | ||
| inferAdditionalFields, | ||
| } from "better-auth/client/plugins"; | ||
| import { Result } from "better-result"; | ||
| import { get } from "@/store/config"; | ||
| import { env } from "./env"; | ||
|
|
||
| export const authClient = createAuthClient({ | ||
| baseURL: env.CLI_PUBLIC_SERVER_URL, | ||
| plugins: [deviceAuthorizationClient(), wsTicketClientPlugin()], | ||
| plugins: [ | ||
| deviceAuthorizationClient(), | ||
| wsTicketClientPlugin(), | ||
| inferAdditionalFields({ | ||
| session: { | ||
| workerName: { type: "string", required: false }, | ||
| }, | ||
| }), | ||
| ], | ||
| fetchOptions: { | ||
| auth: { | ||
| type: "Bearer", | ||
| token: async () => (await get("token")) ?? undefined, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| export async function syncWorkerName( | ||
| name: string | ||
| ): Promise<Result<void, string>> { | ||
| const { error } = await authClient.updateSession({ workerName: name }); | ||
| if (error) | ||
| return Result.err( | ||
| error.message || "Failed to sync worker name with the server" | ||
| ); | ||
| return Result.ok(); | ||
| } |
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
1 change: 1 addition & 0 deletions
1
apps/server/src/db/migrations/20260806160054_add_worker_name_to_session/migration.sql
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 @@ | ||
| ALTER TABLE `session` ADD `worker_name` text; |
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.