Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions CONTEXT.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ _Avoid_: archive import, replace, synchronize
The first half of a Bluesky archive import: reading a Cyd Bluesky archive off the device into verified, isolated staging, with no effect on any Bluesky local account. Intake ends *prepared*; the merge that follows is the rest of the import.
_Avoid_: intake, extraction, unpacking, archive validation

**Cyd Bluesky archive export**:
Writing one Bluesky local account's Bluesky saved data out as a Cyd Bluesky archive, from a point-in-time snapshot taken while account-mutating work is briefly paused. It is the mirror of Bluesky archive intake and reads only what Cyd already holds, so it needs no Bluesky connection.
_Avoid_: export, backup, archive export

**Bluesky account settings**:
Local preferences governing how Cyd saves and manages a Bluesky identity. A Cyd Bluesky archive can supply defaults for a new Bluesky local account, but does not silently override an existing Bluesky local account's preferences or schedules.
_Avoid_: account settings, account data, archive state
Expand Down
173 changes: 173 additions & 0 deletions app/account/components/DevArchiveExportCard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import { useCallback, useState } from "react";
import { ActivityIndicator, StyleSheet, Text, View } from "react-native";

import { PrimaryButton } from "@/components/account/shared-tab-components";
import { withBlueskyController } from "@/controllers";
import { getPortableBlueskySettings } from "@/database/accounts";
import type { BlueskyArchiveExportProgress } from "@/services/archive-export";
import type { AccountTabPalette } from "@/types/account-tabs";

/**
* The development-only way to write a Cyd Bluesky archive.
*
* Mobile builds its version 2 writer before the readers that consume it, so
* that reader work has real archives to be tested against (ADR 0004). What
* that ordering must not do is put an export button in front of people before
* #100 proves an archive Mobile writes can be read back — by Mobile and by
* Desktop. Hence `__DEV__`: this renders nothing at all in a release build.
*
* It is deliberately plain. The shippable export flow — resumable, with its
* plaintext warning and a share sheet — is #99's, and none of it belongs here
* yet.
*/

const PHASE_LABELS: Record<BlueskyArchiveExportProgress["phase"], string> = {
staging: "Pausing account work and copying the database…",
hashing: "Hashing preserved media…",
translating: "Translating into the interchange format…",
packaging: "Packaging the archive…",
done: "Finished",
};

type ExportState =
| { status: "idle" }
| { status: "running"; progress: BlueskyArchiveExportProgress }
| {
status: "done";
location: string;
byteLength: number;
completeness: "complete" | "incomplete";
assets: { available: number; missing: number; unavailable: number };
}
| { status: "failed"; message: string };

export type DevArchiveExportCardProps = {
accountId: number;
accountUUID: string;
palette: AccountTabPalette;
};

export function DevArchiveExportCard({
accountId,
accountUUID,
palette,
}: DevArchiveExportCardProps) {
const [state, setState] = useState<ExportState>({ status: "idle" });

const exportArchive = useCallback(async () => {
setState({
status: "running",
progress: { phase: "staging", packagedPayloads: 0, totalPayloads: 0 },
});
try {
const portableSettings = await getPortableBlueskySettings(accountId);
const result = await withBlueskyController(
accountId,
accountUUID,
(controller) =>
controller.exportBlueskyArchive({
exportId: `dev-${Date.now()}`,
portableSettings,
onProgress: (progress) => setState({ status: "running", progress }),
}),
);
setState({
status: "done",
location: result.location,
byteLength: result.byteLength,
completeness: result.metadata.completeness,
assets: result.assets,
});
} catch (error) {
setState({
status: "failed",
message: error instanceof Error ? error.message : String(error),
});
}
}, [accountId, accountUUID]);

if (!__DEV__) {
return null;
}

return (
<View
style={[
styles.card,
{ borderColor: palette.icon + "44", backgroundColor: palette.card },
]}
>
<Text style={[styles.title, { color: palette.text }]}>
Export a Cyd Bluesky archive (development only)
</Text>
<Text style={[styles.body, { color: palette.icon }]}>
Writes a version 2 archive into export staging. Not available in
release builds until Bluesky archive import is proven.
</Text>

<PrimaryButton
label={state.status === "running" ? "Exporting…" : "Export archive"}
palette={palette}
onPress={exportArchive}
disabled={state.status === "running"}
/>

{state.status === "running" ? (
<View style={styles.status}>
<ActivityIndicator color={palette.tint} />
<Text style={[styles.body, { color: palette.icon }]}>
{PHASE_LABELS[state.progress.phase]}
{state.progress.totalPayloads > 0
? ` (${state.progress.packagedPayloads}/${state.progress.totalPayloads} media)`
: ""}
</Text>
</View>
) : null}

{state.status === "done" ? (
<View style={styles.status}>
<Text style={[styles.body, { color: palette.text }]}>
{`${state.completeness} · ${(state.byteLength / 1024).toFixed(1)} KiB · ` +
`${state.assets.available} assets packaged, ` +
`${state.assets.missing + state.assets.unavailable} unavailable`}
</Text>
<Text selectable style={[styles.path, { color: palette.icon }]}>
{state.location}
</Text>
</View>
) : null}

{state.status === "failed" ? (
<Text style={[styles.body, { color: palette.tint }]}>
{state.message}
</Text>
) : null}
</View>
);
}

const styles = StyleSheet.create({
card: {
borderWidth: 1,
borderRadius: 12,
borderStyle: "dashed",
padding: 16,
gap: 8,
marginTop: 24,
},
title: {
fontSize: 15,
fontWeight: "600",
},
body: {
fontSize: 13,
lineHeight: 18,
},
path: {
fontSize: 11,
fontFamily: "monospace",
},
status: {
gap: 6,
},
});
105 changes: 105 additions & 0 deletions app/account/components/__tests__/dev-archive-export-card.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { fireEvent, render, screen, waitFor } from "@testing-library/react-native";
import React from "react";

import { Colors } from "@/constants/theme";

import { DevArchiveExportCard } from "../DevArchiveExportCard";

/**
* Export must not reach people before #100 proves an archive Mobile writes can
* be read back (ADR 0004). `__DEV__` is the whole of that gate, so it is worth
* a test that fails loudly if the affordance ever renders in a release build.
*/

const mockExportBlueskyArchive = jest.fn();

jest.mock("@/controllers", () => ({
withBlueskyController: jest.fn(
(
_accountId: number,
_accountUUID: string,
fn: (controller: { exportBlueskyArchive: jest.Mock }) => Promise<unknown>,
) => fn({ exportBlueskyArchive: mockExportBlueskyArchive }),
),
}));

jest.mock("@/database/accounts", () => ({
getPortableBlueskySettings: jest.fn().mockResolvedValue({ save_posts: true }),
}));

function renderCard() {
return render(
<DevArchiveExportCard
accountId={1}
accountUUID="018d5f7a-9b3c-7d10-8a2e-1f4c6b8d0e12"
palette={Colors.light}
/>,
);
}

function setDevBuild(value: boolean): void {
(global as unknown as { __DEV__: boolean }).__DEV__ = value;
}

describe("DevArchiveExportCard", () => {
const wasDev = (global as unknown as { __DEV__: boolean }).__DEV__;

afterEach(() => {
setDevBuild(wasDev);
jest.clearAllMocks();
});

it("renders nothing in a release build", () => {
setDevBuild(false);

renderCard();

expect(screen.queryByText(/Export archive/)).toBeNull();
});

it("offers export in a development build", () => {
setDevBuild(true);

renderCard();

expect(screen.getByText("Export archive")).toBeTruthy();
expect(
screen.getByText(/Not available in release builds/),
).toBeTruthy();
});

it("reports what the export produced, including what was unavailable", async () => {
setDevBuild(true);
mockExportBlueskyArchive.mockResolvedValue({
location: "file:///staging/dev-1/cyd-bluesky-alice.example-2026-09-10.cyd",
byteLength: 2048,
metadata: { completeness: "incomplete" },
assets: { total: 3, available: 2, missing: 1, unavailable: 0 },
});

renderCard();
fireEvent.press(screen.getByText("Export archive"));

await waitFor(() => {
expect(screen.getByText(/incomplete/)).toBeTruthy();
});
expect(screen.getByText(/1 unavailable/)).toBeTruthy();
expect(
screen.getByText(/cyd-bluesky-alice.example-2026-09-10.cyd/),
).toBeTruthy();
});

it("shows why an export failed rather than failing silently", async () => {
setDevBuild(true);
mockExportBlueskyArchive.mockRejectedValue(
new Error("Cyd could not copy this account's data for export"),
);

renderCard();
fireEvent.press(screen.getByText("Export archive"));

await waitFor(() => {
expect(screen.getByText(/could not copy this account's data/)).toBeTruthy();
});
});
});
7 changes: 7 additions & 0 deletions app/account/tabs/dashboard-tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
type ImageSourcePropType,
} from "react-native";

import { DevArchiveExportCard } from "@/app/account/components/DevArchiveExportCard";
import iconBrowse from "@/assets/images/icon-browse.png";
import iconDelete from "@/assets/images/icon-delete.png";
import iconSave from "@/assets/images/icon-save.png";
Expand Down Expand Up @@ -61,6 +62,7 @@ const TABLET_CARD_MIN_HEIGHT = 240;

export function DashboardTab({
accountId,
accountUUID,
handle,
palette,
onSelectTab,
Expand Down Expand Up @@ -182,6 +184,11 @@ export function DashboardTab({
);
})}
</View>
<DevArchiveExportCard
accountId={accountId}
accountUUID={accountUUID}
palette={palette}
/>
</View>
</View>
);
Expand Down
Loading