Migrate project to pnpm monorepo

Restructure the repository into a pnpm workspace under packages/ (core, webui, runtime). Move source files for core and webui into packages/*, add package manifests for @snowluma/core and @snowluma/runtime, and remove the old launcher package.json. Update root package.json to be a monorepo controller (scripts delegate to pnpm filters, set packageManager and Node engine). Configure build outputs to emit into the repo-root dist/ (vite config changes) so the existing release pipeline continues to work. Add .npmrc with mirrors, expand .gitignore for monorepo layout, add tsconfig base and package-level tsconfigs, and add vitest config for the core package. Adjust injector native search paths and update tests import paths to the new package layout. Small CI/workflow comment added to clarify build behavior and README updated with monorepo structure and common commands.
This commit is contained in:
手瓜一十雪 committed 2026-05-03 14:02:29 +08:00
1 parent 6bba36bba6
commit 39f1672b29
122 files changed
+279 -85

No files matched your search

+16
View File
@@ -0,0 +1,16 @@
import { describe, it, expect } from 'vitest';
import { loadOneBotConfig, makeDefaultOneBotConfig } from '../src/onebot/config';
describe('makeDefaultOneBotConfig', () => {
it('returns default config structure', () => {
const config = makeDefaultOneBotConfig();
expect(config.httpServers).toHaveLength(1);
expect(config.httpServers[0].host).toBe('0.0.0.0');
expect(config.httpServers[0].port).toBe(3000);
expect(config.httpPostEndpoints).toEqual([]);
expect(config.wsServers).toHaveLength(1);
expect(config.wsServers[0].port).toBe(3001);
expect(config.wsClients).toEqual([]);
expect(config.musicSignUrl).toBe('');
});
});
+173
View File
@@ -0,0 +1,173 @@
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);
});
});
+65
View File
@@ -0,0 +1,65 @@
import { describe, it, expect } from 'vitest';
import { toHex, fromHex, hexPreview } from '../src/utils/hex';
describe('toHex', () => {
it('converts empty buffer', () => {
expect(toHex(new Uint8Array(0))).toBe('');
});
it('converts single byte', () => {
expect(toHex(new Uint8Array([0xff]))).toBe('ff');
expect(toHex(new Uint8Array([0x00]))).toBe('00');
expect(toHex(new Uint8Array([0x0a]))).toBe('0a');
});
it('converts multiple bytes', () => {
expect(toHex(new Uint8Array([0xde, 0xad, 0xbe, 0xef]))).toBe('deadbeef');
});
it('works with Buffer', () => {
expect(toHex(Buffer.from([0x01, 0x23, 0x45]))).toBe('012345');
});
});
describe('fromHex', () => {
it('converts empty string', () => {
expect(fromHex('')).toEqual(Buffer.alloc(0));
});
it('converts hex string', () => {
const buf = fromHex('deadbeef');
expect(buf).toEqual(Buffer.from([0xde, 0xad, 0xbe, 0xef]));
});
it('handles uppercase', () => {
const buf = fromHex('DEADBEEF');
expect(buf).toEqual(Buffer.from([0xde, 0xad, 0xbe, 0xef]));
});
it('throws on odd-length string', () => {
expect(() => fromHex('abc')).toThrow('Invalid hex string length');
});
it('roundtrips with toHex', () => {
const original = Buffer.from([0x00, 0x7f, 0x80, 0xff]);
expect(fromHex(toHex(original))).toEqual(original);
});
});
describe('hexPreview', () => {
it('shows full hex for small data', () => {
const data = new Uint8Array([0xab, 0xcd]);
expect(hexPreview(data)).toBe('abcd');
});
it('truncates with ellipsis', () => {
const data = new Uint8Array(128).fill(0xaa);
const preview = hexPreview(data, 4);
expect(preview).toBe('aaaaaaaa...');
});
it('no ellipsis when exactly at limit', () => {
const data = new Uint8Array(4).fill(0xbb);
expect(hexPreview(data, 4)).toBe('bbbbbbbb');
});
});
+49
View File
@@ -0,0 +1,49 @@
import { describe, it, expect } from 'vitest';
import { hashMessageIdInt32, GROUP_MESSAGE_EVENT, PRIVATE_MESSAGE_EVENT } from '../src/onebot/message-id';
describe('hashMessageIdInt32', () => {
it('returns consistent hash for same inputs', () => {
const id1 = hashMessageIdInt32(100, 12345, GROUP_MESSAGE_EVENT);
const id2 = hashMessageIdInt32(100, 12345, GROUP_MESSAGE_EVENT);
expect(id1).toBe(id2);
});
it('returns non-zero', () => {
// The function ensures id !== 0 by setting it to 1
const id = hashMessageIdInt32(0, 0, '');
expect(id).not.toBe(0);
});
it('returns different values for different sequences', () => {
const id1 = hashMessageIdInt32(1, 12345, GROUP_MESSAGE_EVENT);
const id2 = hashMessageIdInt32(2, 12345, GROUP_MESSAGE_EVENT);
expect(id1).not.toBe(id2);
});
it('returns different values for different sessions', () => {
const id1 = hashMessageIdInt32(100, 11111, GROUP_MESSAGE_EVENT);
const id2 = hashMessageIdInt32(100, 22222, GROUP_MESSAGE_EVENT);
expect(id1).not.toBe(id2);
});
it('returns different values for different event names', () => {
const id1 = hashMessageIdInt32(100, 12345, GROUP_MESSAGE_EVENT);
const id2 = hashMessageIdInt32(100, 12345, PRIVATE_MESSAGE_EVENT);
expect(id1).not.toBe(id2);
});
it('returns 32-bit integer', () => {
const id = hashMessageIdInt32(999999, 123456789, GROUP_MESSAGE_EVENT);
expect(id).toBeGreaterThanOrEqual(-2147483648);
expect(id).toBeLessThanOrEqual(2147483647);
});
it('handles non-finite inputs gracefully', () => {
const id1 = hashMessageIdInt32(NaN, 0, 'test');
const id2 = hashMessageIdInt32(Infinity, 0, 'test');
expect(typeof id1).toBe('number');
expect(typeof id2).toBe('number');
// Both should map to sequence=0
expect(id1).toBe(id2);
});
});
+248
View File
@@ -0,0 +1,248 @@
import { describe, it, expect } from 'vitest';
import { parseMessage } from '../src/onebot/message-parser';
describe('parseMessage', () => {
describe('plain text', () => {
it('parses simple text', async () => {
const result = await parseMessage('hello world', false);
expect(result).toEqual([{ type: 'text', text: 'hello world' }]);
});
it('returns empty for empty string', async () => {
const result = await parseMessage('', false);
expect(result).toEqual([]);
});
it('autoEscape treats CQ codes as text', async () => {
const result = await parseMessage('[CQ:face,id=123]', true);
expect(result).toEqual([{ type: 'text', text: '[CQ:face,id=123]' }]);
});
});
describe('CQ code parsing', () => {
it('parses face CQ code', async () => {
const result = await parseMessage('[CQ:face,id=123]', false);
expect(result).toHaveLength(1);
expect(result[0].type).toBe('face');
expect(result[0].faceId).toBe(123);
});
it('parses at CQ code', async () => {
const result = await parseMessage('[CQ:at,qq=12345]', false);
expect(result).toHaveLength(1);
expect(result[0].type).toBe('at');
expect(result[0].targetUin).toBe(12345);
});
it('parses at all', async () => {
const result = await parseMessage('[CQ:at,qq=all]', false);
expect(result).toHaveLength(1);
expect(result[0].type).toBe('at');
expect(result[0].targetUin).toBe(0);
});
it('parses mixed text and CQ codes', async () => {
const result = await parseMessage('Hello [CQ:face,id=1] World', false);
expect(result).toHaveLength(3);
expect(result[0]).toEqual({ type: 'text', text: 'Hello ' });
expect(result[1].type).toBe('face');
expect(result[2]).toEqual({ type: 'text', text: ' World' });
});
it('unescapes CQ special chars', async () => {
const result = await parseMessage('a&b[c]d', false);
expect(result).toEqual([{ type: 'text', text: 'a&b[c]d' }]);
});
});
describe('JSON segment array', () => {
it('parses text segment', async () => {
const result = await parseMessage(
[{ type: 'text', data: { text: 'hello' } }] as any,
false,
);
expect(result).toEqual([{ type: 'text', text: 'hello' }]);
});
it('parses face segment', async () => {
const result = await parseMessage(
[{ type: 'face', data: { id: 123 } }] as any,
false,
);
expect(result).toHaveLength(1);
expect(result[0].type).toBe('face');
expect(result[0].faceId).toBe(123);
});
it('parses image segment', async () => {
const result = await parseMessage(
[{ type: 'image', data: { file: 'https://example.com/img.png' } }] as any,
false,
);
expect(result).toHaveLength(1);
expect(result[0].type).toBe('image');
expect(result[0].url).toBe('https://example.com/img.png');
});
it('parses at segment', async () => {
const result = await parseMessage(
[{ type: 'at', data: { qq: 12345 } }] as any,
false,
);
expect(result).toHaveLength(1);
expect(result[0].type).toBe('at');
expect(result[0].targetUin).toBe(12345);
});
it('parses multiple segments', async () => {
const result = await parseMessage(
[
{ type: 'text', data: { text: 'hi ' } },
{ type: 'at', data: { qq: 999 } },
{ type: 'text', data: { text: ' there' } },
] as any,
false,
);
expect(result).toHaveLength(3);
expect(result[0].type).toBe('text');
expect(result[1].type).toBe('at');
expect(result[2].type).toBe('text');
});
it('skips unknown segment types', async () => {
const result = await parseMessage(
[
{ type: 'text', data: { text: 'ok' } },
{ type: 'unknown_type_xyz', data: {} },
] as any,
false,
);
expect(result).toHaveLength(1);
expect(result[0].type).toBe('text');
});
});
describe('special segments', () => {
it('parses json segment', async () => {
const result = await parseMessage(
[{ type: 'json', data: { data: '{"app":"test"}' } }] as any,
false,
);
expect(result).toHaveLength(1);
expect(result[0].type).toBe('json');
expect(result[0].text).toBe('{"app":"test"}');
});
it('parses reply segment with resolveReplySequence', async () => {
const result = await parseMessage(
[{ type: 'reply', data: { id: 42 } }] as any,
false,
{ resolveReplySequence: (id) => id === 42 ? 100 : null },
);
expect(result).toHaveLength(1);
expect(result[0].type).toBe('reply');
expect(result[0].replySeq).toBe(100);
});
it('parses share as json card', async () => {
const result = await parseMessage(
[{ type: 'share', data: { url: 'https://example.com', title: 'Test' } }] as any,
false,
);
expect(result).toHaveLength(1);
expect(result[0].type).toBe('json');
expect(result[0].text).toBeDefined();
const parsed = JSON.parse(result[0].text!);
expect(parsed.app).toBe('com.tencent.structmsg');
expect(parsed.meta.news.title).toBe('Test');
});
it('parses rps as face', async () => {
const result = await parseMessage(
[{ type: 'rps', data: {} }] as any,
false,
);
expect(result).toHaveLength(1);
expect(result[0].type).toBe('face');
expect(result[0].faceId).toBe(359);
});
it('parses dice as face', async () => {
const result = await parseMessage(
[{ type: 'dice', data: {} }] as any,
false,
);
expect(result).toHaveLength(1);
expect(result[0].type).toBe('face');
expect(result[0].faceId).toBe(358);
});
it('parses shake as poke', async () => {
const result = await parseMessage(
[{ type: 'shake', data: {} }] as any,
false,
);
expect(result).toHaveLength(1);
expect(result[0].type).toBe('poke');
});
it('ignores anonymous segment', async () => {
const result = await parseMessage(
[
{ type: 'anonymous', data: {} },
{ type: 'text', data: { text: 'hi' } },
] as any,
false,
);
expect(result).toHaveLength(1);
expect(result[0].type).toBe('text');
});
it('parses location as json card', async () => {
const result = await parseMessage(
[{ type: 'location', data: { lat: '39.9', lon: '116.3', title: 'Beijing' } }] as any,
false,
);
expect(result).toHaveLength(1);
expect(result[0].type).toBe('json');
expect(result[0].text).toBeDefined();
const parsed = JSON.parse(result[0].text!);
expect(parsed.app).toBe('com.tencent.map');
expect(parsed.meta.Location.lat).toBe('39.9');
});
it('parses contact as json card', async () => {
const result = await parseMessage(
[{ type: 'contact', data: { type: 'group', id: '12345' } }] as any,
false,
);
expect(result).toHaveLength(1);
expect(result[0].type).toBe('json');
expect(result[0].text).toBeDefined();
const parsed = JSON.parse(result[0].text!);
expect(parsed.meta.contact.type).toBe('group');
});
});
describe('single segment object', () => {
it('parses single segment object', async () => {
const result = await parseMessage(
{ type: 'text', data: { text: 'single' } } as any,
false,
);
expect(result).toEqual([{ type: 'text', text: 'single' }]);
});
});
describe('edge cases', () => {
it('returns empty for null', async () => {
const result = await parseMessage(null as any, false);
expect(result).toEqual([]);
});
it('returns empty for number', async () => {
const result = await parseMessage(42 as any, false);
expect(result).toEqual([]);
});
});
});
+175
View File
@@ -0,0 +1,175 @@
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);
});
});