feat(proton): finish OIDB migration — runOidb on protobuf_encode<OidbBase<T>>
Closes the OIDB phase of the proton migration. Every consumer that
talked to the OIDB service through the old runtime
`makeOidbBaseSchema()` factory now goes through the compile-time
generic `OidbBase<T>` instead — proton monomorphizes the codec per
call-site instantiation.
@snowluma/proton — two analyzer fixes uncovered by the migration:
1. Skip queuing call-site type-args that reference an *enclosing
generic function's* type parameter for monomorphization. Before
this, a wrapper like
export function encodeOidbEnv<T>(env: OidbBase<T>): Uint8Array {
return protobuf_encode<OidbBase<T>>(env);
}
would cause the analyzer to mint a literal `OidbBase__T` registry
entry with `body.typeName = 'T'`, which the post-walk wire-type
guard then tripped on. The enclosing-scope type params are tracked
on a stack as the walker descends into FunctionDeclaration,
FunctionExpression, ArrowFunction, MethodDeclaration nodes; matching
call sites are skipped (the wrapper-binding path is still
responsible for materialising concrete codecs at downstream call
sites).
2. Register *explicitly* empty interfaces (`interface X {}`) as
zero-field proto messages. OIDB has a handful of these
('this call has no meaningful response body' placeholders —
Oidb0xeb7Resp, Oidb0xcd4Resp, Oidb0x112aResp, Oidb0x8a0Resp,
Oidb0xf16Resp, OidbClientKeyReq). Proto3 wire allows zero-field
messages, and downstream code does `protobuf_decode<OidbBase<X>>`
where X is one of these — the wire-type guard would otherwise trip
on the missing `X` registration. Interfaces that have members but
no pb-marked fields are still NOT registered.
@snowluma/core — the OIDB-layer refactor itself:
bridge-oidb.ts ditches the runtime ProtoSchema factory and exposes:
- makeOidbEnvelope<T>(oidbCmd, subCmd, body, isUid) pure helper
- encodeOidbEnv<T>(env) pass-through
- decodeOidbEnv<T>(bytes) pass-through
- runOidb(bridge, cmd, envelopeBytes) non-generic raw
`encodeOidbEnv` / `decodeOidbEnv` are pure pass-through generic
wrappers — proton wrapper-binds them and substitutes every call site
with the inlined `protobuf_encode_OidbBase__X` / `protobuf_decode_…`
function. `runOidb` is non-generic (proton leaves it alone) so it can
be mocked in tests. `makeOidbEnvelope` is plain TS — also mockable for
introspection.
56 `runOidb` call sites across 11 files migrated to the new API:
- bridge-contacts.ts, actions/extras.ts, actions/friend.ts,
actions/group-admin.ts, actions/group-file.ts,
actions/group-message.ts, actions/interaction.ts,
actions/misc.ts, actions/profile.ts, web-actions/cookies.ts,
highway/pipeline.ts
pipeline.ts also drops `makeOidbBaseSchema(NTV2UploadRichMediaReqSchema)`
and the residual `protoEncode({...}, EncodableMediaMsgInfoSchema)` —
both go through proton now.
Tests:
- 8 test files migrated from `vi.mocked(decodeOidbEnv).mockReturnValueOnce`
to `vi.mocked(runOidb).mockResolvedValueOnce(protobuf_encode<OidbBase<X>>({body: …}))`
— the proton substitution means the old mock layer is bypassed at
build time, so the only mockable point is the bytes layer at `runOidb`.
- 2 new parity tests pinning `OidbBase<T>` ↔ legacy
`makeOidbBaseSchema(InnerSchema)` byte equivalence.
Verification:
- @snowluma/proton: 95/95 tests pass, tsc --noEmit clean
- @snowluma/core: 605/605 tests pass (network-manager.test.ts
fails to load due to a missing darwin-arm64 native binary in this
environment — pre-existing), tsc --noEmit clean
This commit is contained in:
25 files changed
+1144
-977
No files matched your search
@@ -1,10 +1,26 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { protobuf_encode } from '@snowluma/proton';
|
||||
import type { OidbBase } from '../../src/bridge/proto/proton/oidb';
|
||||
import type {
|
||||
OidbClientKeyResp,
|
||||
OidbGetPskeyResp,
|
||||
} from '../../src/bridge/proto/proton/oidb-action';
|
||||
|
||||
// Mock the OIDB shim so we can control what `forceFetchClientKey` and
|
||||
// `getPSkey` see without standing up a Bridge.
|
||||
vi.mock('../../src/bridge/bridge-oidb', () => ({
|
||||
runOidb: vi.fn(async () => ({})),
|
||||
}));
|
||||
// `encodeOidbEnv` / `decodeOidbEnv` are proton-bound pass-through wrappers
|
||||
// — the plugin substitutes them at the call site with the inlined codec,
|
||||
// so mocking them on the module object is a no-op. We mock `runOidb`
|
||||
// (non-generic, untouched by proton) to return real proton-encoded bytes
|
||||
// that the production-side codec then decodes.
|
||||
vi.mock('../../src/bridge/bridge-oidb', async () => {
|
||||
const actual = await vi.importActual<typeof import('../../src/bridge/bridge-oidb')>(
|
||||
'../../src/bridge/bridge-oidb',
|
||||
);
|
||||
return {
|
||||
...actual,
|
||||
runOidb: vi.fn(async () => new Uint8Array()),
|
||||
makeOidbEnvelope: vi.fn((_oidbCmd, _subCmd, body) => ({ body })),
|
||||
};
|
||||
});
|
||||
|
||||
// Mock the actual HTTP cookie fetch.
|
||||
vi.mock('../../src/bridge/web/request-util', () => ({
|
||||
@@ -28,35 +44,54 @@ import {
|
||||
|
||||
const bridge = { identity: { uin: '10001' } } as any;
|
||||
|
||||
function resetOidbMocks() {
|
||||
vi.mocked(oidb.runOidb).mockReset();
|
||||
vi.mocked(oidb.runOidb).mockResolvedValue(new Uint8Array());
|
||||
vi.mocked(oidb.makeOidbEnvelope).mockClear();
|
||||
}
|
||||
|
||||
// Bake the encoded OidbBase<X> bytes a test wants `runOidb` to return.
|
||||
// Keeping these inline as `protobuf_encode<OidbBase<...>>({body: ...})`
|
||||
// — proton substitutes them with the inlined codec at this file's
|
||||
// transform time, just like for production code.
|
||||
|
||||
describe('cookies — forceFetchClientKey', () => {
|
||||
beforeEach(() => { vi.mocked(oidb.runOidb).mockReset(); });
|
||||
beforeEach(() => { resetOidbMocks(); });
|
||||
|
||||
it('returns clientKey + keyIndex + expireTime from the OIDB response', async () => {
|
||||
vi.mocked(oidb.runOidb).mockResolvedValueOnce({
|
||||
clientKey: 'ck-xyz', keyIndex: 42, expireTime: 3600,
|
||||
});
|
||||
vi.mocked(oidb.runOidb).mockResolvedValueOnce(
|
||||
protobuf_encode<OidbBase<OidbClientKeyResp>>({
|
||||
body: { clientKey: 'ck-xyz', keyIndex: 42, expireTime: 3600 } as any,
|
||||
}),
|
||||
);
|
||||
const out = await forceFetchClientKey(bridge);
|
||||
expect(out).toEqual({ clientKey: 'ck-xyz', keyIndex: '42', expireTime: '3600' });
|
||||
});
|
||||
|
||||
it('falls back to default keyIndex/expireTime when the response omits them', async () => {
|
||||
vi.mocked(oidb.runOidb).mockResolvedValueOnce({ clientKey: 'ck' });
|
||||
vi.mocked(oidb.runOidb).mockResolvedValueOnce(
|
||||
protobuf_encode<OidbBase<OidbClientKeyResp>>({ body: { clientKey: 'ck' } as any }),
|
||||
);
|
||||
const out = await forceFetchClientKey(bridge);
|
||||
expect(out).toEqual({ clientKey: 'ck', keyIndex: '19', expireTime: '1800' });
|
||||
});
|
||||
});
|
||||
|
||||
describe('cookies — getPSkey', () => {
|
||||
beforeEach(() => { vi.mocked(oidb.runOidb).mockReset(); });
|
||||
beforeEach(() => { resetOidbMocks(); });
|
||||
|
||||
it('folds pskeyItems into a domain → pskey Map', async () => {
|
||||
vi.mocked(oidb.runOidb).mockResolvedValueOnce({
|
||||
pskeyItems: [
|
||||
{ domain: 'qun.qq.com', pskey: 'psk-a' },
|
||||
{ domain: 'qzone.qq.com', pskey: 'psk-b' },
|
||||
{ domain: 'corrupt', pskey: '' },
|
||||
],
|
||||
});
|
||||
vi.mocked(oidb.runOidb).mockResolvedValueOnce(
|
||||
protobuf_encode<OidbBase<OidbGetPskeyResp>>({
|
||||
body: {
|
||||
pskeyItems: [
|
||||
{ domain: 'qun.qq.com', pskey: 'psk-a' },
|
||||
{ domain: 'qzone.qq.com', pskey: 'psk-b' },
|
||||
{ domain: 'corrupt', pskey: '' },
|
||||
],
|
||||
} as any,
|
||||
}),
|
||||
);
|
||||
const { domainPskeyMap } = await getPSkey(bridge, ['qun.qq.com', 'qzone.qq.com']);
|
||||
expect(domainPskeyMap.get('qun.qq.com')).toBe('psk-a');
|
||||
expect(domainPskeyMap.get('qzone.qq.com')).toBe('psk-b');
|
||||
@@ -66,12 +101,16 @@ describe('cookies — getPSkey', () => {
|
||||
|
||||
describe('cookies — getCookies', () => {
|
||||
beforeEach(() => {
|
||||
vi.mocked(oidb.runOidb).mockReset();
|
||||
resetOidbMocks();
|
||||
vi.mocked(RequestUtil.HttpsGetCookies).mockReset();
|
||||
});
|
||||
|
||||
it('builds the ptlogin2 jump URL with the right query params', async () => {
|
||||
vi.mocked(oidb.runOidb).mockResolvedValueOnce({ clientKey: 'ck', keyIndex: 19 });
|
||||
vi.mocked(oidb.runOidb).mockResolvedValueOnce(
|
||||
protobuf_encode<OidbBase<OidbClientKeyResp>>({
|
||||
body: { clientKey: 'ck', keyIndex: 19 } as any,
|
||||
}),
|
||||
);
|
||||
vi.mocked(RequestUtil.HttpsGetCookies).mockResolvedValueOnce({
|
||||
p_skey: 'psk',
|
||||
skey: 'sk',
|
||||
@@ -89,10 +128,14 @@ describe('cookies — getCookies', () => {
|
||||
|
||||
it('falls back to getPSkey when p_skey is missing in the cookie response', async () => {
|
||||
vi.mocked(oidb.runOidb)
|
||||
.mockResolvedValueOnce({ clientKey: 'ck', keyIndex: 19 }) // forceFetchClientKey
|
||||
.mockResolvedValueOnce({ // getPSkey
|
||||
pskeyItems: [{ domain: 'qun.qq.com', pskey: 'psk-from-oidb' }],
|
||||
});
|
||||
.mockResolvedValueOnce(
|
||||
protobuf_encode<OidbBase<OidbClientKeyResp>>({ body: { clientKey: 'ck', keyIndex: 19 } as any }),
|
||||
)
|
||||
.mockResolvedValueOnce(
|
||||
protobuf_encode<OidbBase<OidbGetPskeyResp>>({
|
||||
body: { pskeyItems: [{ domain: 'qun.qq.com', pskey: 'psk-from-oidb' }] } as any,
|
||||
}),
|
||||
);
|
||||
vi.mocked(RequestUtil.HttpsGetCookies).mockResolvedValueOnce({});
|
||||
|
||||
const out = await getCookies(bridge, 'qun.qq.com');
|
||||
@@ -101,7 +144,9 @@ describe('cookies — getCookies', () => {
|
||||
|
||||
it('returns whatever cookies it got if getPSkey fallback throws', async () => {
|
||||
vi.mocked(oidb.runOidb)
|
||||
.mockResolvedValueOnce({ clientKey: 'ck', keyIndex: 19 })
|
||||
.mockResolvedValueOnce(
|
||||
protobuf_encode<OidbBase<OidbClientKeyResp>>({ body: { clientKey: 'ck', keyIndex: 19 } as any }),
|
||||
)
|
||||
.mockRejectedValueOnce(new Error('pskey unavailable'));
|
||||
vi.mocked(RequestUtil.HttpsGetCookies).mockResolvedValueOnce({ skey: 'sk' });
|
||||
|
||||
@@ -112,29 +157,38 @@ describe('cookies — getCookies', () => {
|
||||
|
||||
describe('cookies — getSKey', () => {
|
||||
beforeEach(() => {
|
||||
vi.mocked(oidb.runOidb).mockReset();
|
||||
resetOidbMocks();
|
||||
vi.mocked(RequestUtil.HttpsGetCookies).mockReset();
|
||||
});
|
||||
|
||||
it('returns skey from the qzone-targeted ptlogin2 jump', async () => {
|
||||
vi.mocked(oidb.runOidb).mockResolvedValueOnce({ clientKey: 'ck', keyIndex: 19 });
|
||||
vi.mocked(oidb.runOidb).mockResolvedValueOnce(
|
||||
protobuf_encode<OidbBase<OidbClientKeyResp>>({ body: { clientKey: 'ck', keyIndex: 19 } as any }),
|
||||
);
|
||||
vi.mocked(RequestUtil.HttpsGetCookies).mockResolvedValueOnce({ skey: 'the-skey' });
|
||||
expect(await getSKey(bridge)).toBe('the-skey');
|
||||
});
|
||||
|
||||
it('throws when clientKey comes back empty', async () => {
|
||||
vi.mocked(oidb.runOidb).mockResolvedValueOnce({ clientKey: '' });
|
||||
// Empty clientKey is the proto3 default; omit so it decodes back to ''.
|
||||
vi.mocked(oidb.runOidb).mockResolvedValueOnce(
|
||||
protobuf_encode<OidbBase<OidbClientKeyResp>>({ body: {} }),
|
||||
);
|
||||
await expect(getSKey(bridge)).rejects.toThrow(/clientKey is empty/);
|
||||
});
|
||||
|
||||
it('throws when the jump response omits skey', async () => {
|
||||
vi.mocked(oidb.runOidb).mockResolvedValueOnce({ clientKey: 'ck', keyIndex: 19 });
|
||||
vi.mocked(oidb.runOidb).mockResolvedValueOnce(
|
||||
protobuf_encode<OidbBase<OidbClientKeyResp>>({ body: { clientKey: 'ck', keyIndex: 19 } as any }),
|
||||
);
|
||||
vi.mocked(RequestUtil.HttpsGetCookies).mockResolvedValueOnce({});
|
||||
await expect(getSKey(bridge)).rejects.toThrow(/SKey is Empty/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('cookies — pure helpers', () => {
|
||||
beforeEach(() => { resetOidbMocks(); });
|
||||
|
||||
it('getBknFromSKey: deterministic djb2 truncated to 31 bits', () => {
|
||||
// Two calls on the same input → same number; different input → different.
|
||||
const a = getBknFromSKey('hello');
|
||||
@@ -147,21 +201,27 @@ describe('cookies — pure helpers', () => {
|
||||
});
|
||||
|
||||
it('getCookiesStr: joins cookies with "; "', async () => {
|
||||
vi.mocked(oidb.runOidb).mockResolvedValueOnce({ clientKey: 'ck', keyIndex: 19 });
|
||||
vi.mocked(oidb.runOidb).mockResolvedValueOnce(
|
||||
protobuf_encode<OidbBase<OidbClientKeyResp>>({ body: { clientKey: 'ck', keyIndex: 19 } as any }),
|
||||
);
|
||||
vi.mocked(RequestUtil.HttpsGetCookies).mockResolvedValueOnce({ p_skey: 'p', skey: 's' });
|
||||
const out = await getCookiesStr(bridge, 'qun.qq.com');
|
||||
expect(out).toBe('p_skey=p; skey=s');
|
||||
});
|
||||
|
||||
it('getCsrfToken: combines getSKey + getBknFromSKey', async () => {
|
||||
vi.mocked(oidb.runOidb).mockResolvedValueOnce({ clientKey: 'ck', keyIndex: 19 });
|
||||
vi.mocked(oidb.runOidb).mockResolvedValueOnce(
|
||||
protobuf_encode<OidbBase<OidbClientKeyResp>>({ body: { clientKey: 'ck', keyIndex: 19 } as any }),
|
||||
);
|
||||
vi.mocked(RequestUtil.HttpsGetCookies).mockResolvedValueOnce({ skey: 'abc' });
|
||||
const csrf = await getCsrfToken(bridge);
|
||||
expect(csrf).toBe(getBknFromSKey('abc'));
|
||||
});
|
||||
|
||||
it('getCredentials: returns { cookies, token, csrf_token } with bkn(p_skey)', async () => {
|
||||
vi.mocked(oidb.runOidb).mockResolvedValueOnce({ clientKey: 'ck', keyIndex: 19 });
|
||||
vi.mocked(oidb.runOidb).mockResolvedValueOnce(
|
||||
protobuf_encode<OidbBase<OidbClientKeyResp>>({ body: { clientKey: 'ck', keyIndex: 19 } as any }),
|
||||
);
|
||||
vi.mocked(RequestUtil.HttpsGetCookies).mockResolvedValueOnce({ p_skey: 'p', skey: 's' });
|
||||
|
||||
const out = await getCredentials(bridge, 'qun.qq.com');
|
||||
@@ -172,7 +232,9 @@ describe('cookies — pure helpers', () => {
|
||||
|
||||
it('getCredentials: token is 0 when no skey/p_skey available', async () => {
|
||||
vi.mocked(oidb.runOidb)
|
||||
.mockResolvedValueOnce({ clientKey: 'ck', keyIndex: 19 })
|
||||
.mockResolvedValueOnce(
|
||||
protobuf_encode<OidbBase<OidbClientKeyResp>>({ body: { clientKey: 'ck', keyIndex: 19 } as any }),
|
||||
)
|
||||
.mockRejectedValueOnce(new Error('no pskey'));
|
||||
vi.mocked(RequestUtil.HttpsGetCookies).mockResolvedValueOnce({});
|
||||
|
||||
|
||||
Reference in new issue
Block a user