Files
dragonchain-prime-node-sdk/tests/credentials.test.ts
Andrew Miller 98ab2e05a7
Some checks failed
Build and Test / build (16.x) (push) Failing after 12s
Build and Test / build (18.x) (push) Failing after 6s
Build and Test / build (20.x) (push) Failing after 6s
Initial Commit
2025-11-05 15:25:20 -05:00

106 lines
3.3 KiB
TypeScript

/**
* Tests for credentials module
*/
import * as fs from 'fs';
import * as path from 'path';
import { loadConfig, loadConfigFromString, getDefaultChain, getChainByPublicId, listChains } from '../src/credentials';
describe('Credentials Module', () => {
const testYaml = `
default: test-chain-id
chains:
- name: test-chain
publicId: test-chain-id
authKeyId: test-auth-key-id
authKey: test-auth-key
endpoint: https://test.dragonchain.com
- name: another-chain
publicId: another-chain-id
authKeyId: another-auth-key-id
authKey: another-auth-key
endpoint: https://another.dragonchain.com
`;
describe('loadConfigFromString', () => {
it('should load config from YAML string', () => {
const config = loadConfigFromString(testYaml);
expect(config.default).toBe('test-chain-id');
expect(config.chains).toHaveLength(2);
expect(config.chains[0].name).toBe('test-chain');
});
it('should throw error for invalid YAML', () => {
expect(() => loadConfigFromString('invalid: [yaml')).toThrow();
});
it('should throw error for missing chains array', () => {
expect(() => loadConfigFromString('default: test')).toThrow('Invalid config: missing or invalid chains array');
});
});
describe('loadConfig', () => {
const testConfigPath = path.join(__dirname, 'test-config.yaml');
beforeEach(() => {
fs.writeFileSync(testConfigPath, testYaml);
});
afterEach(() => {
if (fs.existsSync(testConfigPath)) {
fs.unlinkSync(testConfigPath);
}
});
it('should load config from file', () => {
const config = loadConfig(testConfigPath);
expect(config.default).toBe('test-chain-id');
expect(config.chains).toHaveLength(2);
});
it('should throw error for non-existent file', () => {
expect(() => loadConfig('/non/existent/path.yaml')).toThrow('Config file not found');
});
});
describe('getDefaultChain', () => {
it('should return the default chain', () => {
const config = loadConfigFromString(testYaml);
const defaultChain = getDefaultChain(config);
expect(defaultChain).toBeDefined();
expect(defaultChain?.name).toBe('test-chain');
expect(defaultChain?.publicId).toBe('test-chain-id');
});
it('should return undefined if default not found', () => {
const config = loadConfigFromString(testYaml);
config.default = 'non-existent';
const defaultChain = getDefaultChain(config);
expect(defaultChain).toBeUndefined();
});
});
describe('getChainByPublicId', () => {
it('should return chain by public ID', () => {
const config = loadConfigFromString(testYaml);
const chain = getChainByPublicId(config, 'another-chain-id');
expect(chain).toBeDefined();
expect(chain?.name).toBe('another-chain');
});
it('should return undefined if chain not found', () => {
const config = loadConfigFromString(testYaml);
const chain = getChainByPublicId(config, 'non-existent');
expect(chain).toBeUndefined();
});
});
describe('listChains', () => {
it('should return all chain names', () => {
const config = loadConfigFromString(testYaml);
const names = listChains(config);
expect(names).toEqual(['test-chain', 'another-chain']);
});
});
});