Skip to content

Commit 203be5b

Browse files
committed
refactor: trim lock policy cleanup
1 parent d01a987 commit 203be5b

2 files changed

Lines changed: 23 additions & 51 deletions

File tree

src/daemon/__tests__/request-lock-policy.test.ts

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -146,18 +146,7 @@ test.each([
146146
IOS_SESSION,
147147
);
148148

149-
assert.deepEqual(
150-
{
151-
platform: req.flags?.platform,
152-
device: req.flags?.device,
153-
serial: req.flags?.serial,
154-
},
155-
{
156-
platform: expected.platform,
157-
device: expected.device,
158-
serial: expected.serial,
159-
},
160-
);
149+
assert.deepEqual(selectedFlags(req), expected);
161150
},
162151
);
163152

@@ -187,18 +176,7 @@ test.each([
187176
},
188177
});
189178

190-
assert.deepEqual(
191-
{
192-
platform: req.flags?.platform,
193-
device: req.flags?.device,
194-
serial: req.flags?.serial,
195-
},
196-
{
197-
platform: expected.platform,
198-
device: expected.device,
199-
serial: expected.serial,
200-
},
201-
);
179+
assert.deepEqual(selectedFlags(req), expected);
202180
},
203181
);
204182

@@ -340,3 +318,15 @@ test('strips only conflicting selectors for existing sessions', () => {
340318
assert.equal(req.flags?.device, 'iPhone 16');
341319
assert.equal(req.flags?.serial, undefined);
342320
});
321+
322+
function selectedFlags(req: ReturnType<typeof applyRequestLockPolicy>): {
323+
platform: string | undefined;
324+
device: string | undefined;
325+
serial: string | undefined;
326+
} {
327+
return {
328+
platform: req.flags?.platform,
329+
device: req.flags?.device,
330+
serial: req.flags?.serial,
331+
};
332+
}

src/daemon/request-lock-policy.ts

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,17 @@ export function applyRequestLockPolicy(
3636
}
3737

3838
const nextFlags: CommandFlags = { ...(req.flags ?? {}) };
39-
const allowsSelectorOverride = SELECTOR_OVERRIDE_LOCK_POLICY_COMMANDS.has(req.command);
40-
const conflicts = listLockPolicyConflicts(
41-
req,
42-
existingSession,
43-
nextFlags,
44-
allowsSelectorOverride,
45-
);
39+
const canOverrideSelector = SELECTOR_OVERRIDE_LOCK_POLICY_COMMANDS.has(req.command);
40+
const conflicts = canOverrideSelector
41+
? []
42+
: existingSession
43+
? listSessionSelectorConflicts(existingSession, nextFlags)
44+
: listFreshSessionConflicts(nextFlags, req.meta?.lockPlatform, req.command);
4645
const lockPlatform = req.meta?.lockPlatform;
4746

4847
if (conflicts.length === 0) {
4948
if (
50-
shouldApplyLockPlatformDefault(
51-
allowsSelectorOverride,
52-
existingSession,
53-
nextFlags,
54-
lockPlatform,
55-
)
49+
shouldApplyLockPlatformDefault(canOverrideSelector, existingSession, nextFlags, lockPlatform)
5650
) {
5751
nextFlags.platform = lockPlatform;
5852
}
@@ -77,28 +71,16 @@ export function applyRequestLockPolicy(
7771
);
7872
}
7973

80-
function listLockPolicyConflicts(
81-
req: DaemonRequest,
82-
existingSession: SessionState | undefined,
83-
flags: CommandFlags,
84-
allowsSelectorOverride: boolean,
85-
): SessionSelectorConflict[] {
86-
if (allowsSelectorOverride) return [];
87-
return existingSession
88-
? listSessionSelectorConflicts(existingSession, flags)
89-
: listFreshSessionConflicts(flags, req.meta?.lockPlatform, req.command);
90-
}
91-
9274
function shouldApplyLockPlatformDefault(
93-
allowsSelectorOverride: boolean,
75+
canOverrideSelector: boolean,
9476
existingSession: SessionState | undefined,
9577
flags: CommandFlags,
9678
lockPlatform: LockPlatform,
9779
): boolean {
9880
if (!lockPlatform || existingSession || flags.platform !== undefined) {
9981
return false;
10082
}
101-
if (!allowsSelectorOverride) {
83+
if (!canOverrideSelector) {
10284
return true;
10385
}
10486
return flags.serial === undefined && flags.androidDeviceAllowlist === undefined;

0 commit comments

Comments
 (0)