Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 19x 19x 19x 19x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 19x 1x | import {type IAssert, type IMeta, handler} from '@feasibleone/blong';
/**
* Extract the HTTP status code from an error thrown by the handler proxy
* (JSON-RPC codec or MLE errorReceive path).
*/
function getHttpStatus(err: unknown): number | undefined {
const e = err as Record<string, unknown>;
return (
((e.res as Record<string, unknown>)?.statusCode as number | undefined) ??
(e.statusCode as number | undefined) ??
((e.params as Record<string, unknown>)?.code as number | undefined)
);
}
/**
* browser/test/test/testSessionFlow.ts — browser-platform (server-run, via the
* MLE codec) session + refresh + restore flow.
*
* Exercises the codec/semi-SDK with NO UI: login stores tokens in memory,
* explicit `login.token.refresh` re-mints them, a protected call still works
* afterwards, and `login.token.restore` without a cookie is refused (401) —
* the reload-skip-login path with a real cookie is covered by Playwright.
*
* Registered as the `test.session.flow` group in browser-test.ts.
*/
export default handler(
({
lib: {group},
handler: {loginTokenCreate, loginTokenRefresh, loginTokenRestore, accessTestPrivate},
}) => ({
testSessionFlow: ({
name = 'session flow browser',
hasSession = false,
}: {name?: string; hasSession?: boolean} = {}) =>
group(name)([
// 1. Login over HTTP — session + tokens.
async function login(assert: IAssert, {$meta}: {$meta: IMeta}) {
const result = await loginTokenCreate<{
access_token: string;
refresh_token: string;
session_id: string;
permissions: string[];
}>({username: 'testUser', password: 'testPassword'}, $meta);
assert.ok(result.access_token, 'login returns an access token');
assert.ok(result.refresh_token, 'login returns a refresh token');
assert.ok(result.session_id, 'login returns a session id');
return result;
},
// 2. Explicit renewal via the codec — new tokens, same session.
async function refresh(
assert: IAssert,
{
$meta,
login,
}: {$meta: IMeta; login: Awaited<{refresh_token: string; session_id: string}>},
) {
const t = await login;
const refreshed = await loginTokenRefresh<{
access_token: string;
refresh_token: string;
session_id: string;
}>({refreshToken: t.refresh_token}, $meta);
assert.ok(refreshed.access_token, 'refresh returns a new access token');
assert.ok(refreshed.refresh_token, 'refresh returns a new refresh token');
assert.equal(
refreshed.session_id,
t.session_id,
'session preserved on refresh',
);
return refreshed;
},
// 3. A protected call still works with the refreshed token.
async function protectedCall(assert: IAssert, {$meta}: {$meta: IMeta}) {
const result = await accessTestPrivate<{success: boolean}>({}, $meta);
assert.equal(result.success, true, 'protected call succeeds after refresh');
},
// 4. Restore: without a cookie it is refused with an auth-classified
// error; a host page that is already signed in exercises the
// successful restore instead.
async function restoreNoCookie(assert: IAssert, {$meta}: {$meta: IMeta}) {
if (hasSession) {
const restored = await loginTokenRestore<{access_token: string}>({}, $meta);
assert.ok(
restored.access_token,
'restore with the host session returns a token',
);
return;
}
try {
// No cookie is the normal first visit, not a failure: declaring the
// type keeps the refusal out of the error stream and the dialog
// (@@D-695).
await loginTokenRestore({}, {...$meta, expect: 'login.invalidCookie'});
assert.fail('login.token.restore should fail without a cookie');
} catch (err: unknown) {
const status = getHttpStatus(err);
assert.equal(status, 200, 'restore without a cookie returns HTTP 200');
}
},
]),
}),
);
|