Skip to content

Commit

Permalink
Final cleanup before initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
technoplato committed Apr 22, 2024
1 parent 0ebe3c5 commit a06163b
Show file tree
Hide file tree
Showing 6 changed files with 3,698 additions and 3,756 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,105 +20,99 @@ type JESActorSystem = ActorSystem<{

describe('Counting Machine That Needs Permission At 3', () => {
describe('Actor system tests', () => {
it(
'should increment count to 3, ask for permission, and continue counting to 5 when permission is granted',
async () => {
const applicationActor = createActor(applicationMachine, {
systemId: ActorSystemIds.application,
inspect: createSkyInspector({
// @ts-expect-error
WebSocket: WebSocket,
inspectorType: 'node',
autoStart: true,
}).inspect,
});
applicationActor.start();
const actorSystem: JESActorSystem = applicationActor.system;

const permissionMonitorActor = actorSystem.get(
ActorSystemIds.permissionMonitoring
)!;

const countingPermissionReporter = applicationActor.system.get(
'permissionReportingCounting'
it('should increment count to 3, ask for permission, and continue counting to 5 when permission is granted', async () => {
const applicationActor = createActor(applicationMachine, {
systemId: ActorSystemIds.application,
inspect: createSkyInspector({
// @ts-expect-error
WebSocket: WebSocket,
inspectorType: 'node',
autoStart: true,
}).inspect,
});
applicationActor.start();
const actorSystem: JESActorSystem = applicationActor.system;

const permissionMonitorActor = actorSystem.get(
ActorSystemIds.permissionMonitoring
)!;

const countingPermissionReporter = applicationActor.system.get(
'permissionReportingCounting'
);

// @ts-expect-error this means the actor system type is working as expected
permissionMonitorActor.getSnapshot().value === 'foo';
permissionMonitorActor?.getSnapshot().value.applicationLifecycle ===
'applicationInBackground';

// @ts-expect-error this means the actor system type is working as expected
permissionMonitorActor.getSnapshot().context === 'foo';

expect(permissionMonitorActor).toBeDefined();
expect(countingPermissionReporter).toBeDefined();

/* Required due to a bug in the initialization of actor systems*/ await new Promise(
(resolve) => setTimeout(resolve, 0)
);

const state: PermissionMonitoringSnapshot =
permissionMonitorActor?.getSnapshot();

// We should be able to find the permission coordinator for the Counting
// feature in the Permission Monitor's subscription map
const countingMachinePermissionCoordinator =
state.context.permissionSubscribers[Permissions.bluetooth]?.some(
(subscriber) => subscriber.id === 'permissionReportingCounting'
);
expect(countingMachinePermissionCoordinator).toBeDefined();

// @ts-expect-error this means the actor system type is working as expected
permissionMonitorActor.getSnapshot().value === 'foo';
permissionMonitorActor?.getSnapshot().value.applicationLifecycle ===
'applicationInBackground';
const countingActor = applicationActor.system.get(
ActorSystemIds.counting
);

// @ts-expect-error this means the actor system type is working as expected
permissionMonitorActor.getSnapshot().context === 'foo';
expect(countingActor?.getSnapshot().value).toStrictEqual('enabled');

expect(permissionMonitorActor).toBeDefined();
expect(countingPermissionReporter).toBeDefined();

/* Required due to a bug in the initialization of actor systems*/ await new Promise(
(resolve) => setTimeout(resolve, 0)
);

const state: PermissionMonitoringSnapshot =
permissionMonitorActor?.getSnapshot();

// We should be able to find the permission coordinator for the Counting
// feature in the Permission Monitor's subscription map
const countingMachinePermissionCoordinator =
state.context.permissionSubscribers[Permissions.bluetooth]?.some(
(subscriber) => subscriber.id === 'permissionReportingCounting'
);
expect(countingMachinePermissionCoordinator).toBeDefined();

const countingActor = applicationActor.system.get(
ActorSystemIds.counting
);

expect(countingActor?.getSnapshot().value).toStrictEqual('enabled');
countingActor.send({ type: 'count.inc' });
countingActor.send({ type: 'count.inc' });
countingActor.send({ type: 'count.inc' });
expect(countingActor.getSnapshot().context.count).toBe(3);
expect(countingActor.getSnapshot().value).toStrictEqual(
'handlingPermissions'
);

countingActor.send({ type: 'count.inc' });
countingActor.send({ type: 'count.inc' });
countingActor.send({ type: 'count.inc' });
expect(countingActor.getSnapshot().context.count).toBe(3);
expect(countingActor.getSnapshot().value).toStrictEqual(
'handlingPermissions'
);
countingActor.send({ type: 'count.inc' });
expect(countingActor.getSnapshot().context.count).toBe(3);
expect(countingActor.getSnapshot().value).toStrictEqual(
'handlingPermissions'
);

countingActor.send({ type: 'count.inc' });
expect(countingActor.getSnapshot().context.count).toBe(3);
expect(countingActor.getSnapshot().value).toStrictEqual(
'handlingPermissions'
);
// Configure the permission actor to grant permission
const permissionCheckerActor = applicationActor.system.get(
ActorSystemIds.permissionCheckerAndRequester
);

// Configure the permission actor to grant permission
const permissionCheckerActor = applicationActor.system.get(
ActorSystemIds.permissionCheckerAndRequester
);
countingActor.send({ type: 'user.didTapBluetoothRequestPermission' });

countingActor.send({ type: 'user.didTapBluetoothRequestPermission' });
await waitFor(permissionCheckerActor, (state) => state.value === 'idle');

await waitFor(
permissionCheckerActor,
(state) => state.value === 'idle'
);
expect(countingActor.getSnapshot().value).toStrictEqual('enabled');

expect(countingActor.getSnapshot().value).toStrictEqual('enabled');
expect(countingActor.getSnapshot().context.permissionStatus).toBe(
PermissionStatuses.granted
);

expect(countingActor.getSnapshot().context.permissionStatus).toBe(
PermissionStatuses.granted
);
// Send 'count.inc' events to increment the count to 5
countingActor.send({ type: 'count.inc' });
countingActor.send({ type: 'count.inc' });

// Send 'count.inc' events to increment the count to 5
countingActor.send({ type: 'count.inc' });
countingActor.send({ type: 'count.inc' });

expect(countingActor.getSnapshot().context.count).toBe(5);
expect(countingActor.getSnapshot().value).toStrictEqual('finished');
countingActor.send({ type: 'count.inc' });
expect(countingActor.getSnapshot().context.count).toBe(5);
///* Required for debugging with stately inspector */ await new Promise((resolve) => setTimeout(resolve, vLongTime));
},
vLongTime
);
expect(countingActor.getSnapshot().context.count).toBe(5);
expect(countingActor.getSnapshot().value).toStrictEqual('finished');
countingActor.send({ type: 'count.inc' });
expect(countingActor.getSnapshot().context.count).toBe(5);
// /* Required if you want to debug a test with the stately inspector */ await new Promise((resolve) => setTimeout(resolve, vLongTime));
});
// vLongTime
});

describe('Actor unit tests', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
assertEvent,
assign,
enqueueActions,
log,
raise,
sendTo,
setup,
Expand Down Expand Up @@ -77,24 +76,22 @@ export const permissionMonitoringMachine = setup({
permissionSubscribers: updatedPermissionSubscribers,
};
}),
broadcastPermissionsToListeners: enqueueActions(
({ context, event, enqueue }) => {
// TODO this should only send permission updates for the recently modified permissions
// and is currently sending updates to all permissions to everyone
Object.keys(context.permissionSubscribers).forEach((permission) => {
context.permissionSubscribers[permission].forEach(
(actorRef: AnyActorRef) => {
enqueue.sendTo(actorRef, {
type: 'permissionStatusChanged',
permission,
status: context.permissionsStatuses[permission],
});
}
);
});
}
),
assignPermissionRequestResultToContext: assign({
broadcastPermissionsToListeners: enqueueActions(({ context, enqueue }) => {
// TODO this should only send permission updates for the recently modified permissions
// and is currently sending updates to all permissions to everyone
Object.keys(context.permissionSubscribers).forEach((permission) => {
context.permissionSubscribers[permission].forEach(
(actorRef: AnyActorRef) => {
enqueue.sendTo(actorRef, {
type: 'permissionStatusChanged',
permission,
status: context.permissionsStatuses[permission],
});
}
);
});
}),
savePermissionRequestResult: assign({
permissionsStatuses: ({ event, context }) => {
assertEvent(event, 'permissionRequestCompleted');
return {
Expand Down Expand Up @@ -123,7 +120,6 @@ export const permissionMonitoringMachine = setup({
/** @xstate-layout N4IgpgJg5mDOIC5QCMCWUDSBDAFgVwDssA6LABzIBtUBjLAF1QHsCAZVAMzBoE8bKwAYnJVaDZgQBiTAE5goMpoQiQA2gAYAuolBkmsVIxY6QAD0QBaABwBmdcQCcANicAWKwFY7D165tWAGhAeSwBGf2JQgHYbf3U7J38PACYogF80oLRMXEISEWo6IzZObj4BYQpC8RYAISwaAGsFJQIVCA1tJBA9A2KTcwQLVxjHZISoqz9bcI8gkKHwq0iYmydk9Sso9VdQq2SMrPRsfCJiMjAZAFtUWAMWWGFKSgAFS5u7iVgAYRxuRrUWhMvUMEgGiBsrmSxB2ySsVjcoS8O3Uc2CYSs9lCiSi218DiiHnU6gOmRA2ROeXO71u9wIj3oMnQMBkb2utIkv3+nWB+lBxm6gwsyVCjiRoQcW3iNiivj88zCDhsxBGoXUUQlMQRDg8VkO5OOuTOF3ZnwegkZzMubI+dIASmAAI54OD0HndEH9QWIZLQ4lOKwEgOBqa+tELSHQmW48bhX1rJz6ilGkgm21fQRpjksB3O13fJhXKhgeiArq6Ple0CDGzJVwq2sI6KeDzrcMQjz1zzYkVOGJKvsZMkEJgqeDdZOnLC8vpg71DBzJBwq1K+qFOTa2QLooY2DzLtwIpdwgPJAOJsmTqkFMTFdhcXj8MAz-kEcFDeGiqFRNdnzf+BUhlXYhaw8H8JR2WJXFRJNDSnUgqlvCR7zKJ8ENEIoJAASVgLCpFkeRFGUF8qzMSxbGVb9fw3eEAJ3CwPC-KIlQcJEbFCeN4lgnJ4JvTCWBQx8BHQ6pijw+omhaYiPUrOdq0sVwnGXH8NWJBwHGJCVXEA4VlnUNwnC8RcYy3C8jh4qkszNekSLksiEH2GF-E8fZ3GxSNAJlYhWysCDwkhXZ9yHNIgA */
id: ActorSystemIds.permissionMonitoring,
type: 'parallel',
entry: log('monitoring started'),

context: {
permissionsStatuses: InitialPermissionStatusMap,
Expand Down Expand Up @@ -178,7 +174,7 @@ export const permissionMonitoringMachine = setup({
},
permissionRequestCompleted: {
actions: [
'assignPermissionRequestResultToContext',
'savePermissionRequestResult',
'broadcastPermissionsToListeners',
],
},
Expand Down
10 changes: 0 additions & 10 deletions libs/permissions/permissionLogic/src/lib/permission/reporting/asdf

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
AnyActorRef,
AnyEventObject,
enqueueActions,
log,
sendTo,
setup,
} from 'xstate';
Expand All @@ -21,6 +20,10 @@ export const permissionReportingMachine = setup({
permissions: Array<Permission>;
parent: AnyActorRef;
},
events: {} as {
type: 'requestPermission';
permission: Permission;
},
},
actions: {
sendSubscriptionRequestForStatusUpdates: sendTo(
Expand All @@ -40,12 +43,12 @@ export const permissionReportingMachine = setup({
({ system }) => {
return system.get(ActorSystemIds.permissionCheckerAndRequester);
},
({ event }) => ({
// TODO: determine how to make this typesafe
// I'm thinking an api like sendToPermissionChecker(event: PermissionCheckerEvent)
type: 'triggerPermissionRequest',
permission: event.permission,
})
({ event }) => {
return {
type: 'triggerPermissionRequest',
permission: event.permission,
};
}
),
checkedSendParent: enqueueActions(
({ context, enqueue }, event: AnyEventObject) => {
Expand Down Expand Up @@ -96,8 +99,10 @@ machine.
`,
},
permissionStatusChanged: {
description: `Whenever the Permission Monitoring machine reports that a permission
status has changed, we receive this event and can process and share with our siblings.`,
description: `
Whenever the Permission Monitoring machine reports that a permission
status has changed, we receive this event and can process and share
with the machine in which we are invoked.`,
actions: [
{
/**
Expand Down
Loading

0 comments on commit a06163b

Please sign in to comment.