Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,38 @@ jobs:
with:
cmd: lint

- name: Run Unit Tests
uses: borales/actions-yarn@v5
with:
cmd: test --reporter=junit --outputFile=junit.xml --coverage

- name: Build
uses: borales/actions-yarn@v5
with:
cmd: build

- name: Upload Test Results
if: always()
uses: actions/upload-artifact@v4
with:
name: Test Results
path: junit.xml

publish-test-results:
name: 'Publish Test Results'
needs: build-and-test
runs-on: ubuntu-latest
permissions:
checks: write
pull-requests: write
if: always()
steps:
- name: Download Artifacts
uses: actions/download-artifact@v4
with:
path: artifacts

- name: Publish Test Results
uses: EnricoMi/publish-unit-test-result-action@v2
with:
junit_files: 'artifacts/**/*.xml'
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
node_modules/
dist/
coverage/
junit.xml
yarn-*.log
.env.local
**/.DS_Store
Expand Down
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
dist/
coverage/
junit.xml
package.json
6 changes: 0 additions & 6 deletions babel.config.cjs

This file was deleted.

12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
"prebuild": "rimraf dist",
"build": "tsc -b ./src/tsconfig.cjs.json ./src/tsconfig.esm.json ./src/tsconfig.types.json",
"start": "tsx watch examples/index.tsx",
"test": "run-s test:compile \"test:run {@}\" --",
"test:compile": "tsc -p tsconfig.test.json",
"test:run": "vitest",
"format": "run-s \"format:no-write --write\"",
"format:no-write": "prettier \"./**/*.{js,ts,md,json,yml,yaml}\"",
"format:check": "run-s \"format:no-write --check\"",
Expand All @@ -49,28 +52,29 @@
"yoga-layout-prebuilt": "^1.10.0"
},
"devDependencies": {
"@babel/core": "^7.17.10",
"@babel/preset-env": "^7.17.10",
"@babel/preset-typescript": "^7.16.7",
"@types/debug": "^4.1.7",
"@types/mitm": "^1.3.8",
"@types/node": "^18.11.9",
"@types/ramda": "^0.30.0",
"@types/react": "^18.2.23",
"@typescript-eslint/eslint-plugin": "^5.22.0",
"@typescript-eslint/parser": "^5.22.0",
"@vitest/coverage-v8": "3.2.1",
"dotenv": "^16.4.7",
"esbuild": "^0.15.13",
"eslint": "^8.14.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-simple-import-sort": "^8.0.0",
"mitm": "^1.7.3",
"node-insim": "^4.6.1",
"npm-run-all": "^4.1.5",
"prettier": "^2.6.2",
"react": "^18.2.0",
"rimraf": "^5.0.5",
"tsx": "^3.12.6",
"typescript": "^4.9.5"
"typescript": "^4.9.5",
"vitest": "^3.2.1"
},
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
}
127 changes: 127 additions & 0 deletions tests/button.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import Mitm from 'mitm';
import { InSim } from 'node-insim';
import { IS_BTN, IS_ISI } from 'node-insim/packets';
import { afterEach, beforeEach, describe, it } from 'vitest';

import { Button, createRoot } from '../src';
import {
getTCPConnectionPromise,
sendVersionPacket,
wait,
} from './packetInterceptor';

describe('Buttons', () => {
let mitm: ReturnType<typeof Mitm>;
let inSim: InSim;
let waitForTCPConnection: ReturnType<typeof getTCPConnectionPromise>;

beforeEach(() => {
mitm = Mitm();
inSim = new InSim();
waitForTCPConnection = getTCPConnectionPromise(mitm, '127.0.0.1', 29999);

inSim.connect({
ReqI: 255,
Host: '127.0.0.1',
Port: 29999,
});
});

afterEach(() => {
mitm.disable();
inSim.disconnect();
});

it('should send a button', async () => {
const root = createRoot(inSim);
root.render(
<Button width={20} height={5}>
Hello world
</Button>,
);

const { packetInterceptor, socket } = await waitForTCPConnection;

await packetInterceptor.waitForPacket(
new IS_ISI({
ReqI: 255,
InSimVer: 9,
}),
);

await wait(10);
await sendVersionPacket(socket, 255);

await packetInterceptor.waitForPacket(
new IS_BTN({
ClickID: 0,
ReqI: 1,
W: 20,
H: 5,
Text: 'Hello world',
}),
);
});

it('should send multiple buttons with incremental unique ClickIDs', async () => {
const root = createRoot(inSim);
root.render(
<>
<Button width={20} height={5}>
One
</Button>
<Button top={20} left={50} width={40} height={10}>
Two
</Button>
<Button top={30} left={60} width={10} height={5}>
Three
</Button>
</>,
);

const { packetInterceptor, socket } = await waitForTCPConnection;

await packetInterceptor.waitForPacket(
new IS_ISI({
ReqI: 255,
InSimVer: 9,
}),
);

await wait(50);
await sendVersionPacket(socket, 255);

await packetInterceptor.waitForPacket(
new IS_BTN({
ClickID: 0,
ReqI: 1,
W: 20,
H: 5,
Text: 'One',
}),
);
await packetInterceptor.waitForPacket(
new IS_BTN({
ClickID: 1,
ReqI: 1,
T: 20,
L: 50,
W: 40,
H: 10,
Text: 'Two',
}),
);
await packetInterceptor.waitForPacket(
new IS_BTN({
ClickID: 2,
ReqI: 1,
T: 30,
L: 60,
W: 10,
H: 5,
Text: 'Three',
}),
);
await packetInterceptor.assertNoMoreData();
});
});
38 changes: 38 additions & 0 deletions tests/connection.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Mitm from 'mitm';
import { InSim } from 'node-insim';
import { IS_ISI } from 'node-insim/packets';
import { describe, it } from 'vitest';

import { createRoot } from '../src';
import { getTCPConnectionPromise } from './packetInterceptor';

describe('InSim connection', () => {
it('should connect to InSim and send an IS_ISI packet', async () => {
const mitm: ReturnType<typeof Mitm> = Mitm();
const inSim: InSim = new InSim();
const waitForTCPConnection: ReturnType<typeof getTCPConnectionPromise> =
getTCPConnectionPromise(mitm, '127.0.0.1', 29999);

inSim.connect({
ReqI: 255,
Host: '127.0.0.1',
Port: 29999,
});

createRoot(inSim);

const { packetInterceptor } = await waitForTCPConnection;

await packetInterceptor.waitForPacket(
new IS_ISI({
ReqI: 255,
InSimVer: 9,
}),
);

await packetInterceptor.assertNoMoreData();

mitm.disable();
inSim.disconnect();
});
});
Loading
Loading