Skip to content

Commit 0011843

Browse files
authored
fix(desktop): keep project branches after reload (#2214)
1 parent 166f27b commit 0011843

4 files changed

Lines changed: 62 additions & 3 deletions

File tree

‎desktop/src/features/projects/hooks.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
22
import * as React from "react";
33

44
import { relayClient } from "@/shared/api/relayClient";
5+
import { getRelaySelf } from "@/features/moderation/lib/relaySelf";
56
import { getCachedRelayOrigin } from "@/shared/lib/mediaUrl";
67
import { signRelayEvent } from "@/shared/api/tauri";
78
import { getIdentity } from "@/shared/api/tauriIdentity";
@@ -345,9 +346,17 @@ function eventToRepoState(event: RelayEvent): RepoState {
345346
}
346347

347348
async function fetchRepoState(project: Project): Promise<RepoState | null> {
349+
const relaySelf = await getRelaySelf();
350+
const trustedAuthors = [
351+
...new Set(
352+
[project.owner, relaySelf].filter((value): value is string =>
353+
Boolean(value),
354+
),
355+
),
356+
];
348357
const events = await relayClient.fetchEvents({
349358
kinds: [KIND_REPO_STATE],
350-
authors: [project.owner],
359+
authors: trustedAuthors,
351360
"#d": [project.dtag],
352361
limit: 1,
353362
});

‎desktop/src/testing/e2eBridge.ts‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4748,6 +4748,36 @@ function mulberry32(seed: number) {
47484748
}
47494749

47504750
let mockProjectEventStore: RelayEvent[] | null = null;
4751+
const MOCK_PROJECT_BRANCHES_KEY = "buzz-e2e-project-branches";
4752+
4753+
function readMockProjectBranches(): Record<string, Record<string, string>> {
4754+
try {
4755+
return JSON.parse(
4756+
window.sessionStorage.getItem(MOCK_PROJECT_BRANCHES_KEY) ?? "{}",
4757+
);
4758+
} catch {
4759+
return {};
4760+
}
4761+
}
4762+
4763+
function writeMockProjectBranch(
4764+
dtag: string,
4765+
branch: string,
4766+
commit: string | null,
4767+
) {
4768+
const projects = readMockProjectBranches();
4769+
const branches = { ...(projects[dtag] ?? {}) };
4770+
if (commit) {
4771+
branches[branch] = commit;
4772+
} else {
4773+
delete branches[branch];
4774+
}
4775+
projects[dtag] = branches;
4776+
window.sessionStorage.setItem(
4777+
MOCK_PROJECT_BRANCHES_KEY,
4778+
JSON.stringify(projects),
4779+
);
4780+
}
47514781

47524782
function buildMockProjectEvents(): RelayEvent[] {
47534783
const events: RelayEvent[] = [];
@@ -4795,8 +4825,11 @@ function buildMockProjectEvents(): RelayEvent[] {
47954825
}`,
47964826
],
47974827
["refs/heads/main", "0123456789abcdef0123456789abcdef01234567"],
4828+
...Object.entries(readMockProjectBranches()[seed.dtag] ?? {}).map(
4829+
([branch, commit]) => [`refs/heads/${branch}`, commit],
4830+
),
47984831
],
4799-
owner,
4832+
getConfig()?.mock?.relaySelf ?? owner,
48004833
now - projectIndex,
48014834
`mock-repo-state-${seed.dtag}`.replace(/[^a-zA-Z0-9]/g, ""),
48024835
),
@@ -9427,6 +9460,9 @@ export function maybeInstallE2eTauriMocks() {
94279460
]);
94289461
repoState.created_at = Math.floor(Date.now() / 1000);
94299462
}
9463+
if (dtag) {
9464+
writeMockProjectBranch(dtag, input.newBranch, input.expectedCommit);
9465+
}
94309466
return {
94319467
branch: input.newBranch,
94329468
commit: input.expectedCommit,
@@ -9455,6 +9491,9 @@ export function maybeInstallE2eTauriMocks() {
94559491
);
94569492
repoState.created_at = Math.floor(Date.now() / 1000);
94579493
}
9494+
if (dtag) {
9495+
writeMockProjectBranch(dtag, input.branch, null);
9496+
}
94589497
return {
94599498
branch: input.branch,
94609499
commit: input.expectedCommit,

‎desktop/tests/e2e/project-pr-review.spec.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,10 @@ test("project branches can be created from the selected remote branch", async ({
712712
page,
713713
}) => {
714714
await enableProjectsFeature(page);
715-
await installMockBridge(page, { projectHeadBranch: "master" });
715+
await installMockBridge(page, {
716+
projectHeadBranch: "master",
717+
relaySelf: TEST_IDENTITIES.bob.pubkey,
718+
});
716719
await openBuzzProject(page);
717720

718721
await page.getByRole("button", { name: /main/ }).click();
@@ -745,6 +748,12 @@ test("project branches can be created from the selected remote branch", async ({
745748
() => window.__BUZZ_E2E_COMMANDS__ ?? [],
746749
);
747750
expect(commands).toContain("create_project_remote_branch");
751+
752+
await openBuzzProject(page);
753+
await page.getByRole("button", { name: /main/ }).click();
754+
await expect(
755+
page.getByRole("menuitemradio", { name: "feature/branch-management" }),
756+
).toBeVisible();
748757
});
749758

750759
test("project branches can be deleted but the default branch cannot", async ({

‎desktop/tests/helpers/bridge.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ export type MockAgentMemoryListing = {
129129
type MockBridgeOptions = {
130130
/** Advertised HEAD for the first mock project without adding that branch. */
131131
projectHeadBranch?: string;
132+
/** Relay NIP-11 identity used to sign authoritative repository state. */
133+
relaySelf?: string | null;
132134
/** Builderlab account returned by hosted-community onboarding. Null/omitted = signed out. */
133135
builderlabAuth?: { email?: string; name?: string; expiresAt: string } | null;
134136
/** Bound Builderlab Nostr identity. Null/omitted = not linked yet. */

0 commit comments

Comments
 (0)