refactor: enforce enum machine codes
Use owner enums for error and T-Bank protocol values with ESLint regression checks. Preserve open wire fallbacks and keep protocol imports independent of DI and Prisma. Verified npm ci, verify, 226 unit tests, 262 isolated e2e tests and unchanged OpenAPI.
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
import { loadSync } from '@grpc/proto-loader';
|
||||
import { join } from 'node:path';
|
||||
|
||||
import { isTbankErrorCode, TbankErrorCode } from './tbank-error-code';
|
||||
import {
|
||||
InstrumentIdType,
|
||||
isTbankProtocolValue,
|
||||
TbankAccountStatus,
|
||||
TbankAccountType,
|
||||
TbankCandleInterval,
|
||||
TbankCouponType,
|
||||
TbankInstrumentType,
|
||||
TbankOperationState,
|
||||
TbankOperationType,
|
||||
} from './tbank-protocol';
|
||||
|
||||
it('uses the string enum values declared by the vendored protocol', () => {
|
||||
const directory = join(__dirname, 'proto/contracts');
|
||||
const definitions = loadSync(['users.proto', 'instruments.proto', 'operations.proto', 'marketdata.proto'].map((file) => join(directory, file)), { includeDirs: [directory], enums: String });
|
||||
for (const [name, values] of Object.entries({
|
||||
InstrumentIdType,
|
||||
InstrumentType: TbankInstrumentType,
|
||||
AccountStatus: TbankAccountStatus,
|
||||
AccountType: TbankAccountType,
|
||||
CandleInterval: TbankCandleInterval,
|
||||
CouponType: TbankCouponType,
|
||||
OperationState: TbankOperationState,
|
||||
OperationType: TbankOperationType,
|
||||
})) {
|
||||
const definition = definitions[`tinkoff.public.invest.api.contract.v1.${name}`];
|
||||
expect(definition).toHaveProperty('format', 'Protocol Buffer 3 EnumDescriptorProto');
|
||||
const descriptor = definition as { type: { value: { name: string }[] } };
|
||||
expect(descriptor.type.value.map((member) => member.name)).toEqual(expect.arrayContaining(Object.values(values)));
|
||||
}
|
||||
});
|
||||
|
||||
it('recognizes supported wire values without accepting unknown ones', () => {
|
||||
expect(isTbankProtocolValue('OPERATION_STATE_EXECUTED', TbankOperationState.Executed)).toBe(true);
|
||||
expect(isTbankProtocolValue('NEW_UPSTREAM_STATE', TbankOperationState.Executed)).toBe(false);
|
||||
expect(isTbankErrorCode('INVALID_TBANK_TOKEN', TbankErrorCode.InvalidToken)).toBe(true);
|
||||
expect(isTbankErrorCode('NEW_UPSTREAM_ERROR', TbankErrorCode.InvalidToken)).toBe(false);
|
||||
});
|
||||
Reference in New Issue
Block a user