refactor(oidb): thread ctx into serialize/deserialize for self-contained namespaces
The first pass at namespace-per-cmd left a smell: namespaces that
needed identity/uid resolution had to declare `Deps` AND then rewrite
their own spec inside `invoke` via a closure-over-resolved-value:
// before
export const serialize = (p: Params, targetUid: string) => ({...});
export const invoke = async (deps, params) => {
const targetUid = await deps.resolveUserUid(params.userId);
return invokeOidb(deps, {
...Self,
serialize: p => serialize(p, targetUid), // ← escape hatch
}, params);
};
Reading "what does this cmd do on the wire?" required reading the
namespace AND the closure rewrite — the namespace wasn't really
self-contained, just a forwarder of two pure functions.
Fix: thread `ctx` (the namespace's full capability surface) into
`serialize` and `deserialize` directly. `serialize` may now return
`Promise<TReq>` so identity / uid lookups happen in place:
// after
export const serialize = async (ctx: Deps, p: Params) => ({
groupUin: p.groupId, type: 1,
body: {
targetUid: await ctx.resolveUserUid(p.userId, p.groupId),
duration: p.duration,
},
});
export const invoke = (deps, params) => invokeOidb(deps, Self, params);
OidbCallSpec gains a `TCtx extends OidbSender` generic; invokeOidb's
first arg becomes `ctx: TCtx` (the sender capability is just a slice
of ctx now). buildOidbRequest / parseOidbResponse follow the same
shape. Async serialize is awaited inside invokeOidb.
17 namespaces had closure rewrites; all 17 collapse to plain
`invokeOidb(deps, Self, params)` invokes. Only one closure remains —
`fetch-user-profile` binds `params.uin` into deserialize as a server-
echo fallback; kept as a documented exception (the rare case where
deserialize wants request context, not ctx).
Wire bytes preserved across all migrations. 326 bridge tests + 302
core tests pass; lint clean.
This commit is contained in:
80 files changed
+416
-397
No files matched your search
@@ -22,13 +22,13 @@ describe('FetchReactionSummary namespace', () => {
|
||||
|
||||
describe('serialize', () => {
|
||||
it('encodes groupId/sequence as BigInt for uint_64 fields', () => {
|
||||
const out = FetchReactionSummary.serialize({ groupId: 1022489779, sequence: 1730183 });
|
||||
const out = FetchReactionSummary.serialize({} as any, { groupId: 1022489779, sequence: 1730183 });
|
||||
expect(out.groupId).toBe(1022489779n);
|
||||
expect(out.sequence).toBe(1730183n);
|
||||
});
|
||||
|
||||
it('passes empty / default values for the unused filter fields', () => {
|
||||
const out = FetchReactionSummary.serialize({ groupId: 1, sequence: 1 });
|
||||
const out = FetchReactionSummary.serialize({} as any, { groupId: 1, sequence: 1 });
|
||||
expect(out.emojiId).toBe('');
|
||||
expect(out.emojiType).toBe(0);
|
||||
expect(out.count).toBe(0);
|
||||
@@ -36,7 +36,7 @@ describe('FetchReactionSummary namespace', () => {
|
||||
});
|
||||
|
||||
it('emits a 0-length cookie buffer (no continuation)', () => {
|
||||
const out = FetchReactionSummary.serialize({ groupId: 1, sequence: 1 });
|
||||
const out = FetchReactionSummary.serialize({} as any, { groupId: 1, sequence: 1 });
|
||||
expect(out.cookie).toBeInstanceOf(Uint8Array);
|
||||
expect(out.cookie!.length).toBe(0);
|
||||
});
|
||||
@@ -50,7 +50,7 @@ describe('FetchReactionSummary namespace', () => {
|
||||
{ lastReactionTime: 1779456962n, count: 1, emojiType: 1, emojiId: '124' },
|
||||
],
|
||||
};
|
||||
expect(FetchReactionSummary.deserialize(body)).toEqual([
|
||||
expect(FetchReactionSummary.deserialize({} as any, body)).toEqual([
|
||||
{ emojiId: '76', emojiType: 1, count: 3, lastReactionTime: 1779456439 },
|
||||
{ emojiId: '124', emojiType: 1, count: 1, lastReactionTime: 1779456962 },
|
||||
]);
|
||||
@@ -64,20 +64,20 @@ describe('FetchReactionSummary namespace', () => {
|
||||
{ emojiType: 1, emojiId: '66' }, // catalog entry
|
||||
],
|
||||
};
|
||||
const out = FetchReactionSummary.deserialize(body);
|
||||
const out = FetchReactionSummary.deserialize({} as any, body);
|
||||
expect(out).toHaveLength(1);
|
||||
expect(out[0]!.emojiId).toBe('76');
|
||||
});
|
||||
|
||||
it('returns an empty array when the response has no entries', () => {
|
||||
expect(FetchReactionSummary.deserialize({})).toEqual([]);
|
||||
expect(FetchReactionSummary.deserialize({} as any, {})).toEqual([]);
|
||||
});
|
||||
|
||||
it('defaults emojiId / emojiType to safe values when omitted', () => {
|
||||
const body: Oidb0x9084Resp = {
|
||||
entries: [{ count: 5, lastReactionTime: 1700000000n }],
|
||||
};
|
||||
const [entry] = FetchReactionSummary.deserialize(body);
|
||||
const [entry] = FetchReactionSummary.deserialize({} as any, body);
|
||||
expect(entry).toEqual({
|
||||
emojiId: '', emojiType: 1, count: 5, lastReactionTime: 1700000000,
|
||||
});
|
||||
@@ -85,7 +85,7 @@ describe('FetchReactionSummary namespace', () => {
|
||||
|
||||
it('reports lastReactionTime=0 when omitted on a used entry (degenerate but defensive)', () => {
|
||||
const body: Oidb0x9084Resp = { entries: [{ count: 2, emojiId: 'x', emojiType: 1 }] };
|
||||
expect(FetchReactionSummary.deserialize(body)[0]!.lastReactionTime).toBe(0);
|
||||
expect(FetchReactionSummary.deserialize({} as any, body)[0]!.lastReactionTime).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ describe('GetEmojiLikes namespace', () => {
|
||||
|
||||
describe('serialize', () => {
|
||||
it('encodes groupId/sequence as BigInt for uint_64 fields', () => {
|
||||
const out = GetEmojiLikes.serialize({
|
||||
const out = GetEmojiLikes.serialize({} as any, {
|
||||
groupId: 12345, sequence: 99, emojiId: '76',
|
||||
});
|
||||
expect(out.groupId).toBe(12345n);
|
||||
@@ -30,44 +30,44 @@ describe('GetEmojiLikes namespace', () => {
|
||||
});
|
||||
|
||||
it('passes emojiId through unchanged', () => {
|
||||
const out = GetEmojiLikes.serialize({ groupId: 1, sequence: 1, emojiId: '128516' });
|
||||
const out = GetEmojiLikes.serialize({} as any, { groupId: 1, sequence: 1, emojiId: '128516' });
|
||||
expect(out.emojiId).toBe('128516');
|
||||
});
|
||||
|
||||
it('defaults emojiType to 1 when omitted', () => {
|
||||
const out = GetEmojiLikes.serialize({ groupId: 1, sequence: 1, emojiId: 'x' });
|
||||
const out = GetEmojiLikes.serialize({} as any, { groupId: 1, sequence: 1, emojiId: 'x' });
|
||||
expect(out.emojiType).toBe(1);
|
||||
});
|
||||
|
||||
it('uses caller-supplied emojiType when present', () => {
|
||||
const out = GetEmojiLikes.serialize({ groupId: 1, sequence: 1, emojiId: 'x', emojiType: 2 });
|
||||
const out = GetEmojiLikes.serialize({} as any, { groupId: 1, sequence: 1, emojiId: 'x', emojiType: 2 });
|
||||
expect(out.emojiType).toBe(2);
|
||||
});
|
||||
|
||||
it('defaults count to 10 when omitted', () => {
|
||||
const out = GetEmojiLikes.serialize({ groupId: 1, sequence: 1, emojiId: 'x' });
|
||||
const out = GetEmojiLikes.serialize({} as any, { groupId: 1, sequence: 1, emojiId: 'x' });
|
||||
expect(out.count).toBe(10);
|
||||
});
|
||||
|
||||
it('uses caller-supplied count when present', () => {
|
||||
const out = GetEmojiLikes.serialize({ groupId: 1, sequence: 1, emojiId: 'x', count: 50 });
|
||||
const out = GetEmojiLikes.serialize({} as any, { groupId: 1, sequence: 1, emojiId: 'x', count: 50 });
|
||||
expect(out.count).toBe(50);
|
||||
});
|
||||
|
||||
it('decodes base64 cookie into the bytes field for continuation', () => {
|
||||
const cookie = Buffer.from([0xCA, 0xFE]).toString('base64');
|
||||
const out = GetEmojiLikes.serialize({ groupId: 1, sequence: 1, emojiId: 'x', cookie });
|
||||
const out = GetEmojiLikes.serialize({} as any, { groupId: 1, sequence: 1, emojiId: 'x', cookie });
|
||||
expect(out.cookie).toBeInstanceOf(Uint8Array);
|
||||
expect(Buffer.from(out.cookie!).equals(Buffer.from([0xCA, 0xFE]))).toBe(true);
|
||||
});
|
||||
|
||||
it('emits a 0-length cookie buffer for the first page (no cookie supplied)', () => {
|
||||
const out = GetEmojiLikes.serialize({ groupId: 1, sequence: 1, emojiId: 'x' });
|
||||
const out = GetEmojiLikes.serialize({} as any, { groupId: 1, sequence: 1, emojiId: 'x' });
|
||||
expect(out.cookie!.length).toBe(0);
|
||||
});
|
||||
|
||||
it('sets field7=0 and field12=1 (magic values matching the historic wire shape)', () => {
|
||||
const out = GetEmojiLikes.serialize({ groupId: 1, sequence: 1, emojiId: 'x' });
|
||||
const out = GetEmojiLikes.serialize({} as any, { groupId: 1, sequence: 1, emojiId: 'x' });
|
||||
expect(out.field7).toBe(0);
|
||||
expect(out.field12).toBe(1);
|
||||
});
|
||||
@@ -75,7 +75,7 @@ describe('GetEmojiLikes namespace', () => {
|
||||
|
||||
describe('deserialize', () => {
|
||||
it('extracts users from inner.userInfo and base64-encodes the cookie', () => {
|
||||
const result = GetEmojiLikes.deserialize({
|
||||
const result = GetEmojiLikes.deserialize({} as any, {
|
||||
inner: { userInfo: [{ uin: 10001n }, { uin: 20002n }] },
|
||||
cookie: new Uint8Array([0xCA, 0xFE]),
|
||||
});
|
||||
@@ -85,28 +85,28 @@ describe('GetEmojiLikes namespace', () => {
|
||||
});
|
||||
|
||||
it('filters out users with uin=0 (placeholder / zero-value)', () => {
|
||||
const result = GetEmojiLikes.deserialize({
|
||||
const result = GetEmojiLikes.deserialize({} as any, {
|
||||
inner: { userInfo: [{ uin: 10001n }, { uin: 0n }, { uin: 20002n }] },
|
||||
});
|
||||
expect(result.users).toEqual([{ uin: 10001 }, { uin: 20002 }]);
|
||||
});
|
||||
|
||||
it('returns empty users + empty cookie + isLast=true when body has nothing', () => {
|
||||
const result = GetEmojiLikes.deserialize({});
|
||||
const result = GetEmojiLikes.deserialize({} as any, {});
|
||||
expect(result.users).toEqual([]);
|
||||
expect(result.cookie).toBe('');
|
||||
expect(result.isLast).toBe(true);
|
||||
});
|
||||
|
||||
it('returns isLast=true when cookie is absent (final page)', () => {
|
||||
const result = GetEmojiLikes.deserialize({
|
||||
const result = GetEmojiLikes.deserialize({} as any, {
|
||||
inner: { userInfo: [{ uin: 10001n }] },
|
||||
});
|
||||
expect(result.isLast).toBe(true);
|
||||
});
|
||||
|
||||
it('handles missing userInfo entries field', () => {
|
||||
const result = GetEmojiLikes.deserialize({ inner: {} });
|
||||
const result = GetEmojiLikes.deserialize({} as any, { inner: {} });
|
||||
expect(result.users).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -26,31 +26,31 @@ describe('SetReaction namespace', () => {
|
||||
|
||||
describe('serialize', () => {
|
||||
it('produces type=1 for short QQ-face codes (≤3 chars)', () => {
|
||||
const out = SetReaction.serialize({ groupId: 12345, sequence: 99, code: '76', isSet: true });
|
||||
const out = SetReaction.serialize({} as any, { groupId: 12345, sequence: 99, code: '76', isSet: true });
|
||||
expect(out).toEqual({
|
||||
groupUin: 12345, sequence: 99, code: '76', type: 1, field6: false, field7: false,
|
||||
});
|
||||
});
|
||||
|
||||
it('produces type=1 for exactly 3-character codes (boundary)', () => {
|
||||
const out = SetReaction.serialize({ groupId: 1, sequence: 1, code: '999', isSet: true });
|
||||
const out = SetReaction.serialize({} as any, { groupId: 1, sequence: 1, code: '999', isSet: true });
|
||||
expect(out.type).toBe(1);
|
||||
});
|
||||
|
||||
it('produces type=2 for codes longer than 3 chars (unicode codepoint)', () => {
|
||||
const out = SetReaction.serialize({ groupId: 1, sequence: 1, code: '128516', isSet: false });
|
||||
const out = SetReaction.serialize({} as any, { groupId: 1, sequence: 1, code: '128516', isSet: false });
|
||||
expect(out.type).toBe(2);
|
||||
});
|
||||
|
||||
it('passes through groupId/sequence/code verbatim', () => {
|
||||
const out = SetReaction.serialize({ groupId: 999, sequence: 42, code: 'abc', isSet: true });
|
||||
const out = SetReaction.serialize({} as any, { groupId: 999, sequence: 42, code: 'abc', isSet: true });
|
||||
expect(out.groupUin).toBe(999);
|
||||
expect(out.sequence).toBe(42);
|
||||
expect(out.code).toBe('abc');
|
||||
});
|
||||
|
||||
it('always sets field6/field7 to false (deferred behaviour)', () => {
|
||||
const out = SetReaction.serialize({ groupId: 1, sequence: 1, code: 'x', isSet: true });
|
||||
const out = SetReaction.serialize({} as any, { groupId: 1, sequence: 1, code: 'x', isSet: true });
|
||||
expect(out.field6).toBe(false);
|
||||
expect(out.field7).toBe(false);
|
||||
});
|
||||
@@ -58,7 +58,7 @@ describe('SetReaction namespace', () => {
|
||||
|
||||
describe('deserialize', () => {
|
||||
it('always returns void regardless of body content', () => {
|
||||
expect(SetReaction.deserialize({})).toBeUndefined();
|
||||
expect(SetReaction.deserialize({} as any, {})).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user