Use protopiler for protocol buffers

Co-authored-by: Jamie Kyle <jamie@signal.org>
This commit is contained in:
Fedor Indutny
2026-03-10 15:31:29 -07:00
committed by GitHub
parent b0e19f334e
commit c4ee32e9ee
97 changed files with 6197 additions and 6362 deletions

View File

@@ -5,30 +5,40 @@ import { assert } from 'chai';
import { arePinnedConversationsEqual } from '../../util/arePinnedConversationsEqual.node.js';
import { SignalService as Proto } from '../../protobuf/index.std.js';
import PinnedConversation = Proto.AccountRecord.IPinnedConversation;
import PinnedConversation = Proto.AccountRecord.PinnedConversation.Params;
describe('arePinnedConversationsEqual', () => {
it('is equal if both have same values at same indices', () => {
const localValue = [
{
contact: {
serviceId: '72313cde-2784-4a6f-a92a-abbe23763a60',
e164: '+13055551234',
identifier: {
contact: {
serviceId: '72313cde-2784-4a6f-a92a-abbe23763a60',
serviceIdBinary: null,
e164: '+13055551234',
},
},
},
{
groupMasterKey: new Uint8Array(32),
identifier: {
groupMasterKey: new Uint8Array(32),
},
},
];
const remoteValue = [
{
contact: {
serviceId: '72313cde-2784-4a6f-a92a-abbe23763a60',
e164: '+13055551234',
identifier: {
contact: {
serviceId: '72313cde-2784-4a6f-a92a-abbe23763a60',
serviceIdBinary: null,
e164: '+13055551234',
},
},
},
{
groupMasterKey: new Uint8Array(32),
identifier: {
groupMasterKey: new Uint8Array(32),
},
},
];
@@ -38,29 +48,41 @@ describe('arePinnedConversationsEqual', () => {
it('is not equal if values are mixed', () => {
const localValue = [
{
contact: {
serviceId: '72313cde-2784-4a6f-a92a-abbe23763a60',
e164: '+13055551234',
identifier: {
contact: {
serviceId: '72313cde-2784-4a6f-a92a-abbe23763a60',
serviceIdBinary: null,
e164: '+13055551234',
},
},
},
{
contact: {
serviceId: 'f59a9fed-9e91-4bb4-a015-d49e58b47e25',
e164: '+17865554321',
identifier: {
contact: {
serviceId: 'f59a9fed-9e91-4bb4-a015-d49e58b47e25',
serviceIdBinary: null,
e164: '+17865554321',
},
},
},
];
const remoteValue = [
{
contact: {
serviceId: 'f59a9fed-9e91-4bb4-a015-d49e58b47e25',
e164: '+17865554321',
identifier: {
contact: {
serviceId: 'f59a9fed-9e91-4bb4-a015-d49e58b47e25',
serviceIdBinary: null,
e164: '+17865554321',
},
},
},
{
contact: {
serviceId: '72313cde-2784-4a6f-a92a-abbe23763a60',
e164: '+13055551234',
identifier: {
contact: {
serviceId: '72313cde-2784-4a6f-a92a-abbe23763a60',
serviceIdBinary: null,
e164: '+13055551234',
},
},
},
];
@@ -71,9 +93,12 @@ describe('arePinnedConversationsEqual', () => {
it('is not equal if lengths are not same', () => {
const localValue = [
{
contact: {
serviceId: '72313cde-2784-4a6f-a92a-abbe23763a60',
e164: '+13055551234',
identifier: {
contact: {
serviceId: '72313cde-2784-4a6f-a92a-abbe23763a60',
serviceIdBinary: null,
e164: '+13055551234',
},
},
},
];
@@ -84,15 +109,20 @@ describe('arePinnedConversationsEqual', () => {
it('is not equal if content does not match', () => {
const localValue = [
{
contact: {
serviceId: '72313cde-2784-4a6f-a92a-abbe23763a60',
e164: '+13055551234',
identifier: {
contact: {
serviceId: '72313cde-2784-4a6f-a92a-abbe23763a60',
serviceIdBinary: null,
e164: '+13055551234',
},
},
},
];
const remoteValue = [
{
groupMasterKey: new Uint8Array(32),
identifier: {
groupMasterKey: new Uint8Array(32),
},
},
];
assert.isFalse(arePinnedConversationsEqual(localValue, remoteValue));

View File

@@ -20,11 +20,11 @@ describe('callingMessageToProto', () => {
describe('hangup field', () => {
it('leaves the field unset if `hangup` is not provided', () => {
const result = callingMessageToProto(new CallingMessage());
assert.isUndefined(result.hangup);
assert.isNull(result.hangup);
});
it('attaches the type if provided', () => {
const callId: CallId = { high: 0, low: 0, unsigned: false };
const callId: CallId = 0n;
const callingMessage = new CallingMessage();
callingMessage.hangup = new HangupMessage(callId, HangupType.Busy, 1);
@@ -38,7 +38,7 @@ describe('callingMessageToProto', () => {
describe('opaque field', () => {
it('leaves the field unset if neither `opaque` nor urgency are provided', () => {
const result = callingMessageToProto(new CallingMessage());
assert.isUndefined(result.opaque);
assert.isNull(result.opaque);
});
it('attaches opaque data', () => {

View File

@@ -4,12 +4,13 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { assert } from 'chai';
import Long from 'long';
import * as Bytes from '../../Bytes.std.js';
import type { LocalUserDataType } from '../../util/sessionTranslation.node.js';
import { sessionRecordToProtobuf } from '../../util/sessionTranslation.node.js';
import { toNumber } from '../../util/toNumber.std.js';
const getRecordCopy = (record: any): any => JSON.parse(JSON.stringify(record));
export const SESSION_V1_RECORD = {
@@ -208,8 +209,8 @@ function protoToJSON(value: unknown): unknown {
return value.map(protoToJSON);
}
if (Long.isLong(value)) {
return value.toNumber();
if (typeof value === 'bigint') {
return toNumber(value);
}
if (typeof value === 'object') {
@@ -310,6 +311,7 @@ describe('sessionTranslation', () => {
receiverChains: [
{
senderRatchetKey: 'BQo3HG1UhWIh6A7NBxZtNGezBZH8nElZjOqNCBHPzlBz',
senderRatchetKeyPrivate: null,
chainKey: {
index: 6,
key: 'Wnvy2TjYs0HdZFNahmsKw5cc9KEbW1nSwraDFmGwBDw=',
@@ -332,6 +334,8 @@ describe('sessionTranslation', () => {
],
remoteRegistrationId: 4243,
localRegistrationId: 3554,
needsRefresh: null,
pendingPreKey: null,
aliceBaseKey: 'BVeHv5MAbMgKeaoO/G1CMBdqhC7bo7Mtc4EWxI0oT19N',
},
previousSessions: [],
@@ -370,6 +374,7 @@ describe('sessionTranslation', () => {
receiverChains: [
{
senderRatchetKey: 'BQo3HG1UhWIh6A7NBxZtNGezBZH8nElZjOqNCBHPzlBz',
senderRatchetKeyPrivate: null,
chainKey: {
index: 6,
key: 'Wnvy2TjYs0HdZFNahmsKw5cc9KEbW1nSwraDFmGwBDw=',
@@ -391,8 +396,10 @@ describe('sessionTranslation', () => {
},
{
senderRatchetKey: 'BTpb20+IlnBkryDC2ecQT96Hd3t9/Qh3ljnA3509kxRa',
senderRatchetKeyPrivate: null,
chainKey: {
index: 2,
key: null,
},
messageKeys: [
{
@@ -405,8 +412,10 @@ describe('sessionTranslation', () => {
},
{
senderRatchetKey: 'Bd5nlMVr6YMBE5eh//tOWMgoOQakkneYri/YuVJpi0pJ',
senderRatchetKeyPrivate: null,
chainKey: {
index: 12,
key: null,
},
messageKeys: [
{
@@ -437,8 +446,10 @@ describe('sessionTranslation', () => {
},
{
senderRatchetKey: 'BYSxQO1OIs0ZSFN7JI/vF5Rb0VwaKjs+UAAfDkhOYfkp',
senderRatchetKeyPrivate: null,
chainKey: {
index: 6,
key: null,
},
messageKeys: [
{
@@ -469,22 +480,28 @@ describe('sessionTranslation', () => {
},
{
senderRatchetKey: 'BbXSFD/IoivRUvfnPzOaRLqDXEAwi4YEristfwiOj3IJ',
senderRatchetKeyPrivate: null,
chainKey: {
index: 3,
key: null,
},
messageKeys: [],
},
{
senderRatchetKey: 'BRRAnr1NhizgCPPzmYV9qGBpvwCpSQH0Rx+UOtl78wUg',
senderRatchetKeyPrivate: null,
chainKey: {
index: 1,
key: null,
},
messageKeys: [],
},
{
senderRatchetKey: 'BZvOKPA+kXiCg8TIP/52fu1reCDirC7wb5nyRGce3y4N',
senderRatchetKeyPrivate: null,
chainKey: {
index: 7,
key: null,
},
messageKeys: [
{
@@ -497,8 +514,10 @@ describe('sessionTranslation', () => {
},
{
senderRatchetKey: 'Ba9q9bHjMHfbUNDCU8+0O7cmEcIluq+wk3/d2f7q+ThG',
senderRatchetKeyPrivate: null,
chainKey: {
index: 4,
key: null,
},
messageKeys: [
{
@@ -523,28 +542,36 @@ describe('sessionTranslation', () => {
},
{
senderRatchetKey: 'BTwX5SmcUeBG7mwyOZ3YgxyXIN0ktzuEdWTfBUmPfGYG',
senderRatchetKeyPrivate: null,
chainKey: {
index: 2,
key: null,
},
messageKeys: [],
},
{
senderRatchetKey: 'BV7ECvKbwKIAD61BXDYr0xr3JtckuKzR1Hw8cVPWGtlo',
senderRatchetKeyPrivate: null,
chainKey: {
index: 3,
key: null,
},
messageKeys: [],
},
{
senderRatchetKey: 'BTC7rQqoykGR5Aaix7RkAhI5fSXufc6pVGN9OIC8EW5c',
senderRatchetKeyPrivate: null,
chainKey: {
index: 1,
key: null,
},
messageKeys: [],
},
],
remoteRegistrationId: 4243,
localRegistrationId: 3554,
needsRefresh: null,
pendingPreKey: null,
aliceBaseKey: 'BVeHv5MAbMgKeaoO/G1CMBdqhC7bo7Mtc4EWxI0oT19N',
},
previousSessions: [],
@@ -632,6 +659,7 @@ describe('sessionTranslation', () => {
receiverChains: [
{
senderRatchetKey: 'BQo3HG1UhWIh6A7NBxZtNGezBZH8nElZjOqNCBHPzlBz',
senderRatchetKeyPrivate: null,
chainKey: {
index: 6,
key: 'Wnvy2TjYs0HdZFNahmsKw5cc9KEbW1nSwraDFmGwBDw=',
@@ -659,6 +687,7 @@ describe('sessionTranslation', () => {
},
remoteRegistrationId: 4243,
localRegistrationId: 3554,
needsRefresh: null,
aliceBaseKey: 'BVeHv5MAbMgKeaoO/G1CMBdqhC7bo7Mtc4EWxI0oT19N',
},
previousSessions: [],
@@ -821,6 +850,7 @@ describe('sessionTranslation', () => {
receiverChains: [
{
senderRatchetKey: 'BQo3HG1UhWIh6A7NBxZtNGezBZH8nElZjOqNCBHPzlBz',
senderRatchetKeyPrivate: null,
chainKey: {
index: 6,
key: 'Wnvy2TjYs0HdZFNahmsKw5cc9KEbW1nSwraDFmGwBDw=',
@@ -843,6 +873,8 @@ describe('sessionTranslation', () => {
],
remoteRegistrationId: 4243,
localRegistrationId: 3554,
needsRefresh: null,
pendingPreKey: null,
aliceBaseKey: 'BVeHv5MAbMgKeaoO/G1CMBdqhC7bo7Mtc4EWxI0oT19N',
},
previousSessions: [
@@ -865,6 +897,7 @@ describe('sessionTranslation', () => {
receiverChains: [
{
senderRatchetKey: 'BQo3HG1UhWIh6A7NBxZtNGezBZH8nElZjOqNCBHPzlBz',
senderRatchetKeyPrivate: null,
chainKey: {
index: 6,
key: 'Wnvy2TjYs0HdZFNahmsKw5cc9KEbW1nSwraDFmGwBDw=',
@@ -887,6 +920,8 @@ describe('sessionTranslation', () => {
],
remoteRegistrationId: 2312,
localRegistrationId: 3554,
needsRefresh: null,
pendingPreKey: null,
aliceBaseKey: 'BUFOv0MAbMgKeaoO/G1CMBdqhC7bo7Mtc4EWxI0oT19N',
},
{
@@ -908,6 +943,7 @@ describe('sessionTranslation', () => {
receiverChains: [
{
senderRatchetKey: 'BQo3HG1UhWIh6A7NBxZtNGezBZH8nElZjOqNCBHPzlBz',
senderRatchetKeyPrivate: null,
chainKey: {
index: 6,
key: 'Wnvy2TjYs0HdZFNahmsKw5cc9KEbW1nSwraDFmGwBDw=',
@@ -930,6 +966,8 @@ describe('sessionTranslation', () => {
],
remoteRegistrationId: 3432,
localRegistrationId: 3554,
needsRefresh: null,
pendingPreKey: null,
aliceBaseKey: 'BUJEv1oAbMgKeaoO/G1CMBdqhC7bo7Mtc4EWxI0oT19N',
},
],
@@ -991,6 +1029,7 @@ describe('sessionTranslation', () => {
aliceBaseKey: 'BTU+PWVWuRnbiW6+ja+XI9+2Xz0TLk7uGqUlhS1d+V8K',
localIdentityPublic: 'Baioqfzc/5JD6b+GNqapPouf6eHK7xr9ynLJHnvl+444',
localRegistrationId: 3554,
needsRefresh: null,
pendingPreKey: {
baseKey: 'BTU+PWVWuRnbiW6+ja+XI9+2Xz0TLk7uGqUlhS1d+V8K',
preKeyId: 386,

View File

@@ -2,7 +2,6 @@
// SPDX-License-Identifier: AGPL-3.0-only
import { assert } from 'chai';
import Long from 'long';
import {
getSafeLongFromTimestamp,
@@ -12,34 +11,38 @@ import {
getCheckedTimestampOrUndefinedFromLong,
} from '../../util/timestampLongUtils.std.js';
import { MAX_SAFE_DATE } from '../../util/timestamp.std.js';
import { MAX_VALUE } from '../../util/long.std.js';
import { toNumber } from '../../util/toNumber.std.js';
describe('getSafeLongFromTimestamp', () => {
it('returns zero when passed undefined', () => {
assert(getSafeLongFromTimestamp(undefined).isZero());
assert.strictEqual(getSafeLongFromTimestamp(undefined), 0n);
});
it('returns the number as a Long when passed a "normal" number', () => {
assert(getSafeLongFromTimestamp(0).isZero());
assert.strictEqual(getSafeLongFromTimestamp(0), 0n);
assert.strictEqual(getSafeLongFromTimestamp(123).toString(), '123');
assert.strictEqual(getSafeLongFromTimestamp(-456).toString(), '-456');
});
it('returns MAX_SAFE_DATE when passed Infinity', () => {
assert.strictEqual(
getSafeLongFromTimestamp(Infinity).toNumber(),
toNumber(getSafeLongFromTimestamp(Infinity)),
MAX_SAFE_DATE
);
});
it('returns Long.MAX_VALUE when passed Infinity and overriden', () => {
assert(
getSafeLongFromTimestamp(Infinity, Long.MAX_VALUE).equals(Long.MAX_VALUE)
it('returns MAX_VALUE when passed Infinity and overriden', () => {
assert.strictEqual(
getSafeLongFromTimestamp(Infinity, MAX_VALUE),
MAX_VALUE
);
});
it("returns MAX_SAFE_DATE when passed very large numbers, outside of JavaScript's safely representable range", () => {
assert.strictEqual(
getSafeLongFromTimestamp(Number.MAX_VALUE).toNumber(),
toNumber(getSafeLongFromTimestamp(Number.MAX_VALUE)),
MAX_SAFE_DATE
);
});
@@ -47,19 +50,19 @@ describe('getSafeLongFromTimestamp', () => {
describe('getTimestampFromLong', () => {
it('returns zero when passed negative Long', () => {
assert.equal(getTimestampFromLong(Long.fromNumber(-1)), 0);
assert.equal(getTimestampFromLong(BigInt(-1)), 0);
});
it('returns zero when passed 0 Long', () => {
assert.equal(getTimestampFromLong(Long.fromNumber(0)), 0);
assert.equal(getTimestampFromLong(0n), 0);
});
it('returns MAX_SAFE_DATE when passed Long.MAX_VALUE', () => {
assert.equal(getTimestampFromLong(Long.MAX_VALUE), MAX_SAFE_DATE);
it('returns MAX_SAFE_DATE when passed MAX_VALUE', () => {
assert.equal(getTimestampFromLong(MAX_VALUE), MAX_SAFE_DATE);
});
it('returns a normal number', () => {
assert.equal(getTimestampFromLong(Long.fromNumber(16)), 16);
assert.equal(getTimestampFromLong(16n), 16);
});
it('returns 0 for null value', () => {
@@ -73,35 +76,29 @@ describe('getCheckedTimestampFromLong', () => {
});
it('throws on negative Long', () => {
assert.throws(() => getCheckedTimestampFromLong(Long.fromNumber(-1)));
assert.throws(() => getCheckedTimestampFromLong(BigInt(-1)));
});
it('throws on Long.MAX_VALUE', () => {
assert.throws(() => getCheckedTimestampFromLong(Long.MAX_VALUE));
it('throws on MAX_VALUE', () => {
assert.throws(() => getCheckedTimestampFromLong(MAX_VALUE));
});
it('does not throw otherwise', () => {
assert.equal(getCheckedTimestampFromLong(Long.fromNumber(16)), 16);
assert.equal(getCheckedTimestampFromLong(16n), 16);
});
});
describe('getTimestampOrUndefinedFromLong', () => {
it('returns undefined when passed 0 Long', () => {
assert.equal(
getTimestampOrUndefinedFromLong(Long.fromNumber(0)),
undefined
);
assert.equal(getTimestampOrUndefinedFromLong(0n), undefined);
});
it('returns MAX_SAFE_DATE when passed Long.MAX_VALUE', () => {
assert.equal(
getTimestampOrUndefinedFromLong(Long.MAX_VALUE),
MAX_SAFE_DATE
);
it('returns MAX_SAFE_DATE when passed MAX_VALUE', () => {
assert.equal(getTimestampOrUndefinedFromLong(MAX_VALUE), MAX_SAFE_DATE);
});
it('returns a normal number', () => {
assert.equal(getTimestampOrUndefinedFromLong(Long.fromNumber(16)), 16);
assert.equal(getTimestampOrUndefinedFromLong(16n), 16);
});
it('returns undefined for null value', () => {
@@ -111,9 +108,7 @@ describe('getTimestampOrUndefinedFromLong', () => {
describe('getCheckedTimestampOrUndefinedFromLong', () => {
it('throws on negative Long', () => {
assert.throws(() =>
getCheckedTimestampOrUndefinedFromLong(Long.fromNumber(-1))
);
assert.throws(() => getCheckedTimestampOrUndefinedFromLong(-1n));
});
it('returns undefined on absent Long', () => {
@@ -121,13 +116,10 @@ describe('getCheckedTimestampOrUndefinedFromLong', () => {
});
it('returns undefined on zero Long', () => {
assert.equal(getCheckedTimestampOrUndefinedFromLong(Long.ZERO), undefined);
assert.equal(getCheckedTimestampOrUndefinedFromLong(0n), undefined);
});
it('returns a normal number', () => {
assert.equal(
getCheckedTimestampOrUndefinedFromLong(Long.fromNumber(16)),
16
);
assert.equal(getCheckedTimestampOrUndefinedFromLong(16n), 16);
});
});