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
5 changes: 4 additions & 1 deletion products/desktop/apps/mobilehog/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"prebuild": "expo prebuild",
"prebuild:clean": "expo prebuild --clean",
"typecheck": "tsc --noEmit",
"test": "vitest run",
"lint": "biome check .",
"lint:fix": "biome check --write .",
"clean": "node ../../scripts/rimraf.mjs .turbo .expo"
Expand Down Expand Up @@ -46,6 +47,7 @@
"expo-crypto": "~57.0.1",
"expo-dev-client": "~57.0.19",
"expo-device": "~57.0.2",
"expo-file-system": "~57.0.7",
"expo-font": "~57.0.4",
"expo-glass-effect": "~57.0.3",
"expo-haptics": "~57.0.3",
Expand Down Expand Up @@ -78,7 +80,8 @@
},
"devDependencies": {
"@types/react": "^19.2.0",
"typescript": "~5.9.2"
"typescript": "~5.9.2",
"vitest": "^4.1.8"
},
"private": true
}
74 changes: 74 additions & 0 deletions products/desktop/apps/mobilehog/src/lib/attachments.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { beforeEach, describe, expect, it, vi } from "vitest";

const mocks = vi.hoisted(() => {
const photoBytes = new Uint8Array([137, 80, 78, 71]);
class MockFile extends Blob {
constructor(_uri: string) {
super([photoBytes], { type: "image/png" });
}
}

return {
client: {
finalizeTaskStagedArtifactUploads: vi.fn(),
prepareTaskStagedArtifactUploads: vi.fn(),
},
expoFetch: vi.fn(),
MockFile,
photoBytes,
};
});

vi.mock("expo/fetch", () => ({ fetch: mocks.expoFetch }));
vi.mock("expo-file-system", () => ({ File: mocks.MockFile }));
vi.mock("expo-image-picker", () => ({
UIImagePickerPreferredAssetRepresentationMode: { Compatible: "compatible" },
launchImageLibraryAsync: vi.fn(),
}));
vi.mock("@/lib/client", () => ({ getClient: () => mocks.client }));

import { uploadStagedPhotos } from "./attachments";

beforeEach(() => {
vi.clearAllMocks();
mocks.client.prepareTaskStagedArtifactUploads.mockResolvedValue([
{
id: "prepared-id",
presigned_post: {
fields: { key: "artifact-key" },
url: "https://uploads.example.com",
},
},
]);
mocks.client.finalizeTaskStagedArtifactUploads.mockResolvedValue([
{ id: "artifact-id" },
]);
});

describe("uploadStagedPhotos", () => {
it("sends the selected photo bytes through Expo fetch", async () => {
let uploadedBytes: Uint8Array | undefined;
mocks.expoFetch.mockImplementation(async (_url, init) => {
const file = (init?.body as FormData).get("file") as Blob;
uploadedBytes = new Uint8Array(await file.arrayBuffer());
return { ok: true };
});

const result = await uploadStagedPhotos("task-id", [
{
id: "photo-id",
uri: "file:///photo.png",
name: "photo.png",
mimeType: "image/png",
size: mocks.photoBytes.byteLength,
},
]);

expect(uploadedBytes).toEqual(mocks.photoBytes);
expect(mocks.expoFetch).toHaveBeenCalledWith(
"https://uploads.example.com",
expect.objectContaining({ method: "POST" }),
);
expect(result).toEqual(["artifact-id"]);
});
});
12 changes: 6 additions & 6 deletions products/desktop/apps/mobilehog/src/lib/attachments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type {
PreparedTaskArtifactUpload,
TaskArtifactUploadRequest,
} from "@posthog/api-client/posthog-client";
import { fetch } from "expo/fetch";
import { File } from "expo-file-system";
import * as ImagePicker from "expo-image-picker";
import { getClient } from "@/lib/client";

Expand Down Expand Up @@ -51,7 +53,6 @@ function uploadRequests(photos: Photo[]): TaskArtifactUploadRequest[] {
}));
}

// Streams each file straight from disk to object storage; nothing is base64 encoded.
async function postFiles(
photos: Photo[],
prepared: PreparedTaskArtifactUpload[],
Expand All @@ -65,11 +66,10 @@ async function postFiles(
)) {
form.append(key, value);
}
form.append("file", {
uri: photo.uri,
name: photo.name,
type: photo.mimeType,
} as unknown as Blob);
// Expo fetch rejects React Native's legacy { uri, name, type } part.
// File supplies the bytes() contract that its multipart encoder requires;
// React Native's FormData type does not yet include Expo's File support.
form.append("file", new File(photo.uri) as unknown as Blob);
Comment thread
dmarticus marked this conversation as resolved.
const response = await fetch(artifact.presigned_post.url, {
Comment thread
dmarticus marked this conversation as resolved.
method: "POST",
body: form,
Expand Down
18 changes: 15 additions & 3 deletions products/desktop/apps/mobilehog/src/lib/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,22 @@ export function useTaskPages(search: string, archived: boolean) {
const scope = archived ? "archived" : "active";
return useInfiniteQuery({
queryKey: search
? [...keys.tasks, "pages", scope, "search", search]
: [...keys.tasks, "pages", scope],
? [
...keys.tasks,
"created-by",
session?.userId,
"pages",
scope,
"search",
search,
]
: [...keys.tasks, "created-by", session?.userId, "pages", scope],
initialPageParam: 0,
queryFn: async ({ pageParam }) => {
const page = await getClient().getTasksPage({
basic: true,
archived,
createdBy: session?.userId,
search: search || undefined,
ordering: "-last_activity_at",
limit: PAGE_SIZE,
Expand All @@ -69,11 +78,14 @@ export function useTaskPages(search: string, archived: boolean) {
export function useTasks(search = "") {
const session = useAuth((s) => s.session);
return useQuery({
queryKey: search ? [...keys.tasks, "search", search] : keys.tasks,
queryKey: search
? [...keys.tasks, "created-by", session?.userId, "search", search]
: [...keys.tasks, "created-by", session?.userId],
queryFn: async () =>
visibleTasks(
await getClient().getTasks({
basic: true,
createdBy: session?.userId,
search: search || undefined,
}),
),
Expand Down
15 changes: 15 additions & 0 deletions products/desktop/apps/mobilehog/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import path from "node:path";
import { defineConfig } from "vitest/config";
import { trunkTestOptions } from "../../vitest.config.base";

export default defineConfig({
test: {
...trunkTestOptions,
environment: "node",
},
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
},
},
});
6 changes: 6 additions & 0 deletions products/desktop/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading