Skip to content

Commit 297f0df

Browse files
ci, tests: Fix tictactoe tests and include them in CI (#4699)
1 parent f6142e7 commit 297f0df

3 files changed

Lines changed: 67 additions & 69 deletions

File tree

.github/workflows/reusable-tests.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,8 @@ jobs:
536536
path: tests/test-instruction-validation
537537
- cmd: cd tests/account-generation-test && anchor test
538538
path: tests/account-generation-test
539+
- cmd: cd tests/tictactoe && anchor test
540+
path: tests/tictactoe
539541

540542
steps:
541543
- uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3

tests/tictactoe/programs/tictactoe/src/lib.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
1818
pub mod tictactoe {
1919
use super::*;
2020

21-
pub fn initialize_dashboard(ctx: Context<Initializedashboard>) -> Result<()> {
21+
pub fn initialize_dashboard(ctx: Context<InitializeDashboard>) -> Result<()> {
2222
let dashboard = &mut ctx.accounts.dashboard;
2323
dashboard.game_count = 0;
2424
dashboard.address = *dashboard.to_account_info().key;
@@ -34,15 +34,15 @@ pub mod tictactoe {
3434
Ok(())
3535
}
3636

37-
pub fn player_join(ctx: Context<Playerjoin>) -> Result<()> {
37+
pub fn player_join(ctx: Context<PlayerJoin>) -> Result<()> {
3838
let game = &mut ctx.accounts.game;
3939
game.player_o = *ctx.accounts.player_o.key;
4040
game.game_state = 1;
4141
Ok(())
4242
}
4343

44-
#[access_control(Playermove::accounts(&ctx, x_or_o, player_move))]
45-
pub fn player_move(ctx: Context<Playermove>, x_or_o: u8, player_move: u8) -> Result<()> {
44+
#[access_control(PlayerMove::accounts(&ctx, x_or_o, player_move))]
45+
pub fn player_move(ctx: Context<PlayerMove>, x_or_o: u8, player_move: u8) -> Result<()> {
4646
let game = &mut ctx.accounts.game;
4747
game.board[player_move as usize] = x_or_o;
4848
game.status(x_or_o);
@@ -55,13 +55,7 @@ pub mod tictactoe {
5555
}
5656

5757
#[derive(Accounts)]
58-
pub struct Status<'info> {
59-
dashboard: Account<'info, Dashboard>,
60-
game: Account<'info, Game>,
61-
}
62-
63-
#[derive(Accounts)]
64-
pub struct Initializedashboard<'info> {
58+
pub struct InitializeDashboard<'info> {
6559
#[account(zero)]
6660
dashboard: Account<'info, Dashboard>,
6761
authority: Signer<'info>,
@@ -77,34 +71,40 @@ pub struct Initialize<'info> {
7771
}
7872

7973
#[derive(Accounts)]
80-
pub struct Playerjoin<'info> {
74+
pub struct PlayerJoin<'info> {
8175
player_o: Signer<'info>,
82-
#[account(mut, constraint = game.game_state != 0 && game.player_x != Pubkey::default())]
76+
#[account(mut, constraint = game.game_state == 0 && game.player_x != Pubkey::default())]
8377
game: Account<'info, Game>,
8478
}
8579

8680
#[derive(Accounts)]
87-
pub struct Playermove<'info> {
81+
pub struct PlayerMove<'info> {
8882
player: Signer<'info>,
8983
#[account(mut)]
9084
game: Account<'info, Game>,
9185
}
9286

93-
impl<'info> Playermove<'info> {
94-
pub fn accounts(ctx: &Context<Playermove>, x_or_o: u8, player_move: u8) -> Result<()> {
87+
#[derive(Accounts)]
88+
pub struct Status<'info> {
89+
dashboard: Account<'info, Dashboard>,
90+
game: Account<'info, Game>,
91+
}
92+
93+
impl<'info> PlayerMove<'info> {
94+
pub fn accounts(ctx: &Context<PlayerMove>, x_or_o: u8, player_move: u8) -> Result<()> {
9595
if ctx.accounts.game.board[player_move as usize] != 0 {
9696
return Err(ErrorCode::Illegalmove.into());
9797
}
9898
if x_or_o == BOARD_ITEM_X {
99-
return Playermove::player_x_checks(ctx);
99+
return PlayerMove::player_x_checks(ctx);
100100
} else if x_or_o == BOARD_ITEM_O {
101-
return Playermove::player_o_checks(ctx);
101+
return PlayerMove::player_o_checks(ctx);
102102
} else {
103103
return Err(ErrorCode::UnexpectedValue.into());
104104
}
105105
}
106106

107-
pub fn player_x_checks(ctx: &Context<Playermove>) -> Result<()> {
107+
pub fn player_x_checks(ctx: &Context<PlayerMove>) -> Result<()> {
108108
if ctx.accounts.game.player_x != *ctx.accounts.player.key {
109109
return Err(ErrorCode::Unauthorized.into());
110110
}
@@ -114,7 +114,7 @@ impl<'info> Playermove<'info> {
114114
Ok(())
115115
}
116116

117-
pub fn player_o_checks(ctx: &Context<Playermove>) -> Result<()> {
117+
pub fn player_o_checks(ctx: &Context<PlayerMove>) -> Result<()> {
118118
if ctx.accounts.game.player_o != *ctx.accounts.player.key {
119119
return Err(ErrorCode::Unauthorized.into());
120120
}
@@ -196,7 +196,7 @@ impl Game {
196196
}
197197
}
198198

199-
#[error]
199+
#[error_code]
200200
pub enum ErrorCode {
201201
#[msg("You are not authorized to perform this action.")]
202202
Unauthorized,

tests/tictactoe/tests/tictactoe.js

Lines changed: 44 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,145 +3,142 @@ const anchor = require("@anchor-lang/core");
33
describe("tictactoe", () => {
44
anchor.setProvider(anchor.AnchorProvider.env());
55
const program = anchor.workspace.Tictactoe;
6-
let dashboard = anchor.web3.Keypair.generate();
7-
let game = anchor.web3.Keypair.generate();
8-
let player_o = anchor.web3.Keypair.generate();
6+
7+
const dashboard = anchor.web3.Keypair.generate();
8+
const game = anchor.web3.Keypair.generate();
9+
const playerO = anchor.web3.Keypair.generate();
10+
const playerX = program.provider.wallet;
911

1012
it("Initialize Dashboard", async () => {
1113
const tx = await program.rpc.initializeDashboard({
1214
accounts: {
13-
authority: program.provider.wallet.publicKey,
15+
authority: playerX.publicKey,
1416
dashboard: dashboard.publicKey,
15-
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
1617
},
1718
signers: [dashboard],
1819
instructions: [
1920
await program.account.dashboard.createInstruction(dashboard),
2021
],
2122
});
22-
23-
console.log("transaction: ", tx);
23+
console.log("transaction:", tx);
2424
});
2525

2626
it("Initialize Game", async () => {
2727
const tx = await program.rpc.initialize({
2828
accounts: {
29-
playerX: program.provider.wallet.publicKey,
29+
playerX: playerX.publicKey,
3030
dashboard: dashboard.publicKey,
3131
game: game.publicKey,
32-
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
3332
},
3433
signers: [game],
3534
instructions: [await program.account.game.createInstruction(game)],
3635
});
37-
38-
console.log("transaction: ", tx);
36+
console.log("transaction:", tx);
3937
});
4038

4139
it("Player O joins", async () => {
4240
const tx = await program.rpc.playerJoin({
4341
accounts: {
44-
playerO: player_o.publicKey,
42+
playerO: playerO.publicKey,
4543
game: game.publicKey,
4644
},
47-
signers: [player_o],
45+
signers: [playerO],
4846
});
49-
50-
console.log("transaction: ", tx);
47+
console.log("transaction:", tx);
5148
});
5249

53-
it("Player x plays", async () => {
50+
it("Player X plays", async () => {
5451
const tx = await program.rpc.playerMove(1, 0, {
5552
accounts: {
56-
player: program.provider.wallet.publicKey,
53+
player: playerX.publicKey,
5754
game: game.publicKey,
5855
},
5956
});
60-
console.log("transaction: ", tx);
57+
console.log("transaction:", tx);
6158
});
6259

63-
it("Player o plays", async () => {
60+
it("Player O plays", async () => {
6461
const tx = await program.rpc.playerMove(2, 1, {
6562
accounts: {
66-
player: player_o.publicKey,
63+
player: playerO.publicKey,
6764
game: game.publicKey,
6865
},
69-
signers: [player_o],
66+
signers: [playerO],
7067
});
71-
console.log("transaction: ", tx);
68+
console.log("transaction:", tx);
7269
});
7370

74-
it("Player x plays", async () => {
71+
it("Player X plays", async () => {
7572
const tx = await program.rpc.playerMove(1, 3, {
7673
accounts: {
77-
player: program.provider.wallet.publicKey,
74+
player: playerX.publicKey,
7875
game: game.publicKey,
7976
},
8077
});
81-
console.log("transaction: ", tx);
78+
console.log("transaction:", tx);
8279
});
8380

84-
it("Player o plays", async () => {
81+
it("Player O plays", async () => {
8582
const tx = await program.rpc.playerMove(2, 6, {
8683
accounts: {
87-
player: player_o.publicKey,
84+
player: playerO.publicKey,
8885
game: game.publicKey,
8986
},
90-
signers: [player_o],
87+
signers: [playerO],
9188
});
92-
console.log("transaction: ", tx);
89+
console.log("transaction:", tx);
9390
});
9491

95-
it("Player x plays", async () => {
92+
it("Player X plays", async () => {
9693
const tx = await program.rpc.playerMove(1, 2, {
9794
accounts: {
98-
player: program.provider.wallet.publicKey,
95+
player: playerX.publicKey,
9996
game: game.publicKey,
10097
},
10198
});
102-
console.log("transaction: ", tx);
99+
console.log("transaction:", tx);
103100
});
104101

105-
it("Player o plays", async () => {
102+
it("Player O plays", async () => {
106103
const tx = await program.rpc.playerMove(2, 4, {
107104
accounts: {
108-
player: player_o.publicKey,
105+
player: playerO.publicKey,
109106
game: game.publicKey,
110107
},
111-
signers: [player_o],
108+
signers: [playerO],
112109
});
113-
console.log("transaction: ", tx);
110+
console.log("transaction:", tx);
114111
});
115112

116-
it("Player x plays", async () => {
113+
it("Player X plays", async () => {
117114
const tx = await program.rpc.playerMove(1, 5, {
118115
accounts: {
119-
player: program.provider.wallet.publicKey,
116+
player: playerX.publicKey,
120117
game: game.publicKey,
121118
},
122119
});
123-
console.log("transaction: ", tx);
120+
console.log("transaction:", tx);
124121
});
125122

126-
it("Player o plays", async () => {
123+
it("Player O plays", async () => {
127124
const tx = await program.rpc.playerMove(2, 8, {
128125
accounts: {
129-
player: player_o.publicKey,
126+
player: playerO.publicKey,
130127
game: game.publicKey,
131128
},
132-
signers: [player_o],
129+
signers: [playerO],
133130
});
134-
console.log("transaction: ", tx);
131+
console.log("transaction:", tx);
135132
});
136133

137-
it("Player x plays", async () => {
134+
it("Player X plays", async () => {
138135
const tx = await program.rpc.playerMove(1, 7, {
139136
accounts: {
140-
player: program.provider.wallet.publicKey,
137+
player: playerX.publicKey,
141138
game: game.publicKey,
142139
},
143140
});
144-
console.log("transaction: ", tx);
141+
console.log("transaction:", tx);
145142
});
146143

147144
it("Status", async () => {
@@ -151,7 +148,6 @@ describe("tictactoe", () => {
151148
game: game.publicKey,
152149
},
153150
});
154-
155-
console.log("transaction: ", tx);
151+
console.log("transaction:", tx);
156152
});
157153
});

0 commit comments

Comments
 (0)