Skip to content

Commit bbb4b1f

Browse files
committed
Cleanup mock-auth
1 parent 2683b65 commit bbb4b1f

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

test/mock-auth.mts

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,47 +25,53 @@ import type { CResult } from '../src/types.mts'
2525

2626
export interface MockAuthOptions {
2727
/** Whether the operation should succeed. */
28-
shouldSucceed?: boolean
28+
shouldSucceed?: boolean | undefined
2929
/** Custom delay in milliseconds to simulate network latency. */
30-
delay?: number
30+
delay?: number | undefined
3131
/** Custom error message for failure scenarios. */
32-
errorMessage?: string
32+
errorMessage?: string | undefined
3333
/** Custom response data for success scenarios. */
34-
responseData?: any
34+
responseData?: any | undefined
3535
}
3636

3737
export interface MockLoginOptions extends MockAuthOptions {
3838
/** Mock email address for login. */
39-
email?: string
39+
email?: string | undefined
4040
/** Mock organization slug. */
41-
orgSlug?: string
41+
orgSlug?: string | undefined
4242
/** Mock API token to return. */
43-
apiToken?: string
43+
apiToken?: string | undefined
4444
/** Whether to simulate MFA requirement. */
45-
requireMfa?: boolean
45+
requireMfa?: boolean | undefined
4646
}
4747

4848
export interface MockTokenOptions extends MockAuthOptions {
4949
/** The token to validate. */
50-
token?: string
50+
token?: string | undefined
5151
/** Token permissions/scopes. */
52-
scopes?: string[]
52+
scopes?: string[] | readonly string[] | undefined
5353
/** Token expiration time. */
54-
expiresAt?: Date
54+
expiresAt?: Date | undefined
5555
}
5656

5757
export interface MockOrgOptions extends MockAuthOptions {
5858
/** List of organizations to return. */
59-
organizations?: Array<{
60-
id: string
61-
slug: string
62-
name: string
63-
role: string
64-
}>
59+
organizations?:
60+
| Array<{
61+
id: string
62+
slug: string
63+
name: string
64+
role: string
65+
}>
66+
| undefined
6567
/** Selected organization index. */
66-
selectedIndex?: number
68+
selectedIndex?: number | undefined
6769
}
6870

71+
const MILLISECONDS_1_DAY = Date.now() + 24 * 60 * 60 * 1000
72+
73+
const MILLISECONDS_30_DAYS = Date.now() + 30 * 24 * 60 * 60 * 1000
74+
6975
/**
7076
* Simulate a delay for realistic async behavior.
7177
*/
@@ -77,7 +83,7 @@ function simulateDelay(ms: number): Promise<void> {
7783
* Mock interactive login flow.
7884
*/
7985
export async function mockInteractiveLogin(
80-
options?: MockLoginOptions,
86+
options?: MockLoginOptions | undefined,
8187
): Promise<CResult<{ apiToken: string; orgSlug: string }>> {
8288
const {
8389
apiToken = 'test-token-123',
@@ -124,7 +130,7 @@ export async function mockApiTokenAuth(
124130
const {
125131
delay = 50,
126132
errorMessage = 'Invalid token',
127-
expiresAt = new Date(Date.now() + 30 * 24 * 60 * 60 * 1000), // 30 days.
133+
expiresAt = new Date(MILLISECONDS_30_DAYS),
128134
scopes = ['read', 'write'],
129135
shouldSucceed = true,
130136
token = 'test-token',
@@ -464,9 +470,7 @@ export async function mockValidateSession(
464470
ok: true,
465471
data: {
466472
valid: isValid,
467-
expiresAt: isValid
468-
? new Date(Date.now() + 24 * 60 * 60 * 1000) // 24 hours.
469-
: undefined,
473+
expiresAt: isValid ? new Date(MILLISECONDS_1_DAY) : undefined,
470474
},
471475
}
472476
}

0 commit comments

Comments
 (0)