Skip to content

Commit 1b3e330

Browse files
only21milclaudecodexmaria-rcks
authored
fix(device): report failed Android permission changes (pingdotgg#22358)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: GPT-6.1 Sol <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent 443edd5 commit 1b3e330

2 files changed

Lines changed: 62 additions & 3 deletions

File tree

‎apps/server/src/device/DeviceActions.test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,60 @@ describe("runDeviceAction", () => {
9898
expect(calls.at(-1)?.args.at(-1)).toBe(expected);
9999
}),
100100
);
101+
102+
it.effect.each([
103+
["camera", "grant", "android.permission.CAMERA"],
104+
["photos", "revoke", "android.permission.READ_MEDIA_IMAGES"],
105+
] as const)(
106+
"reports an Android %s %s that no pm command applied",
107+
([permission, decision, firstPermission]) =>
108+
Effect.gen(function* () {
109+
const { ready } = makeReady((call) => ({
110+
code: 1,
111+
stderr: `Unknown package: ${call.args.at(-1)}`,
112+
}));
113+
const error = yield* Effect.flip(
114+
runDeviceAction(ready, "android", {
115+
type: "setPermission",
116+
deviceId: "emulator-5554",
117+
appId: "com.example.missing",
118+
permission,
119+
decision,
120+
}),
121+
);
122+
expect(error._tag).toBe("DeviceOperationError");
123+
expect(error.operation).toBe("permission");
124+
expect(error._tag === "DeviceOperationError" && error.cause).toMatchObject({
125+
stderr: `Unknown package: ${firstPermission}`,
126+
});
127+
}),
128+
);
129+
130+
it.effect.each([
131+
[
132+
"photos",
133+
["android.permission.READ_MEDIA_IMAGES", "android.permission.READ_EXTERNAL_STORAGE"],
134+
1,
135+
],
136+
["contacts", ["android.permission.READ_CONTACTS", "android.permission.WRITE_CONTACTS"], 0],
137+
] as const)(
138+
"keeps a partially applied %s permission change",
139+
([permission, permissions, success]) =>
140+
Effect.gen(function* () {
141+
const { ready, calls } = makeReady((call) => ({
142+
code: call.args.at(-1) === permissions[success] ? 0 : 1,
143+
}));
144+
const result = yield* runDeviceAction(ready, "android", {
145+
type: "setPermission",
146+
deviceId: "emulator-5554",
147+
appId: "com.example.app",
148+
permission,
149+
decision: "grant",
150+
});
151+
expect(result).toBeUndefined();
152+
expect(calls.map((call) => call.args.at(-1))).toEqual(permissions);
153+
}),
154+
);
101155
});
102156

103157
describe("readDeviceDetail", () => {

‎apps/server/src/device/DeviceActions.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
type DeviceTextSize,
1212
} from "@t3tools/contracts";
1313
import * as Effect from "effect/Effect";
14+
import * as Result from "effect/Result";
1415
import * as Schema from "effect/Schema";
1516

1617
import type { DeviceHostReady } from "./DeviceHost.ts";
@@ -432,9 +433,13 @@ const runAndroid = Effect.fn("DeviceActions.runAndroid")(function* (
432433
});
433434
}
434435
const verb = input.decision === "grant" ? "grant" : "revoke";
435-
for (const permission of permissions) {
436-
yield* shell(["pm", verb, input.appId, permission], "permission").pipe(Effect.ignore);
437-
}
436+
// `pm` rejects a permission the app or system image does not declare, and Photos lists both
437+
// the Android 13 and the older storage permission, so fail only when none of them applied.
438+
const results = yield* Effect.forEach(permissions, (permission) =>
439+
shell(["pm", verb, input.appId, permission], "permission").pipe(Effect.result),
440+
);
441+
const failure = results.find(Result.isFailure);
442+
if (failure && !results.some(Result.isSuccess)) return yield* failure.failure;
438443
return;
439444
}
440445
case "openUrl":

0 commit comments

Comments
 (0)