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

Fix import error #110

Merged
merged 1 commit into from
Sep 9, 2024
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
"license": "GPL-3.0",
"devDependencies": {
"typescript": "latest",
"vitest": "^1.2.2"
"vitest": "^2.0.5"
},
"dependencies": {
"@types/node": "^20.11.10",
"json5": "2.2.3",
"prettier": "3.2.4",
"prettier": "3.3.3",
"rosetta": "1.1.0"
},
"repository": {
Expand Down
11 changes: 9 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ import { OperatorCode, OutputType, Kind } from './types.js'
export const DEFAULT_OPERATOR = OperatorCode.ArrayCount
export const DEFAULT_INPUT_TYPE = OutputType.Array
export const DEFAULT_SCRIPT_FIRST_TYPE = OutputType.String
export const KIND_OPTIONS = [Kind.HttpGet, Kind.RNG, Kind.HttpPost]
export const KIND_OPTIONS = {
[Kind.HttpGet]: 'HTTP-GET',
[Kind.HttpPost]: 'HTTP-POST',
[Kind.RNG]: 'RNG',
// TODO: Support HTTP-HEAD
// [Kind.HttpHead]: 'HTTP-HEAD',
}
export const CONTENT_TYPE_OPTIONS = {
[Kind.HttpGet]: 'JSON API',
[Kind.HttpHead]: 'JSON API',
[Kind.HttpPost]: 'JSON API',
[Kind.RNG]: 'Binary file',
// TODO: Support HTTP-HEAD
// [Kind.HttpHead]: 'JSON API',
}
export const DEFAULT_KIND_OPTION = Kind.HttpGet
5 changes: 2 additions & 3 deletions src/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import {
MirSource,
OutputType,
Context,
KindOptions,
Kind,
EventEmitter,
ContentTypeOptions,
KindOptions,
} from './types.js'
import { KIND_OPTIONS, CONTENT_TYPE_OPTIONS } from './constants.js'
import { Cache } from './structures.js'
Expand All @@ -19,7 +18,7 @@ export class Source {
public kindOptions: KindOptions
public url: string
public contentType: string
public contentTypeOptions: ContentTypeOptions
public contentTypeOptions: KindOptions
public headers: OutgoingHttpHeaders
public body?: object
public script: Script
Expand Down
22 changes: 9 additions & 13 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,16 @@ export type MarkupArgumentScript = {
hierarchicalType: MarkupHierarchicalType.Argument
}

export type KindOptions = Record<Kind, string>

export type MarkupSource = {
kind: Kind
kindOptions: KindOptions
headers: OutgoingHttpHeaders
body?: object
url: string
contentType: string
contentTypeOptions: ContentTypeOptions
contentTypeOptions: KindOptions
script: MarkupScript
scriptId: number
}
Expand All @@ -216,13 +218,12 @@ export type Markup = {
radRequest: MarkupRequest
}

export type KindOptions = Array<Kind>

export enum Kind {
HttpGet = 'HTTP-GET',
HttpPost = 'HTTP-POST',
HttpHead = 'HTTP-HEAD',
RNG = 'RNG',
HttpGet = 0x01,
HttpPost = 0x02,
RNG = 0x03,
// TODO: suppport Http Head requests
// HttpHead = 0x04,
}

export enum OperatorCode {
Expand Down Expand Up @@ -378,15 +379,10 @@ export type MirSource = {
kindOptions: KindOptions
url: string
contentType: string
contentTypeOptions: ContentTypeOptions
contentTypeOptions: KindOptions
script: MirScript
}

export type ContentTypeOptions = {
[Kind.HttpGet]: string | Array<string>
[Kind.RNG]: string
}

export type MirRequest = {
timelock: number
retrieve: Array<MirSource>
Expand Down
10 changes: 8 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as prettier from 'prettier'
import { format } from 'prettier/standalone'
import * as prettierPluginBabel from 'prettier/plugins/babel'
import prettierPluginEstree from 'prettier/plugins/estree'
import {
ArgumentInfo,
MarkupInputType,
Expand Down Expand Up @@ -57,7 +59,11 @@ export function fromOutputTypeToType(type: OutputType): Type | null {
}

export async function formatJs(source: string): Promise<string> {
return prettier.format(source, { semi: false, parser: 'babel' })
return await format(source, {
semi: false,
parser: 'babel',
plugins: [prettierPluginBabel, prettierPluginEstree],
})
}

export function getEnumNames(e: any): Array<any> {
Expand Down
42 changes: 21 additions & 21 deletions test/src/createRngRequest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('RandomNumberGenerator request', () => {
kindOptions: KIND_OPTIONS,
url: 'source_1',
headers: {},
contentType: 'JSON API',
contentType: CONTENT_TYPE_OPTIONS[DEFAULT_KIND_OPTION],
contentTypeOptions: CONTENT_TYPE_OPTIONS,
script: [OperatorCode.StringAsBoolean, OperatorCode.BooleanNegate],
},
Expand All @@ -31,7 +31,7 @@ describe('RandomNumberGenerator request', () => {
kindOptions: KIND_OPTIONS,
url: 'source_2',
headers: {},
contentType: 'JSON API',
contentType: CONTENT_TYPE_OPTIONS[DEFAULT_KIND_OPTION],
contentTypeOptions: CONTENT_TYPE_OPTIONS,
script: [OperatorCode.StringAsBoolean, OperatorCode.BooleanNegate],
},
Expand All @@ -55,7 +55,7 @@ describe('RandomNumberGenerator request', () => {
kindOptions: KIND_OPTIONS,
url: 'source_1',
headers: {},
contentType: 'JSON API',
contentType: CONTENT_TYPE_OPTIONS[Kind.RNG],
contentTypeOptions: CONTENT_TYPE_OPTIONS,
script: [],
},
Expand Down Expand Up @@ -90,7 +90,7 @@ describe('RandomNumberGenerator request', () => {
radon.retrieve[0].update({
kind: Kind.RNG,
url: '',
contentType: Kind.RNG,
contentType: CONTENT_TYPE_OPTIONS[Kind.RNG],
})
expect(radon.retrieve[0].script.operators.length).toBe(0)
})
Expand All @@ -100,7 +100,7 @@ describe('RandomNumberGenerator request', () => {
radon.retrieve[0].update({
kind: Kind.RNG,
url: '',
contentType: Kind.RNG,
contentType: CONTENT_TYPE_OPTIONS[Kind.RNG],
})
expect(radon.aggregate.filters.length).toBe(0)
})
Expand All @@ -110,7 +110,7 @@ describe('RandomNumberGenerator request', () => {
radon.retrieve[0].update({
kind: Kind.RNG,
url: '',
contentType: Kind.RNG,
contentType: CONTENT_TYPE_OPTIONS[Kind.RNG],
})
expect(radon.retrieve[0].url).toBe('')
})
Expand All @@ -125,7 +125,7 @@ describe('RandomNumberGenerator request', () => {
kindOptions: KIND_OPTIONS,
url: 'source_1',
headers: {},
contentType: 'JSON API',
contentType: CONTENT_TYPE_OPTIONS[Kind.RNG],
contentTypeOptions: CONTENT_TYPE_OPTIONS,
script: [],
},
Expand Down Expand Up @@ -160,7 +160,7 @@ describe('RandomNumberGenerator request', () => {
radon.retrieve[0].update({
kind: Kind.RNG,
url: '',
contentType: Kind.RNG,
contentType: CONTENT_TYPE_OPTIONS[Kind.RNG],
})
expect(radon.getMarkup().retrieve[0].script.length).toBe(0)
})
Expand All @@ -170,7 +170,7 @@ describe('RandomNumberGenerator request', () => {
radon.retrieve[0].update({
kind: Kind.RNG,
url: '',
contentType: Kind.RNG,
contentType: CONTENT_TYPE_OPTIONS[Kind.RNG],
})
expect(radon.getMarkup().aggregate.filters.length).toBe(0)
})
Expand All @@ -180,7 +180,7 @@ describe('RandomNumberGenerator request', () => {
radon.retrieve[0].update({
kind: Kind.RNG,
url: '',
contentType: Kind.RNG,
contentType: CONTENT_TYPE_OPTIONS[Kind.RNG],
})
expect(radon.getMarkup().retrieve[0].url).toBe('')
})
Expand All @@ -195,7 +195,7 @@ describe('RandomNumberGenerator request', () => {
kindOptions: KIND_OPTIONS,
url: 'source_1',
headers: {},
contentType: 'JSON API',
contentType: CONTENT_TYPE_OPTIONS[Kind.RNG],
contentTypeOptions: CONTENT_TYPE_OPTIONS,
script: [],
},
Expand Down Expand Up @@ -224,7 +224,7 @@ describe('RandomNumberGenerator request', () => {
kindOptions: KIND_OPTIONS,
url: 'source_1',
headers: {},
contentType: 'JSON API',
contentType: CONTENT_TYPE_OPTIONS[Kind.RNG],
contentTypeOptions: CONTENT_TYPE_OPTIONS,
script: [],
},
Expand All @@ -233,7 +233,7 @@ describe('RandomNumberGenerator request', () => {
kindOptions: KIND_OPTIONS,
url: 'source_1',
headers: {},
contentType: 'JSON API',
contentType: CONTENT_TYPE_OPTIONS[Kind.RNG],
contentTypeOptions: CONTENT_TYPE_OPTIONS,
script: [],
},
Expand All @@ -252,9 +252,9 @@ describe('RandomNumberGenerator request', () => {
radon.retrieve[0].update({
kind: Kind.HttpGet,
url: '',
contentType: 'JSON API',
contentType: CONTENT_TYPE_OPTIONS[Kind.HttpGet],
})
expect(radon.getMir().retrieve[0].kind).toBe('HTTP-GET')
expect(radon.getMir().retrieve[0].kind).toBe(1)
})
it('creates an aggregation/tally script with RNG source', () => {
const mirScript: MirAggregationTallyScript = {
Expand All @@ -271,7 +271,7 @@ describe('RandomNumberGenerator request', () => {
radon.retrieve[0].update({
kind: Kind.RNG,
url: '',
contentType: Kind.RNG,
contentType: CONTENT_TYPE_OPTIONS[Kind.RNG],
})
expect(radon.getMir().retrieve[0].script.length).toBe(0)
})
Expand All @@ -281,7 +281,7 @@ describe('RandomNumberGenerator request', () => {
radon.retrieve[0].update({
kind: Kind.RNG,
url: '',
contentType: Kind.RNG,
contentType: CONTENT_TYPE_OPTIONS[Kind.RNG],
})
expect(radon.getMir().retrieve.length).toBe(1)
})
Expand All @@ -291,7 +291,7 @@ describe('RandomNumberGenerator request', () => {
radon.retrieve[0].update({
kind: Kind.RNG,
url: '',
contentType: Kind.RNG,
contentType: CONTENT_TYPE_OPTIONS[Kind.RNG],
})
expect(radon.getMir().aggregate.filters.length).toBe(0)
})
Expand All @@ -301,7 +301,7 @@ describe('RandomNumberGenerator request', () => {
radon.retrieve[0].update({
kind: Kind.RNG,
url: '',
contentType: Kind.RNG,
contentType: CONTENT_TYPE_OPTIONS[Kind.RNG],
})
expect(radon.getMir().retrieve[0].url).toBe('')
})
Expand All @@ -316,7 +316,7 @@ describe('RandomNumberGenerator request', () => {
kindOptions: KIND_OPTIONS,
url: 'source_1',
headers: {},
contentType: 'RNG',
contentType: CONTENT_TYPE_OPTIONS[Kind.RNG],
contentTypeOptions: CONTENT_TYPE_OPTIONS,
script: [],
},
Expand Down Expand Up @@ -354,7 +354,7 @@ describe('RandomNumberGenerator request', () => {
radon.retrieve[0].update({
kind: Kind.RNG,
url: '',
contentType: Kind.RNG,
contentType: CONTENT_TYPE_OPTIONS[Kind.RNG],
})
const expected = await formatJsTest(`import * as Witnet from "witnet-requests"
const request = new Witnet.Request()
Expand Down
47 changes: 45 additions & 2 deletions test/src/radon.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3610,10 +3610,53 @@ describe('Radon', () => {
}

const radon = new Radon(mirRequest)
radon.updateSource(0, { kind: 'new_kind', url: 'new_url' })
radon.updateSource(0, { kind: 2, url: 'new_url' })
const updatedSource = radon.retrieve[0]
expect(updatedSource.url).toBe('new_url')
expect(updatedSource.kind).toBe('new_kind')
expect(updatedSource.kind).toBe(2)
expect(updatedSource.contentType).toBe('JSON API')
})

it('update to rng protocol kind', () => {
const mirRequest: MirRequest = {
timelock: 0,
retrieve: [
{
kind: DEFAULT_KIND_OPTION,
kindOptions: KIND_OPTIONS,
url: 'source_1',
headers: {},
contentType: 'JSON API',
contentTypeOptions: CONTENT_TYPE_OPTIONS,
script: [OperatorCode.StringAsBoolean, OperatorCode.BooleanNegate],
},
{
kind: DEFAULT_KIND_OPTION,
kindOptions: KIND_OPTIONS,
url: 'source_2',
headers: {},
contentType: 'JSON API',
contentTypeOptions: CONTENT_TYPE_OPTIONS,
script: [OperatorCode.StringAsBoolean, OperatorCode.BooleanNegate],
},
],
aggregate: {
filters: [AggregationTallyFilter.mode, [AggregationTallyFilter.deviationStandard, 1.1]],
reducer: AggregationTallyReducer.mode,
},
tally: {
filters: [AggregationTallyFilter.mode, [AggregationTallyFilter.deviationStandard, 1.1]],
reducer: AggregationTallyReducer.mode,
},
}

const radon = new Radon(mirRequest)
expect(radon.retrieve.length).toBe(2)
radon.updateSource(1, { kind: 3 })
const updatedSource = radon.retrieve[0]
expect(updatedSource.kind).toBe(3)
expect(updatedSource.contentType).toBe('Binary file')
expect(radon.retrieve.length).toBe(1)
})
})

Expand Down
Loading
Loading