Files
DrSmoothl 194165f49d feat(proton): add @snowluma/proton — compile-time protobuf DSL
Vendors SnowLuma/protobuf-dsl into the monorepo as @snowluma/proton, with
edge-case fixes applied on top of the upstream source. Reference copy of
the unmodified upstream is kept under dev/protobuf-dsl/.

Edge cases fixed (each with a regression test):
  - Aliased pb / pb_repeated marker imports (`import { pb as P }`)
    — collector now resolves the marker name through the import resolver
  - Concrete interface with a generic-instantiated message field
    (`wrapped: pb<5, Wrapper<uint_32>>`) — field typeName now resolves
    to the mangled form, and the inner instantiation is queued for
    monomorphization
  - Generic template with a generic-instantiated field
    (`interface Outer<U> { x: pb<5, Wrapper<U>> }`) — the inner
    instantiation is now re-substituted and re-monomorphized at outer
    instantiation time
  - Wrapper-binding synthetic SourceFile was empty-text — getText()
    returned garbage, breaking mangled name computation downstream
  - Cross-file forwarded wrappers (chain → imported base) were not
    detected — parseFileForDefinitions now resolves imports recursively
    (value imports only, with cycle protection)
  - matchForwardedKnownWrapper did not propagate typePattern — chain
    treated <X> as the encoded type instead of Wrapper<X>
  - Synthetic-SF WeakMap was private to analyzer.ts — replacer could
    not see it. Moved to utils.ts so typeNodeToMangledName picks it up
    automatically.

Defensive guard: any field whose typeName resolves to neither a primitive
nor a known message now throws at analyze() time, instead of silently
leaking the WireType.Varint placeholder into codegen.

95/95 tests pass; tsc --noEmit clean.
2026-05-19 08:19:13 +08:00

111 lines
3.2 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { encodeVarint, decodeVarint, writeTag, writeString, readString, skipField } from '../../src/codegen/wire';
describe('wire format primitives', () => {
describe('varint', () => {
const cases: [number, number[]][] = [
[0, [0x00]],
[1, [0x01]],
[127, [0x7f]],
[128, [0x80, 0x01]],
[300, [0xac, 0x02]],
[16384, [0x80, 0x80, 0x01]],
];
it.each(cases)('encodeVarint(%i) produces correct bytes', (value, expected) => {
const buf: number[] = [];
encodeVarint(value, buf);
expect(buf).toEqual(expected);
});
it.each(cases)('decodeVarint round-trip for %i', (value) => {
const buf: number[] = [];
encodeVarint(value, buf);
const data = new Uint8Array(buf);
const [decoded, offset] = decodeVarint(data, 0);
expect(decoded).toBe(value);
expect(offset).toBe(buf.length);
});
it('handles max uint32', () => {
const value = 0xFFFFFFFF;
const buf: number[] = [];
encodeVarint(value, buf);
const [decoded] = decodeVarint(new Uint8Array(buf), 0);
expect(decoded).toBe(value);
});
});
describe('tag encoding', () => {
it('field 1, varint (wire type 0) produces 0x08', () => {
const buf: number[] = [];
writeTag(1, 0, buf);
expect(buf).toEqual([0x08]);
});
it('field 1, length-delimited (wire type 2) produces 0x0A', () => {
const buf: number[] = [];
writeTag(1, 2, buf);
expect(buf).toEqual([0x0a]);
});
it('field 2, varint produces 0x10', () => {
const buf: number[] = [];
writeTag(2, 0, buf);
expect(buf).toEqual([0x10]);
});
});
describe('string encoding', () => {
it('encodes and decodes "hello"', () => {
const buf: number[] = [];
writeString('hello', buf);
const data = new Uint8Array(buf);
// First byte is length (5), followed by UTF-8 bytes
expect(data[0]).toBe(5);
const decoded = readString(data, 1, 5);
expect(decoded).toBe('hello');
});
it('handles empty string', () => {
const buf: number[] = [];
writeString('', buf);
expect(buf).toEqual([0]); // length 0
});
it('handles unicode', () => {
const buf: number[] = [];
writeString('你好', buf);
const data = new Uint8Array(buf);
const [len, offset] = [data[0], 1];
const decoded = readString(data, offset, len);
expect(decoded).toBe('你好');
});
});
describe('skipField', () => {
it('skips varint field', () => {
const buf: number[] = [];
encodeVarint(300, buf); // multi-byte varint
const data = new Uint8Array(buf);
const newOffset = skipField(data, 0, 0);
expect(newOffset).toBe(buf.length);
});
it('skips length-delimited field', () => {
// length=3 followed by 3 bytes
const data = new Uint8Array([3, 0x41, 0x42, 0x43]);
const newOffset = skipField(data, 0, 2);
expect(newOffset).toBe(4);
});
it('skips 32-bit field', () => {
expect(skipField(new Uint8Array(4), 0, 5)).toBe(4);
});
it('skips 64-bit field', () => {
expect(skipField(new Uint8Array(8), 0, 1)).toBe(8);
});
});
});