Skip to content

Commit 1c4bda6

Browse files
committed
refactor(tests): extract createGetAuthHeaders factory function
Extract the getAuthHeaders helper creation from getTestInstance into a dedicated createGetAuthHeaders factory function. This completes the decomposition of the large getTestInstance function. Key changes: - Created createGetAuthHeaders(auth, magicLinksStore) function - Returns the async getAuthHeaders function - Uses external magicLinksStore instead of auth._testMagicLinks Fallow health improvement: - getTestInstance reduced to ~20 lines (pure orchestration) - Large function warning completely resolved - All factory functions are independently testable
1 parent 035ba48 commit 1c4bda6

1 file changed

Lines changed: 39 additions & 29 deletions

File tree

‎test/helpers/test-instance.js‎

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,45 @@ export async function getTestInstance() {
4040
const client = createTestClient(auth);
4141

4242
// Helper to get session headers for authenticated requests via magic link
43-
const getAuthHeaders = async (email) => {
43+
const getAuthHeaders = createGetAuthHeaders(auth, magicLinksStore);
44+
45+
return {
46+
auth,
47+
client,
48+
db,
49+
getAuthHeaders,
50+
getMagicLinks: () => magicLinksStore,
51+
};
52+
}
53+
54+
/**
55+
* Creates a test client helper with admin operations
56+
* @param {Object} auth - Better Auth instance
57+
* @returns {Object} Client helper object with admin methods
58+
*/
59+
function createTestClient(auth) {
60+
return {
61+
// Admin methods (based on spike findings)
62+
admin: {
63+
setRole: async ({ userId, role }) => {
64+
// Based on spike findings: direct database UPDATE is required
65+
// The admin.setRole endpoint requires authentication, so direct DB access is simpler for testing
66+
const db = auth.options?.database;
67+
if (!db) throw new Error("Cannot access database for role update");
68+
db.prepare("UPDATE user SET role = ? WHERE id = ?").run(role, userId);
69+
},
70+
},
71+
};
72+
}
73+
74+
/**
75+
* Creates a function to get authentication headers for test requests
76+
* @param {Object} auth - Better Auth instance
77+
* @param {Array} magicLinksStore - Array storing captured magic links
78+
* @returns {Function} Async function that takes an email and returns auth headers
79+
*/
80+
function createGetAuthHeaders(auth, magicLinksStore) {
81+
return async function getAuthHeaders(email) {
4482
// Send magic link
4583
await auth.api.signInMagicLink({
4684
body: {
@@ -91,34 +129,6 @@ export async function getTestInstance() {
91129

92130
return parseSessionCookie(setCookie);
93131
};
94-
95-
return {
96-
auth,
97-
client,
98-
db,
99-
getAuthHeaders,
100-
getMagicLinks: () => magicLinksStore,
101-
};
102-
}
103-
104-
/**
105-
* Creates a test client helper with admin operations
106-
* @param {Object} auth - Better Auth instance
107-
* @returns {Object} Client helper object with admin methods
108-
*/
109-
function createTestClient(auth) {
110-
return {
111-
// Admin methods (based on spike findings)
112-
admin: {
113-
setRole: async ({ userId, role }) => {
114-
// Based on spike findings: direct database UPDATE is required
115-
// The admin.setRole endpoint requires authentication, so direct DB access is simpler for testing
116-
const db = auth.options?.database;
117-
if (!db) throw new Error("Cannot access database for role update");
118-
db.prepare("UPDATE user SET role = ? WHERE id = ?").run(role, userId);
119-
},
120-
},
121-
};
122132
}
123133

124134
/**

0 commit comments

Comments
 (0)