Skip to content

Commit

Permalink
Merge pull request lichess-org#15492 from fitztrev/vitest
Browse files Browse the repository at this point in the history
Vitest with jsdom for frontend tests
  • Loading branch information
ornicar authored Jun 13, 2024
2 parents 7571264 + 9107bec commit dc8dd09
Show file tree
Hide file tree
Showing 21 changed files with 1,280 additions and 2,070 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/assets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ jobs:
- uses: actions/checkout@v4
with:
submodules: recursive
- uses: pnpm/action-setup@v3
- uses: actions/setup-node@v4
with:
node-version: 22
- uses: pnpm/action-setup@v4
with:
run_install: true
- uses: actions/checkout@v4
Expand All @@ -39,7 +42,8 @@ jobs:
- run: pnpm link "$GITHUB_WORKSPACE/ab"
if: steps.ab.outcome == 'success'
- run: ./ui/build --no-install -p
- run: cd ui && pnpm run test && cd -
- run: pnpm test
working-directory: ui
- run: mkdir assets && mv public assets/ && cp -p bin/download-lifat LICENSE COPYING.md README.md assets/ && git log -n 1 --pretty=oneline > assets/commit.txt
- run: cd assets && tar --zstd -cvpf ../assets.tar.zst . && cd -
- uses: actions/upload-artifact@v4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
security-events: write
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v3
- uses: pnpm/action-setup@v4
with:
run_install: true
- uses: github/codeql-action/init@v3
Expand Down
3,010 changes: 1,043 additions & 1,967 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion ui/@types/lichess/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
// file://./../../site/src/site.ts
interface Site {
debug: boolean;
info: any;
info: {
commit: string;
message: string;
date: string;
};
StrongSocket: {
// file://./../../site/src/socket.ts
new (url: string, version: number | false, cfg?: any): any;
Expand Down
11 changes: 7 additions & 4 deletions ui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Client builds are performed by the ui/build script. Stick to `ui/build -w` and l

Usage examples:

```
```bash
ui/build # builds all client assets in dev mode
ui/build -w # builds all client assets and watches for changes
ui/build -p # builds minified client assets (prod builds)
Expand All @@ -22,11 +22,14 @@ Usage examples:

## Testing

The frontend uses the Jest testing framework.
The frontend uses the [Vitest](https://vitest.dev/) testing framework.

```bash
cd ui

```
cd ui/
pnpm test
## or
pnpm test:watch
```

## CSS
Expand Down
2 changes: 1 addition & 1 deletion ui/chat/src/spam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const skip = (txt: string) => (suspLink(txt) || followMe(txt)) && !isKnow
export const selfReport = (txt: string) => {
if (isKnownSpammer()) return;
const hasSuspLink = suspLink(txt);
if (hasSuspLink) xhr.text(`/jslog/${window.location.href.substr(-12)}?n=spam`, { method: 'post' });
if (hasSuspLink) xhr.text(`/jslog/${window.location.href.slice(-12)}?n=spam`, { method: 'post' });
if (hasSuspLink || followMe(txt)) site.storage.set('chat-spam', '1');
};

Expand Down
25 changes: 25 additions & 0 deletions ui/chat/tests/spam.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { expect, test, vi } from 'vitest';
import { selfReport } from '../src/spam';
import * as xhr from 'common/xhr';

test('self report', () => {
vi.stubGlobal('window', { location: { href: 'https://lichess.org/abcdef123456' } });

vi.stubGlobal('site', {
storage: {
set: (key: string, value: string) => {
expect(key).toBe('chat-spam');
expect(value).toBe('1');
},
get: () => '0',
},
});

const spy = vi.spyOn(xhr, 'text').mockImplementation((url: string, init?: RequestInit) => {
expect(url).toBe('/jslog/abcdef123456?n=spam');
return Promise.resolve('ok');
});

selfReport('check out bit.ly/spam');
expect(spy).toHaveBeenCalled();
});
6 changes: 3 additions & 3 deletions ui/common/src/richText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { VNode, Hooks } from 'snabbdom';
export const linkRegex =
/(^|[\s\n]|<[A-Za-z]*\/?>)((?:(?:https?|ftp):\/\/|lichess\.org)[\-A-Z0-9+\u0026\u2019@#\/%?=()~_|!:,.;]*[\-A-Z0-9+\u0026@#\/%=~()_|])/gi;
export const newLineRegex = /\n/g;
export const userPattern = /(^|[^\w@#/])@([a-z0-9][a-z0-9_-]{0,28}[a-z0-9])/gi;
export const userPattern = /(^|[^\w@#/])@([a-zA-Z0-9_-]{2,30})/gi;

// looks like it has a @mention or #gameid or a url.tld
export const isMoreThanText = (str: string) => /(\n|(@|#|\.)\w{2,}|(board|game) \d)/i.test(str);
Expand Down Expand Up @@ -59,8 +59,8 @@ export function richHTML(text: string, newLines = true): Hooks {

const linkPattern = /\b\b(?:https?:\/\/)?(lichess\.org\/[-\w+&'@#\/%?=()~|!:,.;]+[\w+&@#\/%=~|])/gi;
const pawnDropPattern = /^[a-h][2-7]$/;
const movePattern =
/\b(\d+)\s*(\.+)\s*(?:[o0-]+[o0]|[NBRQKP\u2654\u2655\u2656\u2657\u2658\u2659]?[a-h]?[1-8]?[x@]?[a-z][1-8](?:=[NBRQK\u2654\u2655\u2656\u2657\u2658\u2659])?)\+?#?[!\?=]{0,5}/gi;
export const movePattern =
/\b(\d+)\s*(\.+)\s*(?:[o0-]+[o0]|[NBRQKP\u2654\u2655\u2656\u2657\u2658\u2659]?[a-h]?[1-8]?[x@]?[a-h][1-8](?:=[NBRQK\u2654\u2655\u2656\u2657\u2658\u2659])?)\+?#?[!\?=]{0,5}/gi;
const boardPattern = /\b(?:board|game)\s(\d{1,2})/gi;

function moveReplacer(match: string, turn: number, dots: string) {
Expand Down
31 changes: 31 additions & 0 deletions ui/common/tests/richText.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { describe, expect, test, vi } from 'vitest';
import { movePattern, userPattern } from '../src/richText';

describe('test regex patterns', () => {
test('username mentions', () => {
expect('@foo'.match(userPattern)).toStrictEqual(['@foo']);
expect('@foo-'.match(userPattern)).toStrictEqual(['@foo-']);
expect('@__foo'.match(userPattern)).toStrictEqual(['@__foo']);
});

test.each([
['1.e4'],
['1. e4'],
['5...Nf6'],
['5... Nf6'],
['12.♔d1'],
['10.O-O-O'],
['10.o-o-o'],
['10.0-0-0'],
])('moves', move => {
expect(move.match(movePattern)).toStrictEqual([move]);
});

test('move with comment', () => {
expect('I considered 34. f7+ instead'.match(movePattern)).toStrictEqual(['34. f7+']);
});

test('not a move', () => {
expect('4.m3'.match(movePattern)).toBeNull();
});
});
2 changes: 1 addition & 1 deletion ui/game/src/status.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GameData } from './interfaces';

// https://github.com/lichess-org/scalachess/blob/master/src/main/scala/Status.scala
// https://github.com/lichess-org/scalachess/blob/master/core/src/main/scala/Status.scala

export const ids = {
created: 10,
Expand Down
9 changes: 0 additions & 9 deletions ui/jest.config.js

This file was deleted.

1 change: 0 additions & 1 deletion ui/keyboardMove/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"author": "Thibault Duplessis",
"license": "AGPL-3.0-or-later",
"dependencies": {
"@jest/globals": "^29.7.0",
"chess": "workspace:*",
"common": "workspace:*",
"game": "workspace:*",
Expand Down
Loading

0 comments on commit dc8dd09

Please sign in to comment.