mirror of
https://github.com/unraid/api.git
synced 2026-01-04 07:29:48 -06:00
This is batch 2 of the web store tests. I started implementing the recommended approach in the Pinia docs for the store tests and was able to eliminate most of the mocking files. The server.test.ts file still uses `pinia/testing` for now since I was having trouble with some of the dependencies in the store due to it's complexity. I also updated the `web-testing-rules`for Cursor in an effort to streamline the AI's approach when helping with tests. There's some things it still struggles with and seems like it doesn't always take the rules into consideration until after it hits a snag. It likes to try and create a mock for the store it's actually testing even though there's a rule in place and has to be reminded. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Summary by CodeRabbit - **Tests** - Added comprehensive test suites for account management and activation flows to ensure smoother user interactions. - Introduced new test coverage for callback actions, validating state management and action handling. - Added a new test file for the account store, covering various actions and their interactions. - Introduced a new test file for the activation code store, verifying state management and modal visibility. - Established a new test file for dropdown functionality, ensuring accurate state management and action methods. - Added a new test suite for the Errors store, covering error handling and modal interactions. - Enhanced test guidelines for Vue components and Pinia stores, providing clearer documentation and best practices. - Introduced a new test file for the InstallKey store, validating key installation processes and error handling. - Added a new test file for the dropdown store, ensuring accurate visibility state management and action methods. - **Refactor** - Streamlined the structure of account action payloads for consistent behavior. - Improved code formatting and reactivity setups to enhance overall operational stability. - Enhanced reactivity capabilities in various stores by introducing new Vue composition API functions. - Improved code readability and organization in the installKey store and other related files. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: mdatelle <mike@datelle.net>
382 lines
9.8 KiB
TypeScript
382 lines
9.8 KiB
TypeScript
import { computed, ref, watchEffect } from 'vue';
|
|
import { createPinia, defineStore, setActivePinia } from 'pinia';
|
|
import { useMutation } from '@vue/apollo-composable';
|
|
import { logErrorMessages } from '@vue/apollo-util';
|
|
|
|
import { ACCOUNT_CALLBACK } from '~/helpers/urls';
|
|
|
|
import type { ExternalSignIn, ExternalSignOut } from '@unraid/shared-callbacks';
|
|
|
|
import { useCallbackActionsStore } from '~/store/callbackActions';
|
|
import { useErrorsStore } from '~/store/errors';
|
|
import { useReplaceRenewStore } from '~/store/replaceRenew';
|
|
import { useServerStore } from '~/store/server';
|
|
import { useUnraidApiStore } from '~/store/unraidApi';
|
|
import { CONNECT_SIGN_IN, CONNECT_SIGN_OUT } from './account.fragment';
|
|
|
|
/**
|
|
* @see https://stackoverflow.com/questions/73476371/using-pinia-with-vue-js-web-components
|
|
* @see https://github.com/vuejs/pinia/discussions/1085
|
|
*/
|
|
|
|
setActivePinia(createPinia());
|
|
|
|
export interface ConnectSignInMutationPayload {
|
|
apiKey: string;
|
|
email: string;
|
|
preferred_username: string;
|
|
}
|
|
|
|
export const useAccountStore = defineStore('account', () => {
|
|
const callbackStore = useCallbackActionsStore();
|
|
const errorsStore = useErrorsStore();
|
|
const replaceRenewStore = useReplaceRenewStore();
|
|
const serverStore = useServerStore();
|
|
const unraidApiStore = useUnraidApiStore();
|
|
|
|
const serverAccountPayload = computed(() => serverStore.serverAccountPayload);
|
|
const inIframe = computed(() => serverStore.inIframe);
|
|
const sendType = computed(() => callbackStore.sendType);
|
|
|
|
// State
|
|
const accountAction = ref<ExternalSignIn | ExternalSignOut>();
|
|
const accountActionHide = ref<boolean>(false);
|
|
const accountActionStatus = ref<'failed' | 'ready' | 'success' | 'updating' | 'waiting'>('ready');
|
|
|
|
/**
|
|
* Handling sign in / out via graph api
|
|
*/
|
|
const unraidApiClient = computed(() => unraidApiStore.unraidApiClient);
|
|
const connectSignInPayload = ref<ConnectSignInMutationPayload | undefined>();
|
|
const setConnectSignInPayload = (payload: ConnectSignInMutationPayload | undefined) => {
|
|
connectSignInPayload.value = payload;
|
|
if (payload) {
|
|
accountActionStatus.value = 'waiting';
|
|
}
|
|
};
|
|
const queueConnectSignOut = ref<boolean>(false);
|
|
const setQueueConnectSignOut = (data: boolean) => {
|
|
queueConnectSignOut.value = data;
|
|
if (data) {
|
|
accountActionStatus.value = 'waiting';
|
|
}
|
|
};
|
|
watchEffect(() => {
|
|
if (unraidApiClient.value && connectSignInPayload.value) {
|
|
// connectSignInMutation();
|
|
setTimeout(() => {
|
|
connectSignInMutation();
|
|
}, 250);
|
|
}
|
|
if (unraidApiClient.value && queueConnectSignOut.value) {
|
|
// connectSignOutMutation();
|
|
setTimeout(() => {
|
|
connectSignOutMutation();
|
|
}, 250);
|
|
}
|
|
});
|
|
|
|
// Getters
|
|
const accountActionType = computed(() => accountAction.value?.type);
|
|
|
|
// Actions
|
|
const downgradeOs = async (autoRedirectReplace?: boolean) => {
|
|
await callbackStore.send(
|
|
ACCOUNT_CALLBACK.toString(),
|
|
[
|
|
{
|
|
server: {
|
|
...serverAccountPayload.value,
|
|
},
|
|
type: 'downgradeOs',
|
|
},
|
|
],
|
|
inIframe.value ? 'newTab' : autoRedirectReplace ? 'replace' : undefined,
|
|
sendType.value
|
|
);
|
|
};
|
|
|
|
const manage = () => {
|
|
callbackStore.send(
|
|
ACCOUNT_CALLBACK.toString(),
|
|
[
|
|
{
|
|
server: {
|
|
...serverAccountPayload.value,
|
|
},
|
|
type: 'manage',
|
|
},
|
|
],
|
|
inIframe.value ? 'newTab' : undefined,
|
|
sendType.value
|
|
);
|
|
};
|
|
const myKeys = async () => {
|
|
/**
|
|
* Purge the validation response so we can start fresh after the user has linked their key
|
|
*/
|
|
await replaceRenewStore.purgeValidationResponse();
|
|
|
|
callbackStore.send(
|
|
ACCOUNT_CALLBACK.toString(),
|
|
[
|
|
{
|
|
server: {
|
|
...serverAccountPayload.value,
|
|
},
|
|
type: 'myKeys',
|
|
},
|
|
],
|
|
inIframe.value ? 'newTab' : undefined,
|
|
sendType.value
|
|
);
|
|
};
|
|
const linkKey = async () => {
|
|
/**
|
|
* Purge the validation response so we can start fresh after the user has linked their key
|
|
*/
|
|
await replaceRenewStore.purgeValidationResponse();
|
|
|
|
callbackStore.send(
|
|
ACCOUNT_CALLBACK.toString(),
|
|
[
|
|
{
|
|
server: {
|
|
...serverAccountPayload.value,
|
|
},
|
|
type: 'linkKey',
|
|
},
|
|
],
|
|
inIframe.value ? 'newTab' : undefined,
|
|
sendType.value
|
|
);
|
|
};
|
|
const recover = () => {
|
|
callbackStore.send(
|
|
ACCOUNT_CALLBACK.toString(),
|
|
[
|
|
{
|
|
server: {
|
|
...serverAccountPayload.value,
|
|
},
|
|
type: 'recover',
|
|
},
|
|
],
|
|
inIframe.value ? 'newTab' : undefined,
|
|
sendType.value
|
|
);
|
|
};
|
|
const replace = () => {
|
|
callbackStore.send(
|
|
ACCOUNT_CALLBACK.toString(),
|
|
[
|
|
{
|
|
server: {
|
|
...serverAccountPayload.value,
|
|
},
|
|
type: 'replace',
|
|
},
|
|
],
|
|
inIframe.value ? 'newTab' : undefined,
|
|
sendType.value
|
|
);
|
|
};
|
|
const signIn = () => {
|
|
callbackStore.send(
|
|
ACCOUNT_CALLBACK.toString(),
|
|
[
|
|
{
|
|
server: {
|
|
...serverAccountPayload.value,
|
|
},
|
|
type: 'signIn',
|
|
},
|
|
],
|
|
inIframe.value ? 'newTab' : undefined,
|
|
sendType.value
|
|
);
|
|
};
|
|
const signOut = () => {
|
|
callbackStore.send(
|
|
ACCOUNT_CALLBACK.toString(),
|
|
[
|
|
{
|
|
server: {
|
|
...serverAccountPayload.value,
|
|
},
|
|
type: 'signOut',
|
|
},
|
|
],
|
|
inIframe.value ? 'newTab' : undefined,
|
|
sendType.value
|
|
);
|
|
};
|
|
const trialExtend = () => {
|
|
callbackStore.send(
|
|
ACCOUNT_CALLBACK.toString(),
|
|
[
|
|
{
|
|
server: {
|
|
...serverAccountPayload.value,
|
|
},
|
|
type: 'trialExtend',
|
|
},
|
|
],
|
|
inIframe.value ? 'newTab' : undefined,
|
|
sendType.value
|
|
);
|
|
};
|
|
const trialStart = () => {
|
|
callbackStore.send(
|
|
ACCOUNT_CALLBACK.toString(),
|
|
[
|
|
{
|
|
server: {
|
|
...serverAccountPayload.value,
|
|
},
|
|
type: 'trialStart',
|
|
},
|
|
],
|
|
inIframe.value ? 'newTab' : undefined,
|
|
sendType.value
|
|
);
|
|
};
|
|
|
|
const updateOs = async (autoRedirectReplace?: boolean) => {
|
|
await callbackStore.send(
|
|
ACCOUNT_CALLBACK.toString(),
|
|
[
|
|
{
|
|
server: {
|
|
...serverAccountPayload.value,
|
|
},
|
|
type: 'updateOs',
|
|
},
|
|
],
|
|
inIframe.value ? 'newTab' : autoRedirectReplace ? 'replace' : undefined,
|
|
sendType.value
|
|
);
|
|
};
|
|
|
|
const connectSignInMutation = async () => {
|
|
if (
|
|
!connectSignInPayload.value ||
|
|
(connectSignInPayload.value &&
|
|
(!connectSignInPayload.value.apiKey ||
|
|
!connectSignInPayload.value.email ||
|
|
!connectSignInPayload.value.preferred_username))
|
|
) {
|
|
accountActionStatus.value = 'failed';
|
|
return console.error('[connectSignInMutation] incorrect payload', connectSignInPayload.value);
|
|
}
|
|
|
|
accountActionStatus.value = 'updating';
|
|
const {
|
|
mutate: signInMutation,
|
|
onDone,
|
|
onError,
|
|
} = await useMutation(CONNECT_SIGN_IN, {
|
|
variables: {
|
|
input: {
|
|
apiKey: connectSignInPayload.value.apiKey,
|
|
userInfo: {
|
|
email: connectSignInPayload.value.email,
|
|
preferred_username: connectSignInPayload.value.preferred_username,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
signInMutation();
|
|
|
|
onDone((res) => {
|
|
if (res.data?.connectSignIn) {
|
|
accountActionStatus.value = 'success';
|
|
setConnectSignInPayload(undefined); // reset
|
|
return;
|
|
}
|
|
accountActionStatus.value = 'failed';
|
|
errorsStore.setError({
|
|
heading: 'unraid-api failed to update Connect account configuration',
|
|
message: 'Sign In mutation unsuccessful',
|
|
level: 'error',
|
|
ref: 'connectSignInMutation',
|
|
type: 'account',
|
|
});
|
|
});
|
|
|
|
onError((error) => {
|
|
logErrorMessages(error);
|
|
accountActionStatus.value = 'failed';
|
|
errorsStore.setError({
|
|
heading: 'unraid-api failed to update Connect account configuration',
|
|
message: error.message,
|
|
level: 'error',
|
|
ref: 'connectSignInMutation',
|
|
type: 'account',
|
|
});
|
|
});
|
|
};
|
|
|
|
const connectSignOutMutation = async () => {
|
|
accountActionStatus.value = 'updating';
|
|
// @todo is this still needed here with the change to a mutation?
|
|
// if (!serverStore.registered && accountAction.value && !accountAction.value?.user) {
|
|
// accountActionHide.value = true;
|
|
// accountActionStatus.value = 'success';
|
|
// return;
|
|
// }
|
|
|
|
const { mutate: signOutMutation, onDone, onError } = await useMutation(CONNECT_SIGN_OUT);
|
|
|
|
signOutMutation();
|
|
|
|
onDone((res) => {
|
|
console.debug('[connectSignOutMutation]', res);
|
|
accountActionStatus.value = 'success';
|
|
setQueueConnectSignOut(false); // reset
|
|
});
|
|
|
|
onError((error) => {
|
|
logErrorMessages(error);
|
|
accountActionStatus.value = 'failed';
|
|
errorsStore.setError({
|
|
heading: 'Failed to update Connect account configuration',
|
|
message: error.message,
|
|
level: 'error',
|
|
ref: 'connectSignOutMutation',
|
|
type: 'account',
|
|
});
|
|
});
|
|
};
|
|
|
|
const setAccountAction = (action: ExternalSignIn | ExternalSignOut) => {
|
|
console.debug('[setAccountAction]', { action });
|
|
accountAction.value = action;
|
|
};
|
|
|
|
return {
|
|
// State
|
|
accountAction,
|
|
accountActionHide,
|
|
accountActionStatus,
|
|
// Getters
|
|
accountActionType,
|
|
// Actions
|
|
downgradeOs,
|
|
manage,
|
|
myKeys,
|
|
linkKey,
|
|
recover,
|
|
replace,
|
|
signIn,
|
|
signOut,
|
|
trialExtend,
|
|
trialStart,
|
|
updateOs,
|
|
setAccountAction,
|
|
setConnectSignInPayload,
|
|
setQueueConnectSignOut,
|
|
};
|
|
});
|