Skip to content

Commit 5f7f34b

Browse files
author
Mariano Goldman
committed
tests
1 parent 32d9a04 commit 5f7f34b

File tree

4 files changed

+51
-22
lines changed

4 files changed

+51
-22
lines changed

src/client/ContentClient.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import { hashV0, hashV1 } from '@dcl/hashing'
22
import { Entity } from '@dcl/schemas'
3-
import { IFetchComponent, RequestOptions } from '@well-known-components/interfaces'
3+
import { IFetchComponent, RequestOptions as OriginalRequestOptions } from '@well-known-components/interfaces'
44
import FormData from 'form-data'
55
import { ClientOptions, DeploymentData } from './types'
66
import { addModelToFormData, isNode, mergeRequestOptions, sanitizeUrl, splitAndFetch } from './utils/Helper'
77
import { retry } from './utils/retry'
88
import { Response } from '@well-known-components/interfaces/dist/components/fetcher'
99

10+
export type RequestOptions = OriginalRequestOptions & {
11+
deploymentProtocolVersion?: 'v1' | 'v2'
12+
}
13+
1014
function arrayBufferFrom(value: Buffer | Uint8Array) {
1115
if (value.buffer) {
1216
return value.buffer
@@ -70,7 +74,6 @@ async function supportsDeploymentsV2(serverBaseUrl: string): Promise<boolean> {
7074
const response = await fetch(`${serverBaseUrl}/v2/entities/:entityId/files/:fileHash`, { method: 'OPTIONS' })
7175
return response.ok // returns true if response status is 200-299
7276
} catch (error) {
73-
console.log(error)
7477
console.error(`Error: ${error}`)
7578
return false
7679
}
@@ -140,19 +143,19 @@ export function createContentClient(options: ClientOptions): ContentClient {
140143
}
141144

142145
async function deploy(deployData: DeploymentData, options?: RequestOptions): Promise<unknown> {
143-
// TODO We could also check the deployment size (if too small, may not be worth to use V2)
144-
// One file is always the entity itself, so we check more than one for using v2
145-
if (deployData.files.size > 1) {
146+
if (options?.deploymentProtocolVersion === 'v2') {
146147
const supportsV2 = await supportsDeploymentsV2(contentUrl)
147148
if (supportsV2) {
148149
return deployV2(deployData, options)
150+
} else {
151+
throw new Error('The server does not support deployments v2.')
149152
}
150153
}
151154

152-
return deployTraditional(deployData, options)
155+
return deployV1(deployData, options)
153156
}
154157

155-
async function deployTraditional(deployData: DeploymentData, options?: RequestOptions): Promise<unknown> {
158+
async function deployV1(deployData: DeploymentData, options?: RequestOptions): Promise<unknown> {
156159
const form = await buildEntityFormDataForDeployment(deployData, options)
157160

158161
const requestOptions = mergeRequestOptions(options ? options : {}, {
@@ -164,7 +167,6 @@ export function createContentClient(options: ClientOptions): ContentClient {
164167
}
165168

166169
async function deployV2(deployData: DeploymentData, options?: RequestOptions): Promise<unknown> {
167-
console.log('deployData', deployData)
168170
const areWeRunningInNode = isNode()
169171

170172
const fileSizesManifest: Record<string, number> = Object.fromEntries(
@@ -183,7 +185,6 @@ export function createContentClient(options: ClientOptions): ContentClient {
183185
formData.append('fileSizesManifest', JSON.stringify(fileSizesManifest), {
184186
contentType: 'application/json'
185187
})
186-
console.log('form', formData)
187188

188189
const requestOptions = mergeRequestOptions(options ? options : {}, {
189190
body: formData as any,
@@ -194,14 +195,11 @@ export function createContentClient(options: ClientOptions): ContentClient {
194195
if (!response.ok) {
195196
throw new Error(`Failed to deploy entity with id '${deployData.entityId}'.`)
196197
}
197-
console.log('Deployment started successfully! Uploading files...')
198198

199199
const res = await response.json()
200-
console.log('res', res)
201200

202201
const fileUploadRequests = await buildFileUploadRequestsForDeploymentV2(deployData, res.missingFiles)
203202
await Promise.all(fileUploadRequests.map((request) => request()))
204-
console.log('Files uploaded successfully! Finishing deployment...')
205203

206204
const response2 = await fetcher.fetch(`${contentUrl}/v2/entities/${deployData.entityId}`, {
207205
method: 'PUT',

test/components.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
import { createConfigComponent } from '@well-known-components/env-config-provider'
22
import { createServerComponent, Router } from '@well-known-components/http-server'
3-
import { IConfigComponent, IHttpServerComponent, ILoggerComponent, Lifecycle } from '@well-known-components/interfaces'
3+
import {
4+
IConfigComponent,
5+
IHttpServerComponent,
6+
ILoggerComponent,
7+
IMiddlewareAdapterHandler,
8+
Lifecycle
9+
} from '@well-known-components/interfaces'
410
import { createLogComponent } from '@well-known-components/logger'
511
import { createRunner, defaultServerConfig } from '@well-known-components/test-helpers'
612
import * as util from 'util'
713
import DefaultContext = IHttpServerComponent.DefaultContext
814

9-
const logRequestMiddleware = async function logger(
10-
ctx: DefaultContext<AppContext>,
11-
next: () => Promise<IHttpServerComponent.IResponse>
12-
) {
15+
const logRequestMiddleware: IMiddlewareAdapterHandler<
16+
DefaultContext<AppContext>,
17+
IHttpServerComponent.IResponse
18+
> = async function logger(ctx: DefaultContext<AppContext>, next: () => Promise<IHttpServerComponent.IResponse>) {
1319
const headers: Record<string, string> = {}
1420

1521
for (const [header, value] of ctx.request.headers) {

test/integration/ContentClient.E2E.spec.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,26 @@ runServerBasedE2ETest('test client post', ({ components }) => {
5252
files.set('QmA', new Uint8Array([111, 112, 113]))
5353
files.set('QmB', Buffer.from('asd', 'utf-8'))
5454

55-
await client.deploy({ authChain: [], entityId: 'QmENTITY', files })
55+
await client.deploy(
56+
{ authChain: [], entityId: 'QmENTITY', files },
57+
{
58+
deploymentProtocolVersion: 'v1'
59+
}
60+
)
61+
})
62+
63+
it('fails to publish an entity using v2 if the server does not support v2', async () => {
64+
const files = new Map<string, Uint8Array>()
65+
files.set('QmA', new Uint8Array([111, 112, 113]))
66+
files.set('QmB', Buffer.from('asd', 'utf-8'))
67+
68+
await expect(() =>
69+
client.deploy(
70+
{ authChain: [], entityId: 'QmENTITY', files },
71+
{
72+
deploymentProtocolVersion: 'v2'
73+
}
74+
)
75+
).rejects.toThrowError('The server does not support deployments v2.')
5676
})
5777
})

test/integration/deployment-v2.spec.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,15 @@ runServerBasedE2ETest('test deployment v2 protocol', ({ components }) => {
118118
})
119119

120120
test('publishes an entity', async () => {
121-
const res = await client.deploy({
122-
...preparationData,
123-
authChain: [{ type: AuthLinkType.SIGNER, payload: '0x1', signature: '' }]
124-
})
121+
const res = await client.deploy(
122+
{
123+
...preparationData,
124+
authChain: [{ type: AuthLinkType.SIGNER, payload: '0x1', signature: '' }]
125+
},
126+
{
127+
deploymentProtocolVersion: 'v2'
128+
}
129+
)
125130

126131
expect(res).toBe(true)
127132
expect(stage).toBe(5)

0 commit comments

Comments
 (0)