Add TypeScript ESLint configs and lint scripts
Add a repository .editorconfig and a root eslint.config.ts to standardize formatting and linting. Migrate packages/webui ESLint config to TypeScript (eslint.config.ts) with project-specific rules and tsconfigRootDir handling, and remove the old JS config. Update package.json to run ESLint directly (add lint and lint:fix) and add required ESLint/dev dependencies; also add jiti to packages/webui/package.json. Include a small formatting tweak to tsconfig.base.json.
This commit is contained in:
1 parent
f21740a5d3
commit
aea2676c31
30 files changed
+173
-1057
No files matched your search
@@ -1,173 +0,0 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { protoDecode, protoEncode, type ProtoSchema } from '../src/protobuf/decode';
|
||||
import { WireMessage, encodeKey, encodeVarint, WireType } from '../src/protobuf/wire';
|
||||
|
||||
describe('protoDecode', () => {
|
||||
const SimpleSchema = {
|
||||
name: { field: 1, type: 'string' as const },
|
||||
age: { field: 2, type: 'uint32' as const },
|
||||
active: { field: 3, type: 'bool' as const },
|
||||
};
|
||||
|
||||
function buildProto(): Uint8Array {
|
||||
const out: number[] = [];
|
||||
// field 1 string "Alice"
|
||||
encodeKey(1, WireType.LengthDelimited, out);
|
||||
const str = new TextEncoder().encode('Alice');
|
||||
encodeVarint(str.length, out);
|
||||
out.push(...str);
|
||||
// field 2 uint32 = 30
|
||||
encodeKey(2, WireType.Varint, out);
|
||||
encodeVarint(30, out);
|
||||
// field 3 bool = true
|
||||
encodeKey(3, WireType.Varint, out);
|
||||
encodeVarint(1, out);
|
||||
return new Uint8Array(out);
|
||||
}
|
||||
|
||||
it('decodes all fields', () => {
|
||||
const result = protoDecode(buildProto(), SimpleSchema);
|
||||
expect(result).not.toBeNull();
|
||||
expect(result!.name).toBe('Alice');
|
||||
expect(result!.age).toBe(30);
|
||||
expect(result!.active).toBe(true);
|
||||
});
|
||||
|
||||
it('handles missing fields', () => {
|
||||
// Only field 1
|
||||
const out: number[] = [];
|
||||
encodeKey(1, WireType.LengthDelimited, out);
|
||||
const str = new TextEncoder().encode('Bob');
|
||||
encodeVarint(str.length, out);
|
||||
out.push(...str);
|
||||
|
||||
const result = protoDecode(new Uint8Array(out), SimpleSchema);
|
||||
expect(result).not.toBeNull();
|
||||
expect(result!.name).toBe('Bob');
|
||||
expect(result!.age).toBeUndefined();
|
||||
expect(result!.active).toBeUndefined();
|
||||
});
|
||||
|
||||
it('returns null for empty data', () => {
|
||||
expect(protoDecode(new Uint8Array(0), SimpleSchema)).toBeNull();
|
||||
});
|
||||
|
||||
it('decodes nested messages', () => {
|
||||
const InnerSchema = {
|
||||
value: { field: 1, type: 'uint32' as const },
|
||||
};
|
||||
const OuterSchema = {
|
||||
inner: { field: 1, type: 'message' as const, schema: InnerSchema },
|
||||
};
|
||||
|
||||
// Build inner
|
||||
const innerOut: number[] = [];
|
||||
encodeKey(1, WireType.Varint, innerOut);
|
||||
encodeVarint(99, innerOut);
|
||||
|
||||
// Build outer
|
||||
const outerOut: number[] = [];
|
||||
encodeKey(1, WireType.LengthDelimited, outerOut);
|
||||
encodeVarint(innerOut.length, outerOut);
|
||||
outerOut.push(...innerOut);
|
||||
|
||||
const result = protoDecode(new Uint8Array(outerOut), OuterSchema);
|
||||
expect(result).not.toBeNull();
|
||||
expect(result!.inner).not.toBeUndefined();
|
||||
expect(result!.inner!.value).toBe(99);
|
||||
});
|
||||
|
||||
it('decodes repeated messages', () => {
|
||||
const ItemSchema = {
|
||||
id: { field: 1, type: 'uint32' as const },
|
||||
};
|
||||
const ListSchema = {
|
||||
items: { field: 1, type: 'repeated_message' as const, schema: ItemSchema },
|
||||
};
|
||||
|
||||
const out: number[] = [];
|
||||
for (const id of [1, 2, 3]) {
|
||||
const itemOut: number[] = [];
|
||||
encodeKey(1, WireType.Varint, itemOut);
|
||||
encodeVarint(id, itemOut);
|
||||
|
||||
encodeKey(1, WireType.LengthDelimited, out);
|
||||
encodeVarint(itemOut.length, out);
|
||||
out.push(...itemOut);
|
||||
}
|
||||
|
||||
const result = protoDecode(new Uint8Array(out), ListSchema);
|
||||
expect(result).not.toBeNull();
|
||||
expect(result!.items).toHaveLength(3);
|
||||
expect(result!.items.map((i) => i.id)).toEqual([1, 2, 3]);
|
||||
});
|
||||
|
||||
it('accepts WireMessage directly', () => {
|
||||
const wire = WireMessage.decode(buildProto());
|
||||
const result = protoDecode(wire!, SimpleSchema);
|
||||
expect(result!.name).toBe('Alice');
|
||||
});
|
||||
});
|
||||
|
||||
describe('protoEncode', () => {
|
||||
const SimpleSchema = {
|
||||
name: { field: 1, type: 'string' as const },
|
||||
age: { field: 2, type: 'uint32' as const },
|
||||
active: { field: 3, type: 'bool' as const },
|
||||
};
|
||||
|
||||
it('encodes and decodes roundtrip', () => {
|
||||
const data = { name: 'Charlie', age: 25, active: true };
|
||||
const encoded = protoEncode(data, SimpleSchema);
|
||||
const decoded = protoDecode(encoded, SimpleSchema);
|
||||
expect(decoded).not.toBeNull();
|
||||
expect(decoded!.name).toBe('Charlie');
|
||||
expect(decoded!.age).toBe(25);
|
||||
expect(decoded!.active).toBe(true);
|
||||
});
|
||||
|
||||
it('skips undefined fields', () => {
|
||||
const data = { name: 'Dave', age: undefined, active: undefined };
|
||||
const encoded = protoEncode(data as any, SimpleSchema);
|
||||
const decoded = protoDecode(encoded, SimpleSchema);
|
||||
expect(decoded!.name).toBe('Dave');
|
||||
expect(decoded!.age).toBeUndefined();
|
||||
expect(decoded!.active).toBeUndefined();
|
||||
});
|
||||
|
||||
it('encodes bytes', () => {
|
||||
const ByteSchema = {
|
||||
payload: { field: 1, type: 'bytes' as const },
|
||||
};
|
||||
const data = { payload: new Uint8Array([0xDE, 0xAD]) };
|
||||
const encoded = protoEncode(data, ByteSchema);
|
||||
const decoded = protoDecode(encoded, ByteSchema);
|
||||
expect(decoded!.payload).toEqual(new Uint8Array([0xDE, 0xAD]));
|
||||
});
|
||||
|
||||
it('encodes nested messages', () => {
|
||||
const InnerSchema = {
|
||||
value: { field: 1, type: 'uint32' as const },
|
||||
};
|
||||
const OuterSchema = {
|
||||
inner: { field: 1, type: 'message' as const, schema: InnerSchema },
|
||||
};
|
||||
|
||||
const data = { inner: { value: 42 } };
|
||||
const encoded = protoEncode(data, OuterSchema);
|
||||
const decoded = protoDecode(encoded, OuterSchema);
|
||||
expect(decoded!.inner!.value).toBe(42);
|
||||
});
|
||||
|
||||
it('encodes int64/uint64', () => {
|
||||
const BigSchema = {
|
||||
signed: { field: 1, type: 'int64' as const },
|
||||
unsigned: { field: 2, type: 'uint64' as const },
|
||||
};
|
||||
const data = { signed: -1n, unsigned: 0xFFFFFFFFFFFFFFFFn };
|
||||
const encoded = protoEncode(data, BigSchema);
|
||||
const decoded = protoDecode(encoded, BigSchema);
|
||||
expect(decoded!.signed).toBe(-1n);
|
||||
expect(decoded!.unsigned).toBe(0xFFFFFFFFFFFFFFFFn);
|
||||
});
|
||||
});
|
||||
@@ -2,7 +2,7 @@ import fs from 'fs';
|
||||
import os from 'os';
|
||||
import path from 'path';
|
||||
import { pathToFileURL } from 'url';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { loadBinarySource, resolveLocalFilePath } from '../src/bridge/highway/utils';
|
||||
|
||||
describe('highway source paths', () => {
|
||||
|
||||
@@ -5,7 +5,6 @@ import { MediaStore } from '../src/onebot/media-store';
|
||||
import { convertEvent, type ConverterContext } from '../src/onebot/event-converter';
|
||||
import type { GroupMessage, FriendMessage, MessageElement } from '../src/bridge/events';
|
||||
|
||||
const SELF_UIN = '10001';
|
||||
const SELF_ID = 10001;
|
||||
const PEER_UIN = 22222;
|
||||
const GROUP_ID = 99999;
|
||||
|
||||
@@ -236,7 +236,6 @@ describe('onebot/contact-actions / getGroupMemberList', () => {
|
||||
describe('onebot/contact-actions / getGroupMemberInfo', () => {
|
||||
it('returns the cached member when present and noCache is false', async () => {
|
||||
const member = makeMember(44, 'dave', 'D');
|
||||
const cached = makeGroup(1000, '', [member]);
|
||||
const bridge = fakeBridge({
|
||||
fetchGroupMemberList: vi.fn(async () => []),
|
||||
identity: fakeIdentity({
|
||||
|
||||
@@ -2,8 +2,6 @@ import { describe, it, expect } from 'vitest';
|
||||
import { PipeWatcher } from '../src/hook/pipe-watcher';
|
||||
import type { HookProcessBaseInfo } from '../src/hook/injector';
|
||||
|
||||
const flush = () => new Promise<void>(r => setImmediate(r));
|
||||
|
||||
function setupWatcher(initial: { processes: HookProcessBaseInfo[]; live: Set<number> }) {
|
||||
let processes = initial.processes;
|
||||
let live = initial.live;
|
||||
|
||||
@@ -1,175 +0,0 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import {
|
||||
WireType,
|
||||
WireMessage,
|
||||
decodeVarint,
|
||||
encodeVarint,
|
||||
encodeKey,
|
||||
fieldAsInt32,
|
||||
fieldAsUint32,
|
||||
fieldAsString,
|
||||
fieldAsBool,
|
||||
} from '../src/protobuf/wire';
|
||||
|
||||
// Helper to build bytes from number array
|
||||
function bytes(...vals: number[]): Uint8Array {
|
||||
return new Uint8Array(vals);
|
||||
}
|
||||
|
||||
describe('decodeVarint', () => {
|
||||
it('decodes single-byte varint', () => {
|
||||
const result = decodeVarint(bytes(0x05), 0);
|
||||
expect(result).toEqual([5n, 1]);
|
||||
});
|
||||
|
||||
it('decodes zero', () => {
|
||||
const result = decodeVarint(bytes(0x00), 0);
|
||||
expect(result).toEqual([0n, 1]);
|
||||
});
|
||||
|
||||
it('decodes multi-byte varint (300)', () => {
|
||||
// 300 = 0b100101100 => 0xAC 0x02
|
||||
const result = decodeVarint(bytes(0xac, 0x02), 0);
|
||||
expect(result).toEqual([300n, 2]);
|
||||
});
|
||||
|
||||
it('decodes with offset', () => {
|
||||
const result = decodeVarint(bytes(0xff, 0x01), 1);
|
||||
expect(result).toEqual([1n, 1]);
|
||||
});
|
||||
|
||||
it('returns null on empty data', () => {
|
||||
const result = decodeVarint(bytes(), 0);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('encodeVarint', () => {
|
||||
it('encodes small number', () => {
|
||||
const out: number[] = [];
|
||||
encodeVarint(5, out);
|
||||
expect(out).toEqual([5]);
|
||||
});
|
||||
|
||||
it('encodes 300', () => {
|
||||
const out: number[] = [];
|
||||
encodeVarint(300, out);
|
||||
expect(out).toEqual([0xac, 0x02]);
|
||||
});
|
||||
|
||||
it('encodes bigint', () => {
|
||||
const out: number[] = [];
|
||||
encodeVarint(300n, out);
|
||||
expect(out).toEqual([0xac, 0x02]);
|
||||
});
|
||||
|
||||
it('roundtrips', () => {
|
||||
for (const val of [0, 1, 127, 128, 300, 65535, 2147483647]) {
|
||||
const out: number[] = [];
|
||||
encodeVarint(val, out);
|
||||
const [decoded] = decodeVarint(new Uint8Array(out), 0)!;
|
||||
expect(Number(decoded)).toBe(val);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('encodeKey', () => {
|
||||
it('encodes field 1 varint', () => {
|
||||
const out: number[] = [];
|
||||
encodeKey(1, WireType.Varint, out);
|
||||
expect(out).toEqual([0x08]); // (1 << 3) | 0 = 8
|
||||
});
|
||||
|
||||
it('encodes field 2 length-delimited', () => {
|
||||
const out: number[] = [];
|
||||
encodeKey(2, WireType.LengthDelimited, out);
|
||||
expect(out).toEqual([0x12]); // (2 << 3) | 2 = 18
|
||||
});
|
||||
});
|
||||
|
||||
describe('WireMessage', () => {
|
||||
// Build a simple protobuf: field 1 = varint 150, field 2 = string "testing"
|
||||
function buildSimpleMessage(): Uint8Array {
|
||||
const out: number[] = [];
|
||||
// field 1, varint = 150
|
||||
encodeKey(1, WireType.Varint, out);
|
||||
encodeVarint(150, out);
|
||||
// field 2, string = "testing"
|
||||
encodeKey(2, WireType.LengthDelimited, out);
|
||||
const str = new TextEncoder().encode('testing');
|
||||
encodeVarint(str.length, out);
|
||||
out.push(...str);
|
||||
return new Uint8Array(out);
|
||||
}
|
||||
|
||||
it('decodes empty data returns null', () => {
|
||||
expect(WireMessage.decode(new Uint8Array(0))).toBeNull();
|
||||
});
|
||||
|
||||
it('decodes varint field', () => {
|
||||
const msg = WireMessage.decode(buildSimpleMessage());
|
||||
expect(msg).not.toBeNull();
|
||||
expect(msg!.getUint32(1)).toBe(150);
|
||||
});
|
||||
|
||||
it('decodes string field', () => {
|
||||
const msg = WireMessage.decode(buildSimpleMessage());
|
||||
expect(msg).not.toBeNull();
|
||||
expect(msg!.getString(2)).toBe('testing');
|
||||
});
|
||||
|
||||
it('returns null for missing fields', () => {
|
||||
const msg = WireMessage.decode(buildSimpleMessage());
|
||||
expect(msg!.getUint32(99)).toBeNull();
|
||||
expect(msg!.getString(99)).toBeNull();
|
||||
});
|
||||
|
||||
it('has() works', () => {
|
||||
const msg = WireMessage.decode(buildSimpleMessage());
|
||||
expect(msg!.has(1)).toBe(true);
|
||||
expect(msg!.has(2)).toBe(true);
|
||||
expect(msg!.has(3)).toBe(false);
|
||||
});
|
||||
|
||||
it('decodes nested message', () => {
|
||||
const inner: number[] = [];
|
||||
encodeKey(1, WireType.Varint, inner);
|
||||
encodeVarint(42, inner);
|
||||
|
||||
const outer: number[] = [];
|
||||
encodeKey(3, WireType.LengthDelimited, outer);
|
||||
encodeVarint(inner.length, outer);
|
||||
outer.push(...inner);
|
||||
|
||||
const msg = WireMessage.decode(new Uint8Array(outer));
|
||||
const nested = msg!.getMessage(3);
|
||||
expect(nested).not.toBeNull();
|
||||
expect(nested!.getUint32(1)).toBe(42);
|
||||
});
|
||||
|
||||
it('handles repeated fields', () => {
|
||||
const out: number[] = [];
|
||||
for (const v of [10, 20, 30]) {
|
||||
encodeKey(1, WireType.Varint, out);
|
||||
encodeVarint(v, out);
|
||||
}
|
||||
const msg = WireMessage.decode(new Uint8Array(out));
|
||||
const vals = msg!.getRepeatedVarint(1);
|
||||
expect(vals.map(Number)).toEqual([10, 20, 30]);
|
||||
});
|
||||
|
||||
it('field helpers work correctly', () => {
|
||||
const out: number[] = [];
|
||||
encodeKey(1, WireType.Varint, out);
|
||||
encodeVarint(1, out); // bool true
|
||||
encodeKey(2, WireType.Varint, out);
|
||||
encodeVarint(0xFFFFFFFF, out); // uint32 max
|
||||
|
||||
const msg = WireMessage.decode(new Uint8Array(out))!;
|
||||
const f1 = msg.field(1)!;
|
||||
const f2 = msg.field(2)!;
|
||||
|
||||
expect(fieldAsBool(f1)).toBe(true);
|
||||
expect(fieldAsUint32(f2)).toBe(0xFFFFFFFF);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user