Skip to content

Commit

Permalink
chore: test to reproduce issue
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarenaldi committed Dec 13, 2024
1 parent d36d021 commit cd2f52c
Show file tree
Hide file tree
Showing 6 changed files with 260 additions and 20 deletions.
4 changes: 3 additions & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@
},
"devDependencies": {
"@lens-network/sdk": "0.0.0-canary-20241203140504",
"@lens-protocol/metadata": "2.0.0-next.2",
"tsup": "^8.3.5",
"typescript": "^5.6.3",
"viem": "^2.21.53"
"viem": "^2.21.53",
"zod": "^3.23.8"
},
"license": "MIT"
}
2 changes: 1 addition & 1 deletion packages/client/src/actions/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,5 @@ export function switchAccount(
client: SessionClient,
request: SwitchAccountRequest,
): ResultAsync<SwitchAccountResult, UnauthenticatedError | UnexpectedError> {
return client.query(SwitchAccountMutation, { request });
return client.mutation(SwitchAccountMutation, { request });
}
47 changes: 47 additions & 0 deletions packages/client/src/actions/onboarding.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { account } from '@lens-protocol/metadata';
import { assertOk } from '@lens-protocol/types';
import { describe, it } from 'vitest';

import { loginAsOnboardingUser, signerWallet } from '../../testing-utils';
import { handleWith } from '../viem';
import { createAccountWithUsername, fetchAccount } from './account';
import { switchAccount } from './authentication';

const walletClient = signerWallet();
const metadata = account({
name: 'John Doe',
bio: 'A test account',
});
const metadataUri = `data:application/json,${JSON.stringify(metadata)}`;

const handler = handleWith(walletClient);

describe('Given an onboarding user', () => {
describe('When switching to the newly created account', () => {
it('Then it should be authenticated', async () => {
// Login as onboarding user
const result = await loginAsOnboardingUser().andThen((sessionClient) =>
// Create an account with username
createAccountWithUsername(sessionClient, {
username: { localName: `testname${Date.now()}` },
metadataUri,
})
// Sign if necessary
.andThen(handler)

// Wait for the transaction to be mined
.andThen(sessionClient.waitForTransaction as any)

// Fetch the account
.andThen((txHash) => fetchAccount(sessionClient, { txHash }))

// Switch to the newly created account
.andThen((account) => switchAccount(sessionClient, { account: account?.address })),
);

console.log(result);

assertOk(result);

Check failure on line 44 in packages/client/src/actions/onboarding.test.ts

View workflow job for this annotation

GitHub Actions / Verify / Test

src/actions/onboarding.test.ts > Given an onboarding user > When switching to the newly created account > Then it should be authenticated

InvariantError: Expected result to be Ok: Could not GET url: data:application/json,{"$schema":"https://json-schemas.lens.dev/account/1.0.0.json","lens":{"id":"5ac6d0bb-175c-47d4-bf11-86facf40eb54","name":"John Doe","bio":"A test account"}} - Request error: builder error for url (data:application/json,{"$schema":"https://json-schemas.lens.dev/account/1.0.0.json","lens":{"id":"5ac6d0bb-175c-47d4-bf11-86facf40eb54","name":"John Doe","bio":"A test account"}}) ❯ Module.U ../types/src/helpers/assertions.ts:31:11 ❯ src/actions/onboarding.test.ts:44:7
});
});
});
32 changes: 14 additions & 18 deletions packages/client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,20 @@ export type ErrorResponse<T extends string> = {
reason: string;
};

export type DelegableOperationResult<
O extends string,
E extends string,
OR extends OperationResponse<string> = OperationResponse<O>,
ER extends ErrorResponse<string> = ErrorResponse<E>,
> = OR | SponsoredTransactionRequest | SelfFundedTransactionRequest | ER;

export type RestrictedOperationResult<
E extends string,
ER extends ErrorResponse<string> = ErrorResponse<E>,
> = SponsoredTransactionRequest | SelfFundedTransactionRequest | ER;

export type OperationResult<
O extends string,
E extends string,
OR extends OperationResponse<string> = OperationResponse<O>,
ER extends ErrorResponse<string> = ErrorResponse<E>,
> = DelegableOperationResult<O, E, OR, ER> | RestrictedOperationResult<E, ER>;
export type DelegableOperationResult<O extends string, E extends string> =
| OperationResponse<O>
| SponsoredTransactionRequest
| SelfFundedTransactionRequest
| ErrorResponse<E>;

export type RestrictedOperationResult<E extends string> =
| SponsoredTransactionRequest
| SelfFundedTransactionRequest
| ErrorResponse<E>;

export type OperationResult<O extends string, E extends string> =
| DelegableOperationResult<O, E>
| RestrictedOperationResult<E>;

export type RestrictedOperationHandler<E extends string> = (
result: RestrictedOperationResult<E>,
Expand Down
25 changes: 25 additions & 0 deletions packages/client/testing-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { chains } from '@lens-network/sdk/viem';
import { evmAddress } from '@lens-protocol/types';
import { http, type Account, type Transport, type WalletClient, createWalletClient } from 'viem';
import { privateKeyToAccount } from 'viem/accounts';
import { PublicClient, testnet } from './src';

Expand All @@ -22,3 +24,26 @@ export function loginAsAccountOwner() {
signMessage: (message) => signer.signMessage({ message }),
});
}

export function loginAsOnboardingUser() {
const client = PublicClient.create({
environment: testnet,
origin: 'http://example.com',
});

return client.login({
onboardingUser: {
wallet: owner,
app,
},
signMessage: (message) => signer.signMessage({ message }),
});
}

export function signerWallet(): WalletClient<Transport, chains.LensNetworkChain, Account> {
return createWalletClient({
account: privateKeyToAccount(import.meta.env.PRIVATE_KEY),
chain: chains.testnet,
transport: http(),
});
}
Loading

0 comments on commit cd2f52c

Please sign in to comment.