Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confirm README has all example config keys in it #50

Merged
merged 8 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ A record of chain configurations. The record key is the chain ID. For example:

A record of contract addresses used by Airseeker.

###### Api3ServerV1 _(optional)_
###### `Api3ServerV1` _(optional)_

The address of the Api3ServerV1 contract. If not specified, the address is loaded from the
[Airnode protocol v1](https://github.com/api3dao/airnode-protocol-v1) repository.
Expand Down Expand Up @@ -149,6 +149,10 @@ The data needed to make the requests to signed API. This data will in the future
A mapping from Airnode address to signed API URL. When data from particular beacon is needed a request is made to the
signed API corresponding to the beacon address.

##### `activeDapiNames`

An array of dAPI names to execute updates for.

###### `dataFeedIdToBeacons`

A mapping from data feed ID to a list of beacon data.
Expand Down Expand Up @@ -212,6 +216,10 @@ The interval specifying how often to run the data feed update loop. In seconds.

The batch size of active dAPIs that are to be fetched in a single RPC call.

#### `fetchInterval`

The fetch interval in seconds between retrievals of signed API data.

## Docker

### Build
Expand Down
12 changes: 6 additions & 6 deletions src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import fs, { readFileSync } from 'node:fs';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';

import { go } from '@api3/promise-utils';
import { goSync } from '@api3/promise-utils';
import dotenv from 'dotenv';

import { configSchema } from './schema';
import { interpolateSecrets, parseSecrets } from './utils';

export const loadConfig = async () => {
export const loadConfig = () => {
const configPath = join(__dirname, '../../config');
const rawSecrets = dotenv.parse(readFileSync(join(configPath, 'secrets.env'), 'utf8'));

const goLoadConfig = await go(async () => {
const rawConfig = JSON.parse(fs.readFileSync(join(configPath, 'airseeker.json'), 'utf8'));
const goLoadConfig = goSync(() => {
const rawConfig = JSON.parse(readFileSync(join(configPath, 'airseeker.json'), 'utf8'));
const secrets = parseSecrets(rawSecrets);
return configSchema.parseAsync(interpolateSecrets(rawConfig, secrets));
return configSchema.parse(interpolateSecrets(rawConfig, secrets));
});

if (!goLoadConfig.success) throw new Error(`Unable to load configuration.`, { cause: goLoadConfig.error });
Expand Down
54 changes: 54 additions & 0 deletions test/readme.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import * as nodeFs from 'node:fs';
import path from 'node:path';

import { isObject } from 'lodash';
import { z } from 'zod';

import { loadConfig } from '../src/config';

const actualFs = jest.requireActual<typeof import('node:fs')>('node:fs');

/*
* Recursively extracts keys from an object.
*/
const extractKeys = (item: any): string[] =>
isObject(item) ? [...Object.keys(item), ...Object.values(item).flatMap((element) => extractKeys(element))] : [];

jest.mock('node:fs');

describe('checks README', () => {
it('checks that the README contains all configuration keys in airseeker.example.json', () => {
const exampleConfigData = actualFs.readFileSync(path.join(__dirname, '../config/airseeker.example.json'));
const exampleSecretsData = actualFs.readFileSync(path.join(__dirname, '../config/secrets.example.env'));
const readmeData = actualFs.readFileSync(path.join(__dirname, '../README.md')).toString();

expect(exampleConfigData).toBeDefined();
expect(exampleSecretsData).toBeDefined();
expect(readmeData).toBeDefined();

const readFileSyncSpy = jest.spyOn(nodeFs, 'readFileSync');

readFileSyncSpy.mockImplementationOnce(() => exampleSecretsData);
readFileSyncSpy.mockImplementationOnce(() => exampleConfigData);

const readmeHashtagTitles = readmeData
.split('\n')
.map((line) => /^#+ `(?<title>.+)`/.exec(line)?.groups?.title)
.filter((title) => !!title);
const readmeBacktickTitles = readmeData
.split('\n')
.map((line) => /^`(?<title>[^`]+)`/.exec(line)?.groups?.title)
.filter((title) => !!title);
const readmeTitles = [...readmeHashtagTitles, ...readmeBacktickTitles];

const config = loadConfig();
expect(config).toBeDefined();

aquarat marked this conversation as resolved.
Show resolved Hide resolved
const missingKeys = extractKeys(config)
.filter((item) => !z.coerce.number().safeParse(item).success)
.filter((item) => item !== 'hardhat')
.filter((key) => !readmeTitles.some((title) => title?.includes(key)));

expect(missingKeys).toHaveLength(0);
});
});