From 3b4609bed11f2dce6c5ab632824b89a82c0af9e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Ciccola?= Date: Mon, 7 Aug 2023 17:01:22 -0300 Subject: [PATCH 01/23] feat: add tests for valid docs and badDocName --- test/fixture.js | 136 ++++++++++++++++++++++++++++++++++++++ test/index.js | 171 ++++++++++++++++++++++++++---------------------- 2 files changed, 229 insertions(+), 78 deletions(-) create mode 100644 test/fixture.js diff --git a/test/fixture.js b/test/fixture.js new file mode 100644 index 00000000..ec199cfe --- /dev/null +++ b/test/fixture.js @@ -0,0 +1,136 @@ +import { randomBytes } from 'node:crypto' + +export const fixtures = { + onlyId: { + text: 'test encoding of record with missing fields', + plan: 1, + doc: {id: randomBytes(32).toString('hex')} + }, + badDocName: { + text: 'test encoding of wrong record type', + plan: 1, + doc: { + id: randomBytes(32).toString('hex'), + schemaName: 'doesnotexist', + links: [], + createdAt: new Date().toJSON(), + refs: [], + attachments: [], + metadata: { + manual_location: true, + }, + } + }, + goodDocs:{ + text: 'test encoding of rightfully formated records', + plan: 1, + docs: { + observation:{ + docId: randomBytes(32).toString('hex'), + versionId: `${randomBytes(32).toString('hex')}/0`, + schemaName: 'observation', + createdAt: new Date().toJSON(), + updatedAt: new Date().toJSON(), + links: [], + lat: 10, + lon: -20.3, + refs: [], + attachments: [], + metadata: { + manualLocation: true, + }, + tags: {} + }, + project: { + docId: randomBytes(32).toString('hex'), + versionId: `${randomBytes(32).toString('hex')}/0`, + schemaName: 'project', + createdAt: new Date().toJSON(), + updatedAt: new Date().toJSON(), + links: [], + name: 'myProject', + defaultPresets: { + point: ["myPointPreset"], + vertex: ["myVertexPreset"], + area: ["myAreaPreset"], + line: ["myLinePreset"], + relation: ["myRelationPreset"], + } + }, + field: { + docId: randomBytes(32).toString('hex'), + versionId: `${randomBytes(32).toString('hex')}/0`, + schemaName: 'field', + createdAt: new Date().toJSON(), + updatedAt: new Date().toJSON(), + links: [], + tagKey: "myTagKey", + type: "text", + label: "myLabel" + }, + preset: { + docId: randomBytes(32).toString('hex'), + versionId: `${randomBytes(32).toString('hex')}/0`, + schemaName: 'preset', + createdAt: new Date().toJSON(), + updatedAt: new Date().toJSON(), + links: [], + name:"myPreset", + geometry:["point"], + tags:{}, + addTags:{}, + removeTags:{}, + fieldIds:[], + terms:[] + }, + role: { + docId: randomBytes(32).toString('hex'), + versionId: `${randomBytes(32).toString('hex')}/0`, + schemaName: 'role', + createdAt: new Date().toJSON(), + updatedAt: new Date().toJSON(), + links: [], + role:"author", + projectId:randomBytes(32).toString('hex'), + action:"role_set", + signature:"signature", + authorIndex:2, + deviceIndex:4, + authorId:randomBytes(32).toString('hex'), + capabilityType:"capability" + }, + device: { + docId: randomBytes(32).toString('hex'), + versionId: `${randomBytes(32).toString('hex')}/0`, + schemaName: 'device', + createdAt: new Date().toJSON(), + updatedAt: new Date().toJSON(), + links: [], + action:"ban", + authorId:randomBytes(32).toString('hex'), + projectId:randomBytes(32).toString('hex'), + signature:"mySignature", + authorIndex:2, + deviceIndex:20, + capabilityType:"someCapability", + }, + coreOwnership: { + docId: randomBytes(32).toString('hex'), + versionId: `${randomBytes(32).toString('hex')}/0`, + schemaName: 'coreOwnership', + createdAt: new Date().toJSON(), + updatedAt: new Date().toJSON(), + links: [], + action:"remove", + coreId:randomBytes(32).toString('hex'), + projectId:randomBytes(32).toString('hex'), + storeType:"blob", + signature:"mySig", + authorIndex: 10, + deviceIndex: 9, + authorId:randomBytes(32).toString('hex'), + capabilityType: "someCapability" + } + } + } +} diff --git a/test/index.js b/test/index.js index 66ed619e..f798950d 100644 --- a/test/index.js +++ b/test/index.js @@ -1,88 +1,103 @@ import test from 'tape' -import { encode, decode, validate } from '../index.js' -import { randomBytes } from 'node:crypto' -import { docs } from './docs.js' +import { fixtures } from './fixture.js' +import { encode } from '../dist/encode.js' +import { decode } from '../dist/decode.js' +import { validate } from '../dist/validate.js' +import { parseVersionId } from '../dist/lib/utils.js' +// import { randomBytes } from 'node:crypto' -test('test encoding of record with missing fields', async (t) => { - t.plan(1) - t.throws(() => { - encode(docs.onlyId) - }) -}) - -test('test encoding of wrong record type', async (t) => { - t.plan(1) - t.throws(() => { - encode(docs.badDocType) - }) -}) - -test('test encoding of record with wrong schema version', async (t) => { - t.plan(1) - t.throws(() => { - encode(docs.badSchemaVersion) +{ + const {text,plan,doc} = fixtures.onlyId + test(text, async(t) => { + t.plan(plan) + t.throws(() => { + encode(doc) + }) }) -}) - -test('test encoding of rightfully formated record', async (t) => { - const goodDocs = Object.keys(docs.good) - t.plan(goodDocs.length) - goodDocs.forEach((k) => { - const doc = docs.good[k] - t.doesNotThrow(() => { +} +{ + const {text,plan, doc} = fixtures.badDocName + test(text,async(t) => { + t.plan(plan) + t.throws(() => { encode(doc) - }, `testing ${k}`) + }) }) -}) - -test('test validation of record', async (t) => { - const goodDocs = Object.keys(docs.good) - t.plan(goodDocs.length) - goodDocs.forEach((k) => { - const doc = docs.good[k] - const record = decode(encode(doc), { - coreKey: randomBytes(32), - index: 0, +} +{ + const {text,docs} = fixtures.goodDocs + test({text},async(t) => { + t.plan(Object.keys(docs).length) + Object.keys(docs).forEach((docName) => { + const doc = docs[docName] + t.doesNotThrow(() => { + const buf = encode(doc) + console.log(decode(buf, parseVersionId(doc.versionId))) + }, `testing ${docName}`) }) - t.doesNotThrow(() => { - // field has a type which is different from the rest :| - if (k !== 'field') validate(record) - }, `testing validation of ${k}`) }) -}) -test('test encoding, decoding of record and comparing the two versions', async (t) => { - const goodDocs = Object.keys(docs.good) - goodDocs.forEach((k) => { - const doc = docs.good[k] - const record = decode(encode(doc), { - coreKey: randomBytes(32), - index: 0, - }) - const fields = Object.keys(doc) - // t.plan(goodDocs.length * fields.length * 2) - fields.forEach((f) => { - console.log(typeof record[f], typeof doc[f]) - const msg = `checking ${f} for ${k}` - record[f] && doc[f] ? t.pass(msg) : t.fail(msg) +} - // if field is not an object, check equality - // since objects as fields usually mean the possibility of additionalFields in jsonSchemas - if (typeof record[f] !== 'object') { - t.equal(record[f], doc[f], `comparing value of ${f} for ${k}`) - } - }) - }) -}) +// test('test encoding of rightfully formated record', async (t) => { +// const goodDocs = Object.keys(docs.good) +// t.plan(goodDocs.length) +// goodDocs.forEach((k) => { +// const doc = docs.good[k] +// t.doesNotThrow(() => { +// encode(doc) +// }, `testing ${k}`) +// }) +// }) -test('test decoding of record without passing core key and index', async (t) => { - const goodDocs = Object.keys(docs.good) - t.plan(goodDocs.length) - goodDocs.forEach((key) => { - const doc = docs.good[key] - const record = encode(doc) - t.throws(() => { - decode(record) - }, `testing ${key}`) - }) -}) +// test('test validation of record', async (t) => { +// const goodDocs = Object.keys(docs.good) +// t.plan(goodDocs.length) +// goodDocs.forEach((k) => { +// const doc = docs.good[k] +// const record = decode(encode(doc), { +// coreKey: randomBytes(32), +// index: 0, +// }) +// t.doesNotThrow(() => { +// // field has a type which is different from the rest :| +// if (k !== 'field') validate(record) +// }, `testing validation of ${k}`) +// }) +// }) + +// test('test encoding, decoding of record and comparing the two versions', async (t) => { +// const goodDocs = Object.keys(docs.good) +// goodDocs.forEach((k) => { +// const doc = docs.good[k] +// const record = decode(encode(doc), { +// coreKey: randomBytes(32), +// index: 0, +// }) +// const fields = Object.keys(doc) +// // t.plan(goodDocs.length * fields.length * 2) +// fields.forEach((f) => { +// console.log(typeof record[f], typeof doc[f]) +// const msg = `checking ${f} for ${k}` +// record[f] && doc[f] ? t.pass(msg) : t.fail(msg) + +// // if field is not an object, check equality +// // since objects as fields usually mean the possibility of additionalFields in jsonSchemas +// if (typeof record[f] !== 'object') { +// t.equal(record[f], doc[f], `comparing value of ${f} for ${k}`) +// } +// }) +// }) +// }) + +// test('test decoding of record without passing core key and index', async (t) => { +// const goodDocs = Object.keys(docs.good) +// t.plan(goodDocs.length) +// goodDocs.forEach((key) => { +// const doc = docs.good[key] +// const record = encode(doc) +// t.throws(() => { +// decode(record) +// }, `testing ${key}`) +// }) +// }) From 247ae91c0a7a47851ce6f80903a98122ddfbb28d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Ciccola?= Date: Wed, 9 Aug 2023 15:13:00 -0300 Subject: [PATCH 02/23] feat: added test for deep comparison of fields of doc with decodedDoc --- test/fixture.js | 217 ++++++++++++++++++++++++------------------------ test/index.js | 40 +++++++-- 2 files changed, 138 insertions(+), 119 deletions(-) diff --git a/test/fixture.js b/test/fixture.js index ec199cfe..6337a75b 100644 --- a/test/fixture.js +++ b/test/fixture.js @@ -1,5 +1,6 @@ import { randomBytes } from 'node:crypto' + export const fixtures = { onlyId: { text: 'test encoding of record with missing fields', @@ -21,116 +22,112 @@ export const fixtures = { }, } }, - goodDocs:{ - text: 'test encoding of rightfully formated records', - plan: 1, - docs: { - observation:{ - docId: randomBytes(32).toString('hex'), - versionId: `${randomBytes(32).toString('hex')}/0`, - schemaName: 'observation', - createdAt: new Date().toJSON(), - updatedAt: new Date().toJSON(), - links: [], - lat: 10, - lon: -20.3, - refs: [], - attachments: [], - metadata: { - manualLocation: true, - }, - tags: {} - }, - project: { - docId: randomBytes(32).toString('hex'), - versionId: `${randomBytes(32).toString('hex')}/0`, - schemaName: 'project', - createdAt: new Date().toJSON(), - updatedAt: new Date().toJSON(), - links: [], - name: 'myProject', - defaultPresets: { - point: ["myPointPreset"], - vertex: ["myVertexPreset"], - area: ["myAreaPreset"], - line: ["myLinePreset"], - relation: ["myRelationPreset"], - } - }, - field: { - docId: randomBytes(32).toString('hex'), - versionId: `${randomBytes(32).toString('hex')}/0`, - schemaName: 'field', - createdAt: new Date().toJSON(), - updatedAt: new Date().toJSON(), - links: [], - tagKey: "myTagKey", - type: "text", - label: "myLabel" - }, - preset: { - docId: randomBytes(32).toString('hex'), - versionId: `${randomBytes(32).toString('hex')}/0`, - schemaName: 'preset', - createdAt: new Date().toJSON(), - updatedAt: new Date().toJSON(), - links: [], - name:"myPreset", - geometry:["point"], - tags:{}, - addTags:{}, - removeTags:{}, - fieldIds:[], - terms:[] - }, - role: { - docId: randomBytes(32).toString('hex'), - versionId: `${randomBytes(32).toString('hex')}/0`, - schemaName: 'role', - createdAt: new Date().toJSON(), - updatedAt: new Date().toJSON(), - links: [], - role:"author", - projectId:randomBytes(32).toString('hex'), - action:"role_set", - signature:"signature", - authorIndex:2, - deviceIndex:4, - authorId:randomBytes(32).toString('hex'), - capabilityType:"capability" - }, - device: { - docId: randomBytes(32).toString('hex'), - versionId: `${randomBytes(32).toString('hex')}/0`, - schemaName: 'device', - createdAt: new Date().toJSON(), - updatedAt: new Date().toJSON(), - links: [], - action:"ban", - authorId:randomBytes(32).toString('hex'), - projectId:randomBytes(32).toString('hex'), - signature:"mySignature", - authorIndex:2, - deviceIndex:20, - capabilityType:"someCapability", - }, - coreOwnership: { - docId: randomBytes(32).toString('hex'), - versionId: `${randomBytes(32).toString('hex')}/0`, - schemaName: 'coreOwnership', - createdAt: new Date().toJSON(), - updatedAt: new Date().toJSON(), - links: [], - action:"remove", - coreId:randomBytes(32).toString('hex'), - projectId:randomBytes(32).toString('hex'), - storeType:"blob", - signature:"mySig", - authorIndex: 10, - deviceIndex: 9, - authorId:randomBytes(32).toString('hex'), - capabilityType: "someCapability" + /** @type {Array<{ text: string, doc: import('../dist/types').MapeoDoc}>} */ + goodDocs:[{ + docId: randomBytes(32).toString('hex'), + versionId: `${randomBytes(32).toString('hex')}/0`, + schemaName: 'observation', + createdAt: new Date().toJSON(), + updatedAt: new Date().toJSON(), + links: [], + lat: 10, + lon: -20.39090, + refs: [], + attachments: [], + metadata: { + manualLocation: true, + }, + tags: {} + }, + { + docId: randomBytes(32).toString('hex'), + versionId: `${randomBytes(32).toString('hex')}/0`, + schemaName: 'project', + createdAt: new Date().toJSON(), + updatedAt: new Date().toJSON(), + links: [], + name: 'myProject', + defaultPresets: { + point: [randomBytes(32).toString('hex')], + vertex: [randomBytes(32).toString('hex')], + area: [randomBytes(32).toString('hex')], + line: [randomBytes(32).toString('hex')], + relation: [randomBytes(32).toString('hex')], } + }, + { + docId: randomBytes(32).toString('hex'), + versionId: `${randomBytes(32).toString('hex')}/0`, + schemaName: 'field', + createdAt: new Date().toJSON(), + updatedAt: new Date().toJSON(), + links: [], + tagKey: "myTagKey", + type: "text", + label: "myLabel" + }, + { + docId: randomBytes(32).toString('hex'), + versionId: `${randomBytes(32).toString('hex')}/0`, + schemaName: 'preset', + createdAt: new Date().toJSON(), + updatedAt: new Date().toJSON(), + links: [], + name:"myPreset", + geometry:["point"], + tags:{}, + addTags:{}, + removeTags:{}, + fieldIds:[], + terms:[] + }, + { + docId: randomBytes(32).toString('hex'), + versionId: `${randomBytes(32).toString('hex')}/0`, + schemaName: 'role', + createdAt: new Date().toJSON(), + updatedAt: new Date().toJSON(), + links: [], + role:"author", + projectId:randomBytes(32).toString('hex'), + action:"role_set", + signature:"signature", + authorIndex:2, + deviceIndex:4, + authorId:randomBytes(32).toString('hex'), + capabilityType:"capability" + }, + { + docId: randomBytes(32).toString('hex'), + versionId: `${randomBytes(32).toString('hex')}/0`, + schemaName: 'device', + createdAt: new Date().toJSON(), + updatedAt: new Date().toJSON(), + links: [], + action:"ban", + authorId:randomBytes(32).toString('hex'), + projectId:randomBytes(32).toString('hex'), + signature:"mySignature", + authorIndex:2, + deviceIndex:20, + capabilityType:"someCapability", + }, + { + docId: randomBytes(32).toString('hex'), + versionId: `${randomBytes(32).toString('hex')}/0`, + schemaName: 'coreOwnership', + createdAt: new Date().toJSON(), + updatedAt: new Date().toJSON(), + links: [], + action:"remove", + coreId:randomBytes(32).toString('hex'), + projectId:randomBytes(32).toString('hex'), + storeType:"blob", + signature:"mySig", + authorIndex: 10, + deviceIndex: 9, + authorId:randomBytes(32).toString('hex'), + capabilityType: "someCapability" } - } + ] } diff --git a/test/index.js b/test/index.js index f798950d..642453e2 100644 --- a/test/index.js +++ b/test/index.js @@ -2,7 +2,6 @@ import test from 'tape' import { fixtures } from './fixture.js' import { encode } from '../dist/encode.js' import { decode } from '../dist/decode.js' -import { validate } from '../dist/validate.js' import { parseVersionId } from '../dist/lib/utils.js' // import { randomBytes } from 'node:crypto' @@ -25,18 +24,37 @@ import { parseVersionId } from '../dist/lib/utils.js' }) } { - const {text,docs} = fixtures.goodDocs - test({text},async(t) => { - t.plan(Object.keys(docs).length) - Object.keys(docs).forEach((docName) => { - const doc = docs[docName] + test('testing rightfully encoding of doc',async(t) => { + fixtures.goodDocs.forEach( + /** @param {import('../dist/types').MapeoDoc} doc */ + (doc) => { t.doesNotThrow(() => { - const buf = encode(doc) - console.log(decode(buf, parseVersionId(doc.versionId))) - }, `testing ${docName}`) + encode(doc) + }, `testing ${doc.schemaName}`) }) }) +} +{ + test('testing encoding of doc, then decoding and comparing the two objects',async(t) => { + fixtures.goodDocs.forEach( + /** @param {import('../dist/types').MapeoDoc} doc */ + (doc) => { + t.doesNotThrow(() => { + const buf = encode(doc) + const decodedDoc = stripUndef(decode(buf, parseVersionId(doc.versionId))) + + Object.keys(doc).forEach(k => { + if(k === 'defaultPresets') { + console.log(doc[k]) + console.log(decodedDoc[k]) + } + t.deepEqual(doc[k],decodedDoc[k], ` - equal? ${k}`) + }) + + }, `tested ${doc.schemaName}`) + }) + }) } // test('test encoding of rightfully formated record', async (t) => { @@ -101,3 +119,7 @@ import { parseVersionId } from '../dist/lib/utils.js' // }, `testing ${key}`) // }) // }) + +function stripUndef(obj) { + return JSON.parse(JSON.stringify(obj)) +} From 5aa150aab6d094aef19b37c3e637e9c329b9bfe4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Ciccola?= Date: Wed, 9 Aug 2023 15:35:13 -0300 Subject: [PATCH 03/23] feat: fix tests for nested objects (one-level) and floats when decoding a protobuf with a float, the number of decimals is not predictable nor it will necessary match the number of decimals on the field we encoded previously... --- test/index.js | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/test/index.js b/test/index.js index 642453e2..a34cdb5a 100644 --- a/test/index.js +++ b/test/index.js @@ -3,6 +3,7 @@ import { fixtures } from './fixture.js' import { encode } from '../dist/encode.js' import { decode } from '../dist/decode.js' import { parseVersionId } from '../dist/lib/utils.js' +import { and } from 'ajv/dist/compile/codegen/index.js' // import { randomBytes } from 'node:crypto' { @@ -44,14 +45,24 @@ import { parseVersionId } from '../dist/lib/utils.js' const decodedDoc = stripUndef(decode(buf, parseVersionId(doc.versionId))) Object.keys(doc).forEach(k => { - if(k === 'defaultPresets') { - console.log(doc[k]) - console.log(decodedDoc[k]) + // go deeper to the object if its nested (is this necessary?) + if(typeof doc[k] === 'object'){ + Object.keys(doc[k]).forEach(inObjKey => { + const docField = doc[k][inObjKey] + const decDocField = doc[k][inObjKey] + t.deepEqual(docField, decDocField) + }) + // check for decimals in float. + // we strip unnecessary decimal points we get from the decoded value + }else if(typeof doc[k] === 'number'){ + const nDecimals = countDecimals(doc[k]) + const fixedDecValue = Number(decodedDoc[k].toFixed(nDecimals)) + t.deepEqual(doc[k],fixedDecValue, ` - equal? ${k}`) + }else{ + t.deepEqual(doc[k],decodedDoc[k], ` - equal? ${k}`) } - t.deepEqual(doc[k],decodedDoc[k], ` - equal? ${k}`) }) - }, `tested ${doc.schemaName}`) }) }) @@ -123,3 +134,11 @@ import { parseVersionId } from '../dist/lib/utils.js' function stripUndef(obj) { return JSON.parse(JSON.stringify(obj)) } + +/** @param {Number} value + * @returns Number */ +function countDecimals(value) { + return ((value % 1) != 0) + ? Number(value.toString().split(".")[1].length) + : 0; +} From 1358a696c82449954e7c5bc243a8bf8f216f45a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Ciccola?= Date: Wed, 9 Aug 2023 15:45:26 -0300 Subject: [PATCH 04/23] chore: refactor ifelse to switch --- test/index.js | 69 +++++++++++++++++++++++++++------------------------ 1 file changed, 36 insertions(+), 33 deletions(-) diff --git a/test/index.js b/test/index.js index a34cdb5a..6b45fdc2 100644 --- a/test/index.js +++ b/test/index.js @@ -15,6 +15,7 @@ import { and } from 'ajv/dist/compile/codegen/index.js' }) }) } + { const {text,plan, doc} = fixtures.badDocName test(text,async(t) => { @@ -24,49 +25,51 @@ import { and } from 'ajv/dist/compile/codegen/index.js' }) }) } -{ - test('testing rightfully encoding of doc',async(t) => { - fixtures.goodDocs.forEach( - /** @param {import('../dist/types').MapeoDoc} doc */ - (doc) => { - t.doesNotThrow(() => { - encode(doc) - }, `testing ${doc.schemaName}`) - }) + +test('testing rightfully encoding of doc',async(t) => { + fixtures.goodDocs.forEach( + /** @param {import('../dist/types').MapeoDoc} doc */ + (doc) => { + t.doesNotThrow(() => { + encode(doc) + }, `testing ${doc.schemaName}`) }) -} -{ - test('testing encoding of doc, then decoding and comparing the two objects',async(t) => { - fixtures.goodDocs.forEach( - /** @param {import('../dist/types').MapeoDoc} doc */ - (doc) => { - t.doesNotThrow(() => { - const buf = encode(doc) - const decodedDoc = stripUndef(decode(buf, parseVersionId(doc.versionId))) +}) - Object.keys(doc).forEach(k => { - // go deeper to the object if its nested (is this necessary?) - if(typeof doc[k] === 'object'){ +test('testing encoding of doc, then decoding and comparing the two objects',async(t) => { + fixtures.goodDocs.forEach( + /** @param {import('../dist/types').MapeoDoc} doc */ + (doc) => { + t.doesNotThrow(() => { + const buf = encode(doc) + const decodedDoc = stripUndef(decode(buf, parseVersionId(doc.versionId))) + Object.keys(doc).forEach(k => { + const field = doc[k] + switch(typeof field){ + case 'object': { + // go deeper if field is an object Object.keys(doc[k]).forEach(inObjKey => { const docField = doc[k][inObjKey] const decDocField = doc[k][inObjKey] t.deepEqual(docField, decDocField) }) - // check for decimals in float. - // we strip unnecessary decimal points we get from the decoded value - }else if(typeof doc[k] === 'number'){ - const nDecimals = countDecimals(doc[k]) - const fixedDecValue = Number(decodedDoc[k].toFixed(nDecimals)) + } + break + case 'number':{ + // strip unecessary decimals we get from decoding and then compare + let nDecimals = countDecimals(doc[k]) + let fixedDecValue = Number(decodedDoc[k].toFixed(nDecimals)) t.deepEqual(doc[k],fixedDecValue, ` - equal? ${k}`) - }else{ - t.deepEqual(doc[k],decodedDoc[k], ` - equal? ${k}`) + break } - }) + default: + t.deepEqual(doc[k],decodedDoc[k], ` - equal? ${k}`) + } + }) - }, `tested ${doc.schemaName}`) - }) - }) -} + }, `tested ${doc.schemaName}`) + }) +}) // test('test encoding of rightfully formated record', async (t) => { // const goodDocs = Object.keys(docs.good) From a0db8e21322eca4751c775bdb5283c719fab0e32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Ciccola?= Date: Wed, 9 Aug 2023 16:20:45 -0300 Subject: [PATCH 05/23] feat: fix bug in `encode-convertions/convertProject`, refactor tests --- src/lib/encode-converstions.ts | 6 +++--- test/index.js | 33 ++++++++++++--------------------- 2 files changed, 15 insertions(+), 24 deletions(-) diff --git a/src/lib/encode-converstions.ts b/src/lib/encode-converstions.ts index f95e2e28..93e0fc5d 100644 --- a/src/lib/encode-converstions.ts +++ b/src/lib/encode-converstions.ts @@ -28,9 +28,9 @@ export const convertProject: ConvertFunction<'project'> = (mapeoDoc) => { defaultPresets: { point: (mapeoDoc.defaultPresets.point || []).map(p => Buffer.from(p,'hex')), area: (mapeoDoc.defaultPresets.area || []).map(a => Buffer.from(a,'hex')), - vertex: (mapeoDoc.defaultPresets.area || []).map(v => Buffer.from(v,'hex')), - line: (mapeoDoc.defaultPresets.area || []).map(l => Buffer.from(l, 'hex')), - relation: (mapeoDoc.defaultPresets.area || []).map(r => Buffer.from(r, 'hex')) + vertex: (mapeoDoc.defaultPresets.vertex || []).map(v => Buffer.from(v,'hex')), + line: (mapeoDoc.defaultPresets.line || []).map(l => Buffer.from(l, 'hex')), + relation: (mapeoDoc.defaultPresets.relation || []).map(r => Buffer.from(r, 'hex')) } } } diff --git a/test/index.js b/test/index.js index 6b45fdc2..c5cd9de4 100644 --- a/test/index.js +++ b/test/index.js @@ -36,7 +36,7 @@ test('testing rightfully encoding of doc',async(t) => { }) }) -test('testing encoding of doc, then decoding and comparing the two objects',async(t) => { +test('testing encoding of doc, then decoding and comparing the two objects', async(t) => { fixtures.goodDocs.forEach( /** @param {import('../dist/types').MapeoDoc} doc */ (doc) => { @@ -45,25 +45,12 @@ test('testing encoding of doc, then decoding and comparing the two objects',asyn const decodedDoc = stripUndef(decode(buf, parseVersionId(doc.versionId))) Object.keys(doc).forEach(k => { const field = doc[k] - switch(typeof field){ - case 'object': { - // go deeper if field is an object - Object.keys(doc[k]).forEach(inObjKey => { - const docField = doc[k][inObjKey] - const decDocField = doc[k][inObjKey] - t.deepEqual(docField, decDocField) - }) - } - break - case 'number':{ - // strip unecessary decimals we get from decoding and then compare - let nDecimals = countDecimals(doc[k]) - let fixedDecValue = Number(decodedDoc[k].toFixed(nDecimals)) - t.deepEqual(doc[k],fixedDecValue, ` - equal? ${k}`) - break - } - default: - t.deepEqual(doc[k],decodedDoc[k], ` - equal? ${k}`) + if(typeof doc[k] === 'number'){ + const nDecimals = countDecimals(field) + const fixedDecValue = Number(decodedDoc[k].toFixed(nDecimals)) + t.deepEqual(field,fixedDecValue, ` - equal? ${k}`) + }else{ + t.deepEqual(doc[k],decodedDoc[k], ` - equal? ${k}`) } }) @@ -134,12 +121,16 @@ test('testing encoding of doc, then decoding and comparing the two objects',asyn // }) // }) +/** + * @param {object} obj + * @return {object} + * */ function stripUndef(obj) { return JSON.parse(JSON.stringify(obj)) } /** @param {Number} value - * @returns Number */ + * @return Number */ function countDecimals(value) { return ((value % 1) != 0) ? Number(value.toString().split(".")[1].length) From e459b9998bc14ca885b89f7d9d19e4dd6231fef8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Ciccola?= Date: Wed, 9 Aug 2023 17:01:13 -0300 Subject: [PATCH 06/23] feat: observation.proto/{lat,lon}: use double instead of float --- proto/observation/v5.proto | 4 ++-- test/index.js | 18 ++++++++---------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/proto/observation/v5.proto b/proto/observation/v5.proto index f224fdda..9cc2fdc2 100644 --- a/proto/observation/v5.proto +++ b/proto/observation/v5.proto @@ -14,8 +14,8 @@ message Observation_5 { Common_1 common = 1; - optional float lat = 5; - optional float lon = 6; + optional double lat = 5; + optional double lon = 6; message Ref { bytes id = 1; diff --git a/test/index.js b/test/index.js index c5cd9de4..1908c798 100644 --- a/test/index.js +++ b/test/index.js @@ -43,16 +43,14 @@ test('testing encoding of doc, then decoding and comparing the two objects', asy t.doesNotThrow(() => { const buf = encode(doc) const decodedDoc = stripUndef(decode(buf, parseVersionId(doc.versionId))) - Object.keys(doc).forEach(k => { - const field = doc[k] - if(typeof doc[k] === 'number'){ - const nDecimals = countDecimals(field) - const fixedDecValue = Number(decodedDoc[k].toFixed(nDecimals)) - t.deepEqual(field,fixedDecValue, ` - equal? ${k}`) - }else{ - t.deepEqual(doc[k],decodedDoc[k], ` - equal? ${k}`) - } - }) + t.deepEqual(doc,decodedDoc) + // Object.keys(decodedDoc).forEach(k => { + // if(doc.schemaName === 'field'){ + // if(!doc.hasOwnProperty(k)){ + // console.log('missing', k) + // } + // } + // }) }, `tested ${doc.schemaName}`) }) From c075ec0f466e08d2b48af15678fa107784e93262 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Ciccola?= Date: Wed, 9 Aug 2023 17:40:26 -0300 Subject: [PATCH 07/23] add fields with default value to `field` to pass deepEqual test --- test/fixture.js | 6 +++++- test/index.js | 8 -------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/test/fixture.js b/test/fixture.js index 6337a75b..4658328b 100644 --- a/test/fixture.js +++ b/test/fixture.js @@ -64,7 +64,11 @@ export const fixtures = { links: [], tagKey: "myTagKey", type: "text", - label: "myLabel" + label: "myLabel", + universal: true, + options: [], + appearance: 'multiline', + snakeCase: true }, { docId: randomBytes(32).toString('hex'), diff --git a/test/index.js b/test/index.js index 1908c798..504cca44 100644 --- a/test/index.js +++ b/test/index.js @@ -44,14 +44,6 @@ test('testing encoding of doc, then decoding and comparing the two objects', asy const buf = encode(doc) const decodedDoc = stripUndef(decode(buf, parseVersionId(doc.versionId))) t.deepEqual(doc,decodedDoc) - // Object.keys(decodedDoc).forEach(k => { - // if(doc.schemaName === 'field'){ - // if(!doc.hasOwnProperty(k)){ - // console.log('missing', k) - // } - // } - // }) - }, `tested ${doc.schemaName}`) }) }) From e98e928a0d0854d8cad441e80aa4f649c8eb9ca8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Ciccola?= Date: Thu, 10 Aug 2023 16:07:14 -0300 Subject: [PATCH 08/23] feat: add tests for optional values, proto and schema changes --- proto/observation/v5.proto | 12 +- schema/preset/v2.json | 2 +- src/lib/decode-conversions.ts | 1 + src/lib/encode-converstions.ts | 2 +- test/fixture.js | 636 ++++++++++++++++++++++++++++----- test/index.js | 41 ++- 6 files changed, 572 insertions(+), 122 deletions(-) diff --git a/proto/observation/v5.proto b/proto/observation/v5.proto index 9cc2fdc2..1c14e480 100644 --- a/proto/observation/v5.proto +++ b/proto/observation/v5.proto @@ -47,12 +47,12 @@ message Observation_5 { bool mocked = 2; message Coords { - float latitude = 1; - float longitude = 2; - float altitude = 3; - float heading = 4; - float speed = 5; - float acurracy = 6; + double latitude = 1; + double longitude = 2; + double altitude = 3; + double heading = 4; + double speed = 5; + double acurracy = 6; } optional Coords coords = 3; } diff --git a/schema/preset/v2.json b/schema/preset/v2.json index 2852c2d3..d44676bc 100644 --- a/schema/preset/v2.json +++ b/schema/preset/v2.json @@ -83,7 +83,7 @@ "type": "string" } }, - "icon": { + "iconId": { "description": "hex-encoded string. ID of preset icon which represents this preset", "type": "string" }, diff --git a/src/lib/decode-conversions.ts b/src/lib/decode-conversions.ts index f48e3f12..96bbafa9 100644 --- a/src/lib/decode-conversions.ts +++ b/src/lib/decode-conversions.ts @@ -114,6 +114,7 @@ export const convertPreset: ConvertFunction<'preset'> = ( ...jsonSchemaCommon, ...rest, geometry, + iconId: rest.iconId ? rest.iconId.toString('hex') : undefined, tags: convertTags(rest.tags), addTags: convertTags(rest.addTags), removeTags: convertTags(rest.removeTags), diff --git a/src/lib/encode-converstions.ts b/src/lib/encode-converstions.ts index 93e0fc5d..41557bc7 100644 --- a/src/lib/encode-converstions.ts +++ b/src/lib/encode-converstions.ts @@ -64,7 +64,7 @@ export const convertPreset: ConvertFunction<'preset'> = (mapeoDoc) => { addTags: convertTags(mapeoDoc.addTags), removeTags: convertTags(mapeoDoc.removeTags), fieldIds: mapeoDoc.fieldIds.map((field) => Buffer.from(field, 'hex')), - iconId: mapeoDoc.icon ? Buffer.from(mapeoDoc.icon, 'hex') : undefined, + iconId: mapeoDoc.iconId ? Buffer.from(mapeoDoc.iconId, 'hex') : undefined, } } diff --git a/test/fixture.js b/test/fixture.js index 4658328b..25dfb7c3 100644 --- a/test/fixture.js +++ b/test/fixture.js @@ -1,5 +1,32 @@ import { randomBytes } from 'node:crypto' +import { cachedDataVersionTag } from 'node:v8' +// For stuff like dates or generated random Ids, so I can compare with an expected doc +const date = new Date().toJSON() +const cachedValues = { + docId: randomBytes(32).toString('hex'), + versionId:`${randomBytes(32).toString('hex')}/0`, + projectId: randomBytes(32).toString('hex'), + authorId: randomBytes(32).toString('hex'), + coreId: randomBytes(32).toString('hex'), + createdAt: date, + updatedAt: date, + attachments: {driveId: randomBytes(32).toString('hex')}, + metadata: { + position: { + timestamp: date + } + }, + defaultPresets: { + point: [randomBytes(32).toString('hex')], + area: [randomBytes(32).toString('hex')], + vertex: [randomBytes(32).toString('hex')], + line: [randomBytes(32).toString('hex')], + relation: [randomBytes(32).toString('hex')], + }, + fieldIds: [randomBytes(32).toString('hex')], + iconId: randomBytes(32).toString('hex') +} export const fixtures = { onlyId: { @@ -22,116 +49,531 @@ export const fixtures = { }, } }, - /** @type {Array<{ text: string, doc: import('../dist/types').MapeoDoc}>} */ - goodDocs:[{ - docId: randomBytes(32).toString('hex'), - versionId: `${randomBytes(32).toString('hex')}/0`, - schemaName: 'observation', - createdAt: new Date().toJSON(), - updatedAt: new Date().toJSON(), - links: [], - lat: 10, - lon: -20.39090, - refs: [], - attachments: [], - metadata: { - manualLocation: true, + /** @type {Array<{ + * expected:import('../dist/types').MapeoDoc, + * doc: import('../dist/types').MapeoDoc}> + * } */ + goodDocsMinimal:[ + { + doc:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'observation', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + refs: [], + attachments: [], + tags: {} + }, + expected:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'observation', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + refs: [], + attachments: [], + tags: {}, + metadata: {}, + } + }, + { + doc:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'project', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + defaultPresets: {}, + name: 'myProject' + }, + expected:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'project', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + defaultPresets: {point: [], area: [], vertex: [], line: [], relation: []}, + name: 'myProject' + } }, - tags: {} - }, { - docId: randomBytes(32).toString('hex'), - versionId: `${randomBytes(32).toString('hex')}/0`, - schemaName: 'project', - createdAt: new Date().toJSON(), - updatedAt: new Date().toJSON(), - links: [], - name: 'myProject', - defaultPresets: { - point: [randomBytes(32).toString('hex')], - vertex: [randomBytes(32).toString('hex')], - area: [randomBytes(32).toString('hex')], - line: [randomBytes(32).toString('hex')], - relation: [randomBytes(32).toString('hex')], + doc:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'field', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + tagKey: "myTagKey", + }, + expected:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'field', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + tagKey: "myTagKey", + type: "text", + label: "myTagKey", // see src/lib/decode-conversions.js:77 + appearance: 'multiline', + snakeCase: false, + options: [], + universal: false, + }}, + { + doc:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'preset', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + name:"myPreset", + geometry:["point"], + tags:{}, + addTags:{}, + removeTags:{}, + fieldIds:[], + terms:[] + }, + expected:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'preset', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + name:"myPreset", + geometry:["point"], + tags:{}, + addTags:{}, + removeTags:{}, + fieldIds:[], + terms:[] } }, { - docId: randomBytes(32).toString('hex'), - versionId: `${randomBytes(32).toString('hex')}/0`, - schemaName: 'field', - createdAt: new Date().toJSON(), - updatedAt: new Date().toJSON(), - links: [], - tagKey: "myTagKey", - type: "text", - label: "myLabel", - universal: true, - options: [], - appearance: 'multiline', - snakeCase: true + doc:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'role', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + role:"author", + projectId: cachedValues.projectId, + signature:"signature", + authorId: cachedValues.authorId, + capabilityType:"capability" + }, + expected:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'role', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + role:"author", + projectId: cachedValues.projectId, + action: 'UNRECOGNIZED', + signature:"signature", + authorIndex:0, + deviceIndex:0, + authorId:cachedValues.authorId, + capabilityType:"capability" + } }, { - docId: randomBytes(32).toString('hex'), - versionId: `${randomBytes(32).toString('hex')}/0`, - schemaName: 'preset', - createdAt: new Date().toJSON(), - updatedAt: new Date().toJSON(), - links: [], - name:"myPreset", - geometry:["point"], - tags:{}, - addTags:{}, - removeTags:{}, - fieldIds:[], - terms:[] + doc:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'device', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + action:"ban", + authorId: cachedValues.authorId, + projectId: cachedValues.projectId, + signature:"mySignature", + capabilityType:"someCapability", + }, + expected:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'device', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + action:"ban", + authorId: cachedValues.authorId, + projectId: cachedValues.projectId, + signature:"mySignature", + authorIndex:0, + deviceIndex:0, + capabilityType:"someCapability", + } }, { - docId: randomBytes(32).toString('hex'), - versionId: `${randomBytes(32).toString('hex')}/0`, - schemaName: 'role', - createdAt: new Date().toJSON(), - updatedAt: new Date().toJSON(), - links: [], - role:"author", - projectId:randomBytes(32).toString('hex'), - action:"role_set", - signature:"signature", - authorIndex:2, - deviceIndex:4, - authorId:randomBytes(32).toString('hex'), - capabilityType:"capability" + doc:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'coreOwnership', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + action:"remove", + coreId: cachedValues.coreId, + projectId: cachedValues.projectId, + storeType:"blob", + signature:"mySig", + authorId: cachedValues.authorId, + capabilityType: "someCapability" + }, + expected:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'coreOwnership', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + action:"remove", + coreId: cachedValues.coreId, + projectId: cachedValues.projectId, + storeType:"blob", + signature:"mySig", + authorIndex: 0, + deviceIndex: 0, + authorId: cachedValues.authorId, + capabilityType: "someCapability" + } + } + ], + goodDocsCompleted: [ + { + doc:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'observation', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + lat: 24.0424, + lon: 21.0214, + refs: [], + attachments: [{ + name: 'myFile', + type: 'photo', + driveId: cachedValues.attachments.driveId + }], + tags: { + someKeyForSingleVal: 'someVal', + // someKeyForArrVal: ['arr', 'of', 'strings'] + }, + metadata: { + manualLocation: true, + position: { + timestamp: cachedValues.metadata.position.timestamp, + mocked: false, + coords: { + latitude: 0.5, + longitude: -0.2, + altitude: 0.8, + heading: 1.2, + speed: 0.7, + acurracy: 1.3 + } + }, + lastSavedPosition: { + timestamp: cachedValues.metadata.position.timestamp, + mocked: true, + coords: { + latitude: 1.5, + longitude: -2.3, + altitude: 1000.1, + heading: 0.2, + speed: 0.1, + acurracy: 8.3 + } + }, + positionProvider: { + gpsAvailable: true, + passiveAvailable: false, + locationServicesEnabled: true, + networkAvailable: false + } + } + }, + expected:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'observation', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + lat: 24.0424, + lon: 21.0214, + refs: [], + attachments: [{ + name: 'myFile', + type: 'photo', + driveId: cachedValues.attachments.driveId + }], + tags: { + someKeyForSingleVal: 'someVal', + // someKeyForArrVal: ['arr', 'of', 'strings'] + }, + metadata: { + manualLocation: true, + position: { + timestamp: cachedValues.metadata.position.timestamp, + mocked: false, + coords: { + latitude: 0.5, + longitude: -0.2, + altitude: 0.8, + heading: 1.2, + speed: 0.7, + acurracy: 1.3 + } + }, + lastSavedPosition: { + timestamp: cachedValues.metadata.position.timestamp, + mocked: true, + coords: { + latitude: 1.5, + longitude: -2.3, + altitude: 1000.1, + heading: 0.2, + speed: 0.1, + acurracy: 8.3 + } + }, + positionProvider: { + gpsAvailable: true, + passiveAvailable: false, + locationServicesEnabled: true, + networkAvailable: false + } + }, + } + }, + { + doc:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'project', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + defaultPresets: { + point: cachedValues.defaultPresets.point, + area: cachedValues.defaultPresets.point, + vertex: cachedValues.defaultPresets.point, + line: cachedValues.defaultPresets.point, + relation: cachedValues.defaultPresets.point + }, + name: 'myProject' + }, + expected:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'project', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + defaultPresets: { + point: cachedValues.defaultPresets.point, + area: cachedValues.defaultPresets.point, + vertex: cachedValues.defaultPresets.point, + line: cachedValues.defaultPresets.point, + relation: cachedValues.defaultPresets.point, + }, + name: 'myProject' + } }, { - docId: randomBytes(32).toString('hex'), - versionId: `${randomBytes(32).toString('hex')}/0`, - schemaName: 'device', - createdAt: new Date().toJSON(), - updatedAt: new Date().toJSON(), - links: [], - action:"ban", - authorId:randomBytes(32).toString('hex'), - projectId:randomBytes(32).toString('hex'), - signature:"mySignature", - authorIndex:2, - deviceIndex:20, - capabilityType:"someCapability", + doc:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'field', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + tagKey: 'otherTagKey', + type: 'number', + label: 'differentLabel', + appearance: 'singleline', + snakeCase: true, + universal: true, + options: [{ + label: 'someOtherLabel', + value: 'somePrimitiveTagValue' + }], + }, + expected:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'field', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + tagKey: "otherTagKey", + type: "number", + label: "differentLabel", + appearance: 'singleline', + snakeCase: true, + universal: true, + options: [{ + label: 'someOtherLabel', + value: 'somePrimitiveTagValue' + }], + }}, + { + doc:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'preset', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + name:"myPreset", + geometry:["point", "vertex", "line"], + tags: { + someKeyForArrVal: ['arr', 'of', 'strings'] + }, + addTags:{ + heyAddThisTag: 'pleease' + }, + removeTags:{ + deleteInmeditaly: ['this list', 'of things'] + }, + fieldIds:cachedValues.fieldIds, + iconId: cachedValues.iconId, + terms:["imastring"] + }, + expected:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'preset', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + name:"myPreset", + geometry:["point", "vertex", "line"], + tags: { + someKeyForArrVal: ['arr', 'of', 'strings'] + }, + addTags:{ + heyAddThisTag: 'pleease' + }, + removeTags:{ + deleteInmeditaly: ['this list', 'of things'] + }, + fieldIds:cachedValues.fieldIds, + iconId: cachedValues.iconId, + terms:["imastring"] + } }, { - docId: randomBytes(32).toString('hex'), - versionId: `${randomBytes(32).toString('hex')}/0`, - schemaName: 'coreOwnership', - createdAt: new Date().toJSON(), - updatedAt: new Date().toJSON(), - links: [], - action:"remove", - coreId:randomBytes(32).toString('hex'), - projectId:randomBytes(32).toString('hex'), - storeType:"blob", - signature:"mySig", - authorIndex: 10, - deviceIndex: 9, - authorId:randomBytes(32).toString('hex'), - capabilityType: "someCapability" + doc:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'role', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + role:"author", + projectId: cachedValues.projectId, + signature:"signature", + authorId: cachedValues.authorId, + capabilityType:"capability" + }, + expected:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'role', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + role:"author", + projectId: cachedValues.projectId, + action: 'UNRECOGNIZED', + signature:"signature", + authorIndex:0, + deviceIndex:0, + authorId:cachedValues.authorId, + capabilityType:"capability" + } + }, + { + doc:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'device', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + action:"ban", + authorId: cachedValues.authorId, + projectId: cachedValues.projectId, + signature:"mySignature", + capabilityType:"someCapability", + }, + expected:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'device', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + action:"ban", + authorId: cachedValues.authorId, + projectId: cachedValues.projectId, + signature:"mySignature", + authorIndex:0, + deviceIndex:0, + capabilityType:"someCapability", + } + }, + { + doc:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'coreOwnership', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + action:"remove", + coreId: cachedValues.coreId, + projectId: cachedValues.projectId, + storeType:"blob", + signature:"mySig", + authorId: cachedValues.authorId, + capabilityType: "someCapability" + }, + expected:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'coreOwnership', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + action:"remove", + coreId: cachedValues.coreId, + projectId: cachedValues.projectId, + storeType:"blob", + signature:"mySig", + authorIndex: 0, + deviceIndex: 0, + authorId: cachedValues.authorId, + capabilityType: "someCapability" + } } ] } diff --git a/test/index.js b/test/index.js index 504cca44..fc1744dc 100644 --- a/test/index.js +++ b/test/index.js @@ -26,28 +26,35 @@ import { and } from 'ajv/dist/compile/codegen/index.js' }) } -test('testing rightfully encoding of doc',async(t) => { - fixtures.goodDocs.forEach( - /** @param {import('../dist/types').MapeoDoc} doc */ - (doc) => { - t.doesNotThrow(() => { - encode(doc) - }, `testing ${doc.schemaName}`) - }) +test(`testing encoding of doc with minimal required values, + then decoding and comparing the two objects`, async(t) => { + for(let {doc,expected} of fixtures.goodDocsMinimal){ + let buf + t.doesNotThrow(() => { + buf = encode(doc) + }, `tested encoding of ${doc.schemaName}`) + let decodedDoc = stripUndef(decode(buf, parseVersionId(doc.versionId))) + t.deepEqual(decodedDoc,expected, `tested deep equal of ${doc.schemaName}`) + } }) -test('testing encoding of doc, then decoding and comparing the two objects', async(t) => { - fixtures.goodDocs.forEach( - /** @param {import('../dist/types').MapeoDoc} doc */ - (doc) => { +test(`testing encoding of doc with additional optional values, + then decoding and comparing the two objects`, async(t) => { + for(let {doc,expected} of fixtures.goodDocsCompleted){ + let buf t.doesNotThrow(() => { - const buf = encode(doc) - const decodedDoc = stripUndef(decode(buf, parseVersionId(doc.versionId))) - t.deepEqual(doc,decodedDoc) - }, `tested ${doc.schemaName}`) - }) + buf = encode(doc) + }, `tested encoding of ${doc.schemaName}`) + let decodedDoc = stripUndef(decode(buf, parseVersionId(doc.versionId))) + if(doc.schemaName === 'preset'){ + console.log(expected) + console.log(decodedDoc) + } + t.deepEqual(decodedDoc,expected, `tested deep equal of ${doc.schemaName}`) + } }) + // test('test encoding of rightfully formated record', async (t) => { // const goodDocs = Object.keys(docs.good) // t.plan(goodDocs.length) From fc139a4a1fbd3d04b78020a7e44a8522f3c85681 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Ciccola?= Date: Thu, 10 Aug 2023 16:16:17 -0300 Subject: [PATCH 09/23] feat: add configStore tests with additional fields --- test/fixture.js | 21 ++++++++++++++------- test/index.js | 4 ---- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/test/fixture.js b/test/fixture.js index 25dfb7c3..3d654431 100644 --- a/test/fixture.js +++ b/test/fixture.js @@ -490,7 +490,10 @@ export const fixtures = { links: [], role:"author", projectId: cachedValues.projectId, + action: 'role_set', signature:"signature", + authorIndex:8, + deviceIndex:7, authorId: cachedValues.authorId, capabilityType:"capability" }, @@ -503,10 +506,10 @@ export const fixtures = { links: [], role:"author", projectId: cachedValues.projectId, - action: 'UNRECOGNIZED', + action: 'role_set', signature:"signature", - authorIndex:0, - deviceIndex:0, + authorIndex:8, + deviceIndex:7, authorId:cachedValues.authorId, capabilityType:"capability" } @@ -523,6 +526,8 @@ export const fixtures = { authorId: cachedValues.authorId, projectId: cachedValues.projectId, signature:"mySignature", + authorIndex:1, + deviceIndex:10, capabilityType:"someCapability", }, expected:{ @@ -536,8 +541,8 @@ export const fixtures = { authorId: cachedValues.authorId, projectId: cachedValues.projectId, signature:"mySignature", - authorIndex:0, - deviceIndex:0, + authorIndex:1, + deviceIndex:10, capabilityType:"someCapability", } }, @@ -554,6 +559,8 @@ export const fixtures = { projectId: cachedValues.projectId, storeType:"blob", signature:"mySig", + authorIndex: 100, + deviceIndex: 2, authorId: cachedValues.authorId, capabilityType: "someCapability" }, @@ -569,8 +576,8 @@ export const fixtures = { projectId: cachedValues.projectId, storeType:"blob", signature:"mySig", - authorIndex: 0, - deviceIndex: 0, + authorIndex: 100, + deviceIndex: 2, authorId: cachedValues.authorId, capabilityType: "someCapability" } diff --git a/test/index.js b/test/index.js index fc1744dc..2382e4af 100644 --- a/test/index.js +++ b/test/index.js @@ -46,10 +46,6 @@ test(`testing encoding of doc with additional optional values, buf = encode(doc) }, `tested encoding of ${doc.schemaName}`) let decodedDoc = stripUndef(decode(buf, parseVersionId(doc.versionId))) - if(doc.schemaName === 'preset'){ - console.log(expected) - console.log(decodedDoc) - } t.deepEqual(decodedDoc,expected, `tested deep equal of ${doc.schemaName}`) } }) From 5f120726b031e6fb540710d10de4d32c0baaca15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Ciccola?= Date: Thu, 10 Aug 2023 16:22:25 -0300 Subject: [PATCH 10/23] feat: fix test after master merge --- test/fixture.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/fixture.js b/test/fixture.js index 3d654431..b609b98e 100644 --- a/test/fixture.js +++ b/test/fixture.js @@ -119,7 +119,7 @@ export const fixtures = { updatedAt: cachedValues.updatedAt, links: [], tagKey: "myTagKey", - type: "text", + type: "UNRECOGNIZED", label: "myTagKey", // see src/lib/decode-conversions.js:77 appearance: 'multiline', snakeCase: false, From be09f671501c9901fcd647320151c76b8048772b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Ciccola?= Date: Thu, 10 Aug 2023 16:45:01 -0300 Subject: [PATCH 11/23] feat: add tests for extra non required values, and cleanup --- test/fixture.js | 353 +++++++++++++++++++++++++++++++++++++++++++++++- test/index.js | 93 +++---------- 2 files changed, 364 insertions(+), 82 deletions(-) diff --git a/test/fixture.js b/test/fixture.js index b609b98e..772b64ff 100644 --- a/test/fixture.js +++ b/test/fixture.js @@ -32,21 +32,21 @@ export const fixtures = { onlyId: { text: 'test encoding of record with missing fields', plan: 1, - doc: {id: randomBytes(32).toString('hex')} + doc: {docId: randomBytes(32).toString('hex')} }, badDocName: { text: 'test encoding of wrong record type', plan: 1, doc: { - id: randomBytes(32).toString('hex'), - schemaName: 'doesnotexist', + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'observOtion', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, links: [], - createdAt: new Date().toJSON(), refs: [], attachments: [], - metadata: { - manual_location: true, - }, + tags: {} } }, /** @type {Array<{ @@ -582,5 +582,344 @@ export const fixtures = { capabilityType: "someCapability" } } + ], + goodDocsWithExtraFields: [ + { + doc:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'observation', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + zodiacSign: 'banana', + links: [], + lat: 24.0424, + lon: 21.0214, + refs: [], + attachments: [{ + name: 'myFile', + type: 'photo', + driveId: cachedValues.attachments.driveId + }], + tags: { + someKeyForSingleVal: 'someVal' + }, + metadata: { + manualLocation: true, + position: { + IMInc0rRecT: Array(100).fill(Math.random()*1000), + timestamp: cachedValues.metadata.position.timestamp, + mocked: false, + coords: { + latitude: 0.5, + longitude: -0.2, + altitude: 0.8, + heading: 1.2, + speed: 0.7, + acurracy: 1.3, + OTHeRValue: 'Am I a Number?' + } + }, + lastSavedPosition: { + timestamp: cachedValues.metadata.position.timestamp, + mocked: true, + coords: { + latitude: 1.5, + longitude: -2.3, + altitude: 1000.1, + heading: 0.2, + speed: 0.1, + acurracy: 8.3 + } + }, + positionProvider: { + gpsAvailable: true, + passiveAvailable: false, + locationServicesEnabled: true, + evilLairLocationEnabled: 'NOT', + networkAvailable: false + } + } + }, + expected:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'observation', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + lat: 24.0424, + lon: 21.0214, + refs: [], + attachments: [{ + name: 'myFile', + type: 'photo', + driveId: cachedValues.attachments.driveId + }], + tags: { + someKeyForSingleVal: 'someVal', + }, + metadata: { + manualLocation: true, + position: { + timestamp: cachedValues.metadata.position.timestamp, + mocked: false, + coords: { + latitude: 0.5, + longitude: -0.2, + altitude: 0.8, + heading: 1.2, + speed: 0.7, + acurracy: 1.3 + } + }, + lastSavedPosition: { + timestamp: cachedValues.metadata.position.timestamp, + mocked: true, + coords: { + latitude: 1.5, + longitude: -2.3, + altitude: 1000.1, + heading: 0.2, + speed: 0.1, + acurracy: 8.3 + } + }, + positionProvider: { + gpsAvailable: true, + passiveAvailable: false, + locationServicesEnabled: true, + networkAvailable: false + } + }, + } + }, + { + doc:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'project', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + numberOfPeople: 'glove', + defaultPresets: { + point: cachedValues.defaultPresets.point, + area: cachedValues.defaultPresets.point, + vertex: cachedValues.defaultPresets.point, + line: cachedValues.defaultPresets.point, + relation: cachedValues.defaultPresets.point, + way: 'I DONT EXIST YET??' + }, + name: 'myProject' + }, + expected:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'project', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + defaultPresets: { + point: cachedValues.defaultPresets.point, + area: cachedValues.defaultPresets.point, + vertex: cachedValues.defaultPresets.point, + line: cachedValues.defaultPresets.point, + relation: cachedValues.defaultPresets.point, + }, + name: 'myProject' + } + }, + { + doc:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'field', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + destroyedAt: new Date().toJSON(), + links: [], + tagKey: 'otherTagKey', + type: 'number', + label: 'differentLabel', + appearance: 'singleline', + snakeCase: true, + universal: true, + options: [{ + centipede: 'big', + label: 'someOtherLabel', + value: 'somePrimitiveTagValue' + }], + }, + expected:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'field', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + tagKey: "otherTagKey", + type: "number", + label: "differentLabel", + appearance: 'singleline', + snakeCase: true, + universal: true, + options: [{ + label: 'someOtherLabel', + value: 'somePrimitiveTagValue' + }], + }}, + { + doc:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'preset', + keyboardSound: 'rad', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + name:"myPreset", + geometry:["point", "vertex", "line"], + tags: { + someKeyForArrVal: ['arr', 'of', 'strings'] + }, + addTags:{ + heyAddThisTag: 'pleease' + }, + removeTags:{ + deleteInmeditaly: ['this list', 'of things'] + }, + fieldIds:cachedValues.fieldIds, + iconId: cachedValues.iconId, + terms:["imastring"] + }, + expected:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'preset', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + name:"myPreset", + geometry:["point", "vertex", "line"], + tags: { + someKeyForArrVal: ['arr', 'of', 'strings'] + }, + addTags:{ + heyAddThisTag: 'pleease' + }, + removeTags:{ + deleteInmeditaly: ['this list', 'of things'] + }, + fieldIds:cachedValues.fieldIds, + iconId: cachedValues.iconId, + terms:["imastring"] + } + }, + { + doc:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'role', + areYouMasterOfTheUniverse: 'js doesnt allow question marks in identifiers, which saddens me...', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + role:"author", + projectId: cachedValues.projectId, + action: 'role_set', + signature:"signature", + authorIndex:8, + deviceIndex:7, + authorId: cachedValues.authorId, + capabilityType:"capability" + }, + expected:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'role', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + role:"author", + projectId: cachedValues.projectId, + action: 'role_set', + signature:"signature", + authorIndex:8, + deviceIndex:7, + authorId:cachedValues.authorId, + capabilityType:"capability" + } + }, + { + doc:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'device', + programmedObsolescense: 'well, duh...', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + action:"ban", + authorId: cachedValues.authorId, + projectId: cachedValues.projectId, + signature:"mySignature", + authorIndex:1, + deviceIndex:10, + capabilityType:"someCapability", + }, + expected:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'device', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + action:"ban", + authorId: cachedValues.authorId, + projectId: cachedValues.projectId, + signature:"mySignature", + authorIndex:1, + deviceIndex:10, + capabilityType:"someCapability", + } + }, + { + doc:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + NOBODYOWNSANYTHINGOK: ':|', + schemaName: 'coreOwnership', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + action:"remove", + coreId: cachedValues.coreId, + projectId: cachedValues.projectId, + storeType:"blob", + signature:"mySig", + authorIndex: 100, + deviceIndex: 2, + authorId: cachedValues.authorId, + capabilityType: "someCapability" + }, + expected:{ + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'coreOwnership', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + action:"remove", + coreId: cachedValues.coreId, + projectId: cachedValues.projectId, + storeType:"blob", + signature:"mySig", + authorIndex: 100, + deviceIndex: 2, + authorId: cachedValues.authorId, + capabilityType: "someCapability" + } + } ] } diff --git a/test/index.js b/test/index.js index 2382e4af..ac8c957c 100644 --- a/test/index.js +++ b/test/index.js @@ -27,92 +27,43 @@ import { and } from 'ajv/dist/compile/codegen/index.js' } test(`testing encoding of doc with minimal required values, - then decoding and comparing the two objects`, async(t) => { + then decoding and comparing the two objects`, + async(t) => { for(let {doc,expected} of fixtures.goodDocsMinimal){ let buf t.doesNotThrow(() => { buf = encode(doc) }, `tested encoding of ${doc.schemaName}`) let decodedDoc = stripUndef(decode(buf, parseVersionId(doc.versionId))) - t.deepEqual(decodedDoc,expected, `tested deep equal of ${doc.schemaName}`) + t.deepEqual(decodedDoc,expected, `tested deep equal of ${doc.schemaName} with only required values`) } }) test(`testing encoding of doc with additional optional values, - then decoding and comparing the two objects`, async(t) => { + then decoding and comparing the two objects`, + async(t) => { for(let {doc,expected} of fixtures.goodDocsCompleted){ let buf t.doesNotThrow(() => { buf = encode(doc) }, `tested encoding of ${doc.schemaName}`) let decodedDoc = stripUndef(decode(buf, parseVersionId(doc.versionId))) - t.deepEqual(decodedDoc,expected, `tested deep equal of ${doc.schemaName}`) + t.deepEqual(decodedDoc,expected, `tested deep equal of ${doc.schemaName} with additional values`) } }) - -// test('test encoding of rightfully formated record', async (t) => { -// const goodDocs = Object.keys(docs.good) -// t.plan(goodDocs.length) -// goodDocs.forEach((k) => { -// const doc = docs.good[k] -// t.doesNotThrow(() => { -// encode(doc) -// }, `testing ${k}`) -// }) -// }) - -// test('test validation of record', async (t) => { -// const goodDocs = Object.keys(docs.good) -// t.plan(goodDocs.length) -// goodDocs.forEach((k) => { -// const doc = docs.good[k] -// const record = decode(encode(doc), { -// coreKey: randomBytes(32), -// index: 0, -// }) -// t.doesNotThrow(() => { -// // field has a type which is different from the rest :| -// if (k !== 'field') validate(record) -// }, `testing validation of ${k}`) -// }) -// }) - -// test('test encoding, decoding of record and comparing the two versions', async (t) => { -// const goodDocs = Object.keys(docs.good) -// goodDocs.forEach((k) => { -// const doc = docs.good[k] -// const record = decode(encode(doc), { -// coreKey: randomBytes(32), -// index: 0, -// }) -// const fields = Object.keys(doc) -// // t.plan(goodDocs.length * fields.length * 2) -// fields.forEach((f) => { -// console.log(typeof record[f], typeof doc[f]) -// const msg = `checking ${f} for ${k}` -// record[f] && doc[f] ? t.pass(msg) : t.fail(msg) - -// // if field is not an object, check equality -// // since objects as fields usually mean the possibility of additionalFields in jsonSchemas -// if (typeof record[f] !== 'object') { -// t.equal(record[f], doc[f], `comparing value of ${f} for ${k}`) -// } -// }) -// }) -// }) - -// test('test decoding of record without passing core key and index', async (t) => { -// const goodDocs = Object.keys(docs.good) -// t.plan(goodDocs.length) -// goodDocs.forEach((key) => { -// const doc = docs.good[key] -// const record = encode(doc) -// t.throws(() => { -// decode(record) -// }, `testing ${key}`) -// }) -// }) +test(`testing encoding of doc with additional extra values, +then decoding and comparing the two objects - extra values shouldn't be present`, + async(t) => { + for(let {doc,expected} of fixtures.goodDocsWithExtraFields){ + let buf + t.doesNotThrow(() => { + buf = encode(doc) + }, `tested encoding of ${doc.schemaName}`) + let decodedDoc = stripUndef(decode(buf, parseVersionId(doc.versionId))) + t.deepEqual(decodedDoc,expected, `tested deep equal of ${doc.schemaName} with extra - non valid - values`) + } +}) /** * @param {object} obj @@ -121,11 +72,3 @@ test(`testing encoding of doc with additional optional values, function stripUndef(obj) { return JSON.parse(JSON.stringify(obj)) } - -/** @param {Number} value - * @return Number */ -function countDecimals(value) { - return ((value % 1) != 0) - ? Number(value.toString().split(".")[1].length) - : 0; -} From e9097d8fd302ee1c1f57d49a5cbe6cda66a3adf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Ciccola?= Date: Mon, 14 Aug 2023 09:30:35 -0300 Subject: [PATCH 12/23] feat: fix buf in encoding field tags --- src/lib/encode-converstions.ts | 9 ++++----- test/fixture.js | 10 ++++++---- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/lib/encode-converstions.ts b/src/lib/encode-converstions.ts index 41557bc7..2e3ba75d 100644 --- a/src/lib/encode-converstions.ts +++ b/src/lib/encode-converstions.ts @@ -145,11 +145,10 @@ function convertTags(tags: { }): { [key: string]: TagValue_1 } { - return Object.keys(tags).reduce( - (acc: { [key: string]: TagValue_1 }, k: string) => { - return { - [k]: convertTagValue(tags[k]), - } + return Object.keys(tags).reduce<{ [key: string]: TagValue_1}>( + (acc,k) => { + acc[k] = convertTagValue(tags[k]) + return acc }, {} ) diff --git a/test/fixture.js b/test/fixture.js index 772b64ff..b2e5f558 100644 --- a/test/fixture.js +++ b/test/fixture.js @@ -273,7 +273,7 @@ export const fixtures = { }], tags: { someKeyForSingleVal: 'someVal', - // someKeyForArrVal: ['arr', 'of', 'strings'] + someKeyForArrVal: ['arr', 'of', 'strings'] }, metadata: { manualLocation: true, @@ -326,7 +326,7 @@ export const fixtures = { }], tags: { someKeyForSingleVal: 'someVal', - // someKeyForArrVal: ['arr', 'of', 'strings'] + someKeyForArrVal: ['arr', 'of', 'strings'] }, metadata: { manualLocation: true, @@ -448,7 +448,8 @@ export const fixtures = { someKeyForArrVal: ['arr', 'of', 'strings'] }, addTags:{ - heyAddThisTag: 'pleease' + heyAddThisTag: 'pleease', + orMaybeThis: ['right?', '', 'that was empty'] }, removeTags:{ deleteInmeditaly: ['this list', 'of things'] @@ -470,7 +471,8 @@ export const fixtures = { someKeyForArrVal: ['arr', 'of', 'strings'] }, addTags:{ - heyAddThisTag: 'pleease' + heyAddThisTag: 'pleease', + orMaybeThis: ['right?', '', 'that was empty'] }, removeTags:{ deleteInmeditaly: ['this list', 'of things'] From 9e3aca1008ccb8c8f010612e36f84999c5adb226 Mon Sep 17 00:00:00 2001 From: Gregor MacLennan Date: Tue, 15 Aug 2023 10:07:42 +0100 Subject: [PATCH 13/23] chore: format with prettier --- src/lib/decode-conversions.ts | 36 +- src/lib/encode-converstions.ts | 50 +-- test/fixture.js | 628 +++++++++++++++++---------------- test/index.js | 77 ++-- 4 files changed, 412 insertions(+), 379 deletions(-) diff --git a/src/lib/decode-conversions.ts b/src/lib/decode-conversions.ts index b4a84fe9..b85f3330 100644 --- a/src/lib/decode-conversions.ts +++ b/src/lib/decode-conversions.ts @@ -32,12 +32,12 @@ export const convertProject: ConvertFunction<'project'> = ( ...jsonSchemaCommon, ...rest, defaultPresets: { - point: message.defaultPresets?.point.map(p => p.toString('hex')), - area: message.defaultPresets?.area.map(a => a.toString('hex')), - vertex: message.defaultPresets?.vertex.map(v => v.toString('hex')), - line: message.defaultPresets?.line.map(l => l.toString('hex')), - relation: message.defaultPresets?.relation.map(r => r.toString('hex')), - } + point: message.defaultPresets?.point.map((p) => p.toString('hex')), + area: message.defaultPresets?.area.map((a) => a.toString('hex')), + vertex: message.defaultPresets?.vertex.map((v) => v.toString('hex')), + line: message.defaultPresets?.line.map((l) => l.toString('hex')), + relation: message.defaultPresets?.relation.map((r) => r.toString('hex')), + }, } } @@ -121,19 +121,16 @@ export const convertPreset: ConvertFunction<'preset'> = ( } } -export const convertRole: ConvertFunction<'role'> = ( -message, -versionObj -) => { - const { common, schemaVersion, ...rest } = message - const jsonSchemaCommon = convertCommon(common, versionObj) - return { - ...jsonSchemaCommon, - ...rest, +export const convertRole: ConvertFunction<'role'> = (message, versionObj) => { + const { common, schemaVersion, ...rest } = message + const jsonSchemaCommon = convertCommon(common, versionObj) + return { + ...jsonSchemaCommon, + ...rest, role: rest.role, projectId: message.projectId.toString('hex'), - authorId: message.authorId.toString('hex') - } + authorId: message.authorId.toString('hex'), + } } export const convertDevice: ConvertFunction<'device'> = ( @@ -146,7 +143,7 @@ export const convertDevice: ConvertFunction<'device'> = ( ...jsonSchemaCommon, ...rest, authorId: message.authorId.toString('hex'), - projectId: message.projectId.toString('hex') + projectId: message.projectId.toString('hex'), } } @@ -161,9 +158,8 @@ export const convertCoreOwnership: ConvertFunction<'coreOwnership'> = ( ...rest, coreId: message.coreId.toString('hex'), projectId: message.projectId.toString('hex'), - authorId: message.authorId.toString('hex') + authorId: message.authorId.toString('hex'), } - } function convertTags(tags: { [key: string]: TagValue_1 } | undefined): { diff --git a/src/lib/encode-converstions.ts b/src/lib/encode-converstions.ts index 2e3ba75d..c8baae55 100644 --- a/src/lib/encode-converstions.ts +++ b/src/lib/encode-converstions.ts @@ -26,12 +26,22 @@ export const convertProject: ConvertFunction<'project'> = (mapeoDoc) => { common: convertCommon(mapeoDoc), ...mapeoDoc, defaultPresets: { - point: (mapeoDoc.defaultPresets.point || []).map(p => Buffer.from(p,'hex')), - area: (mapeoDoc.defaultPresets.area || []).map(a => Buffer.from(a,'hex')), - vertex: (mapeoDoc.defaultPresets.vertex || []).map(v => Buffer.from(v,'hex')), - line: (mapeoDoc.defaultPresets.line || []).map(l => Buffer.from(l, 'hex')), - relation: (mapeoDoc.defaultPresets.relation || []).map(r => Buffer.from(r, 'hex')) - } + point: (mapeoDoc.defaultPresets.point || []).map((p) => + Buffer.from(p, 'hex') + ), + area: (mapeoDoc.defaultPresets.area || []).map((a) => + Buffer.from(a, 'hex') + ), + vertex: (mapeoDoc.defaultPresets.vertex || []).map((v) => + Buffer.from(v, 'hex') + ), + line: (mapeoDoc.defaultPresets.line || []).map((l) => + Buffer.from(l, 'hex') + ), + relation: (mapeoDoc.defaultPresets.relation || []).map((r) => + Buffer.from(r, 'hex') + ), + }, } } @@ -94,26 +104,21 @@ export const convertObservation: ConvertFunction<'observation'> = ( } } -export const convertRole: ConvertFunction<'role'> = ( - mapeoDoc -) => { +export const convertRole: ConvertFunction<'role'> = (mapeoDoc) => { return { common: convertCommon(mapeoDoc), ...mapeoDoc, - projectId: Buffer.from(mapeoDoc.projectId,'hex'), - authorId: Buffer.from(mapeoDoc.authorId,'hex') - + projectId: Buffer.from(mapeoDoc.projectId, 'hex'), + authorId: Buffer.from(mapeoDoc.authorId, 'hex'), } } -export const convertDevice: ConvertFunction<'device'> = ( - mapeoDoc -) => { +export const convertDevice: ConvertFunction<'device'> = (mapeoDoc) => { return { common: convertCommon(mapeoDoc), ...mapeoDoc, authorId: Buffer.from(mapeoDoc.authorId, 'hex'), - projectId: Buffer.from(mapeoDoc.projectId, 'hex') + projectId: Buffer.from(mapeoDoc.projectId, 'hex'), } } @@ -125,7 +130,7 @@ export const convertCoreOwnership: ConvertFunction<'coreOwnership'> = ( ...mapeoDoc, coreId: Buffer.from(mapeoDoc.coreId, 'hex'), projectId: Buffer.from(mapeoDoc.projectId, 'hex'), - authorId: Buffer.from(mapeoDoc.authorId,'hex') + authorId: Buffer.from(mapeoDoc.authorId, 'hex'), } } @@ -145,13 +150,10 @@ function convertTags(tags: { }): { [key: string]: TagValue_1 } { - return Object.keys(tags).reduce<{ [key: string]: TagValue_1}>( - (acc,k) => { - acc[k] = convertTagValue(tags[k]) - return acc - }, - {} - ) + return Object.keys(tags).reduce<{ [key: string]: TagValue_1 }>((acc, k) => { + acc[k] = convertTagValue(tags[k]) + return acc + }, {}) } function convertTagValue(tagValue: JsonTagValue): TagValue_1 { diff --git a/test/fixture.js b/test/fixture.js index b2e5f558..92e4e7d3 100644 --- a/test/fixture.js +++ b/test/fixture.js @@ -2,20 +2,20 @@ import { randomBytes } from 'node:crypto' import { cachedDataVersionTag } from 'node:v8' // For stuff like dates or generated random Ids, so I can compare with an expected doc -const date = new Date().toJSON() +const date = new Date().toJSON() const cachedValues = { docId: randomBytes(32).toString('hex'), - versionId:`${randomBytes(32).toString('hex')}/0`, + versionId: `${randomBytes(32).toString('hex')}/0`, projectId: randomBytes(32).toString('hex'), authorId: randomBytes(32).toString('hex'), coreId: randomBytes(32).toString('hex'), createdAt: date, updatedAt: date, - attachments: {driveId: randomBytes(32).toString('hex')}, + attachments: { driveId: randomBytes(32).toString('hex') }, metadata: { position: { - timestamp: date - } + timestamp: date, + }, }, defaultPresets: { point: [randomBytes(32).toString('hex')], @@ -25,14 +25,14 @@ const cachedValues = { relation: [randomBytes(32).toString('hex')], }, fieldIds: [randomBytes(32).toString('hex')], - iconId: randomBytes(32).toString('hex') + iconId: randomBytes(32).toString('hex'), } export const fixtures = { onlyId: { text: 'test encoding of record with missing fields', plan: 1, - doc: {docId: randomBytes(32).toString('hex')} + doc: { docId: randomBytes(32).toString('hex') }, }, badDocName: { text: 'test encoding of wrong record type', @@ -46,16 +46,16 @@ export const fixtures = { links: [], refs: [], attachments: [], - tags: {} - } + tags: {}, + }, }, /** @type {Array<{ * expected:import('../dist/types').MapeoDoc, * doc: import('../dist/types').MapeoDoc}> * } */ - goodDocsMinimal:[ + goodDocsMinimal: [ { - doc:{ + doc: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'observation', @@ -64,9 +64,9 @@ export const fixtures = { links: [], refs: [], attachments: [], - tags: {} + tags: {}, }, - expected:{ + expected: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'observation', @@ -77,10 +77,10 @@ export const fixtures = { attachments: [], tags: {}, metadata: {}, - } - }, + }, + }, { - doc:{ + doc: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'project', @@ -88,175 +88,182 @@ export const fixtures = { updatedAt: cachedValues.updatedAt, links: [], defaultPresets: {}, - name: 'myProject' + name: 'myProject', }, - expected:{ + expected: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'project', createdAt: cachedValues.createdAt, updatedAt: cachedValues.updatedAt, links: [], - defaultPresets: {point: [], area: [], vertex: [], line: [], relation: []}, - name: 'myProject' - } + defaultPresets: { + point: [], + area: [], + vertex: [], + line: [], + relation: [], + }, + name: 'myProject', + }, }, { - doc:{ + doc: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'field', createdAt: cachedValues.createdAt, updatedAt: cachedValues.updatedAt, links: [], - tagKey: "myTagKey", + tagKey: 'myTagKey', }, - expected:{ + expected: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'field', createdAt: cachedValues.createdAt, updatedAt: cachedValues.updatedAt, links: [], - tagKey: "myTagKey", - type: "UNRECOGNIZED", - label: "myTagKey", // see src/lib/decode-conversions.js:77 + tagKey: 'myTagKey', + type: 'UNRECOGNIZED', + label: 'myTagKey', // see src/lib/decode-conversions.js:77 appearance: 'multiline', snakeCase: false, options: [], universal: false, - }}, + }, + }, { - doc:{ + doc: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'preset', createdAt: cachedValues.createdAt, updatedAt: cachedValues.updatedAt, links: [], - name:"myPreset", - geometry:["point"], - tags:{}, - addTags:{}, - removeTags:{}, - fieldIds:[], - terms:[] + name: 'myPreset', + geometry: ['point'], + tags: {}, + addTags: {}, + removeTags: {}, + fieldIds: [], + terms: [], }, - expected:{ + expected: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'preset', createdAt: cachedValues.createdAt, updatedAt: cachedValues.updatedAt, links: [], - name:"myPreset", - geometry:["point"], - tags:{}, - addTags:{}, - removeTags:{}, - fieldIds:[], - terms:[] - } + name: 'myPreset', + geometry: ['point'], + tags: {}, + addTags: {}, + removeTags: {}, + fieldIds: [], + terms: [], + }, }, { - doc:{ + doc: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'role', createdAt: cachedValues.createdAt, updatedAt: cachedValues.updatedAt, links: [], - role:"author", + role: 'author', projectId: cachedValues.projectId, - signature:"signature", + signature: 'signature', authorId: cachedValues.authorId, - capabilityType:"capability" + capabilityType: 'capability', }, - expected:{ + expected: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'role', createdAt: cachedValues.createdAt, updatedAt: cachedValues.updatedAt, links: [], - role:"author", + role: 'author', projectId: cachedValues.projectId, action: 'UNRECOGNIZED', - signature:"signature", - authorIndex:0, - deviceIndex:0, - authorId:cachedValues.authorId, - capabilityType:"capability" - } + signature: 'signature', + authorIndex: 0, + deviceIndex: 0, + authorId: cachedValues.authorId, + capabilityType: 'capability', + }, }, { - doc:{ + doc: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'device', createdAt: cachedValues.createdAt, updatedAt: cachedValues.updatedAt, links: [], - action:"ban", + action: 'ban', authorId: cachedValues.authorId, projectId: cachedValues.projectId, - signature:"mySignature", - capabilityType:"someCapability", + signature: 'mySignature', + capabilityType: 'someCapability', }, - expected:{ + expected: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'device', createdAt: cachedValues.createdAt, updatedAt: cachedValues.updatedAt, links: [], - action:"ban", + action: 'ban', authorId: cachedValues.authorId, projectId: cachedValues.projectId, - signature:"mySignature", - authorIndex:0, - deviceIndex:0, - capabilityType:"someCapability", - } + signature: 'mySignature', + authorIndex: 0, + deviceIndex: 0, + capabilityType: 'someCapability', + }, }, { - doc:{ + doc: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'coreOwnership', createdAt: cachedValues.createdAt, updatedAt: cachedValues.updatedAt, links: [], - action:"remove", + action: 'remove', coreId: cachedValues.coreId, projectId: cachedValues.projectId, - storeType:"blob", - signature:"mySig", + storeType: 'blob', + signature: 'mySig', authorId: cachedValues.authorId, - capabilityType: "someCapability" + capabilityType: 'someCapability', }, - expected:{ + expected: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'coreOwnership', createdAt: cachedValues.createdAt, updatedAt: cachedValues.updatedAt, links: [], - action:"remove", + action: 'remove', coreId: cachedValues.coreId, projectId: cachedValues.projectId, - storeType:"blob", - signature:"mySig", + storeType: 'blob', + signature: 'mySig', authorIndex: 0, deviceIndex: 0, authorId: cachedValues.authorId, - capabilityType: "someCapability" - } - } + capabilityType: 'someCapability', + }, + }, ], goodDocsCompleted: [ { - doc:{ + doc: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'observation', @@ -266,14 +273,16 @@ export const fixtures = { lat: 24.0424, lon: 21.0214, refs: [], - attachments: [{ - name: 'myFile', - type: 'photo', - driveId: cachedValues.attachments.driveId - }], + attachments: [ + { + name: 'myFile', + type: 'photo', + driveId: cachedValues.attachments.driveId, + }, + ], tags: { someKeyForSingleVal: 'someVal', - someKeyForArrVal: ['arr', 'of', 'strings'] + someKeyForArrVal: ['arr', 'of', 'strings'], }, metadata: { manualLocation: true, @@ -286,8 +295,8 @@ export const fixtures = { altitude: 0.8, heading: 1.2, speed: 0.7, - acurracy: 1.3 - } + acurracy: 1.3, + }, }, lastSavedPosition: { timestamp: cachedValues.metadata.position.timestamp, @@ -298,18 +307,18 @@ export const fixtures = { altitude: 1000.1, heading: 0.2, speed: 0.1, - acurracy: 8.3 - } + acurracy: 8.3, + }, }, positionProvider: { gpsAvailable: true, passiveAvailable: false, locationServicesEnabled: true, - networkAvailable: false - } - } + networkAvailable: false, + }, + }, }, - expected:{ + expected: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'observation', @@ -319,14 +328,16 @@ export const fixtures = { lat: 24.0424, lon: 21.0214, refs: [], - attachments: [{ - name: 'myFile', - type: 'photo', - driveId: cachedValues.attachments.driveId - }], + attachments: [ + { + name: 'myFile', + type: 'photo', + driveId: cachedValues.attachments.driveId, + }, + ], tags: { someKeyForSingleVal: 'someVal', - someKeyForArrVal: ['arr', 'of', 'strings'] + someKeyForArrVal: ['arr', 'of', 'strings'], }, metadata: { manualLocation: true, @@ -339,8 +350,8 @@ export const fixtures = { altitude: 0.8, heading: 1.2, speed: 0.7, - acurracy: 1.3 - } + acurracy: 1.3, + }, }, lastSavedPosition: { timestamp: cachedValues.metadata.position.timestamp, @@ -351,20 +362,20 @@ export const fixtures = { altitude: 1000.1, heading: 0.2, speed: 0.1, - acurracy: 8.3 - } + acurracy: 8.3, + }, }, positionProvider: { gpsAvailable: true, passiveAvailable: false, locationServicesEnabled: true, - networkAvailable: false - } + networkAvailable: false, + }, }, - } - }, + }, + }, { - doc:{ + doc: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'project', @@ -376,11 +387,11 @@ export const fixtures = { area: cachedValues.defaultPresets.point, vertex: cachedValues.defaultPresets.point, line: cachedValues.defaultPresets.point, - relation: cachedValues.defaultPresets.point + relation: cachedValues.defaultPresets.point, }, - name: 'myProject' + name: 'myProject', }, - expected:{ + expected: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'project', @@ -394,11 +405,11 @@ export const fixtures = { line: cachedValues.defaultPresets.point, relation: cachedValues.defaultPresets.point, }, - name: 'myProject' - } + name: 'myProject', + }, }, { - doc:{ + doc: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'field', @@ -411,183 +422,188 @@ export const fixtures = { appearance: 'singleline', snakeCase: true, universal: true, - options: [{ - label: 'someOtherLabel', - value: 'somePrimitiveTagValue' - }], + options: [ + { + label: 'someOtherLabel', + value: 'somePrimitiveTagValue', + }, + ], }, - expected:{ + expected: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'field', createdAt: cachedValues.createdAt, updatedAt: cachedValues.updatedAt, links: [], - tagKey: "otherTagKey", - type: "number", - label: "differentLabel", + tagKey: 'otherTagKey', + type: 'number', + label: 'differentLabel', appearance: 'singleline', snakeCase: true, universal: true, - options: [{ - label: 'someOtherLabel', - value: 'somePrimitiveTagValue' - }], - }}, + options: [ + { + label: 'someOtherLabel', + value: 'somePrimitiveTagValue', + }, + ], + }, + }, { - doc:{ + doc: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'preset', createdAt: cachedValues.createdAt, updatedAt: cachedValues.updatedAt, links: [], - name:"myPreset", - geometry:["point", "vertex", "line"], + name: 'myPreset', + geometry: ['point', 'vertex', 'line'], tags: { - someKeyForArrVal: ['arr', 'of', 'strings'] + someKeyForArrVal: ['arr', 'of', 'strings'], }, - addTags:{ + addTags: { heyAddThisTag: 'pleease', - orMaybeThis: ['right?', '', 'that was empty'] + orMaybeThis: ['right?', '', 'that was empty'], }, - removeTags:{ - deleteInmeditaly: ['this list', 'of things'] + removeTags: { + deleteInmeditaly: ['this list', 'of things'], }, - fieldIds:cachedValues.fieldIds, + fieldIds: cachedValues.fieldIds, iconId: cachedValues.iconId, - terms:["imastring"] + terms: ['imastring'], }, - expected:{ + expected: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'preset', createdAt: cachedValues.createdAt, updatedAt: cachedValues.updatedAt, links: [], - name:"myPreset", - geometry:["point", "vertex", "line"], + name: 'myPreset', + geometry: ['point', 'vertex', 'line'], tags: { - someKeyForArrVal: ['arr', 'of', 'strings'] + someKeyForArrVal: ['arr', 'of', 'strings'], }, - addTags:{ + addTags: { heyAddThisTag: 'pleease', - orMaybeThis: ['right?', '', 'that was empty'] + orMaybeThis: ['right?', '', 'that was empty'], }, - removeTags:{ - deleteInmeditaly: ['this list', 'of things'] + removeTags: { + deleteInmeditaly: ['this list', 'of things'], }, - fieldIds:cachedValues.fieldIds, + fieldIds: cachedValues.fieldIds, iconId: cachedValues.iconId, - terms:["imastring"] - } + terms: ['imastring'], + }, }, { - doc:{ + doc: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'role', createdAt: cachedValues.createdAt, updatedAt: cachedValues.updatedAt, links: [], - role:"author", + role: 'author', projectId: cachedValues.projectId, action: 'role_set', - signature:"signature", - authorIndex:8, - deviceIndex:7, + signature: 'signature', + authorIndex: 8, + deviceIndex: 7, authorId: cachedValues.authorId, - capabilityType:"capability" + capabilityType: 'capability', }, - expected:{ + expected: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'role', createdAt: cachedValues.createdAt, updatedAt: cachedValues.updatedAt, links: [], - role:"author", + role: 'author', projectId: cachedValues.projectId, action: 'role_set', - signature:"signature", - authorIndex:8, - deviceIndex:7, - authorId:cachedValues.authorId, - capabilityType:"capability" - } + signature: 'signature', + authorIndex: 8, + deviceIndex: 7, + authorId: cachedValues.authorId, + capabilityType: 'capability', + }, }, { - doc:{ + doc: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'device', createdAt: cachedValues.createdAt, updatedAt: cachedValues.updatedAt, links: [], - action:"ban", + action: 'ban', authorId: cachedValues.authorId, projectId: cachedValues.projectId, - signature:"mySignature", - authorIndex:1, - deviceIndex:10, - capabilityType:"someCapability", + signature: 'mySignature', + authorIndex: 1, + deviceIndex: 10, + capabilityType: 'someCapability', }, - expected:{ + expected: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'device', createdAt: cachedValues.createdAt, updatedAt: cachedValues.updatedAt, links: [], - action:"ban", + action: 'ban', authorId: cachedValues.authorId, projectId: cachedValues.projectId, - signature:"mySignature", - authorIndex:1, - deviceIndex:10, - capabilityType:"someCapability", - } + signature: 'mySignature', + authorIndex: 1, + deviceIndex: 10, + capabilityType: 'someCapability', + }, }, { - doc:{ + doc: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'coreOwnership', createdAt: cachedValues.createdAt, updatedAt: cachedValues.updatedAt, links: [], - action:"remove", + action: 'remove', coreId: cachedValues.coreId, projectId: cachedValues.projectId, - storeType:"blob", - signature:"mySig", + storeType: 'blob', + signature: 'mySig', authorIndex: 100, deviceIndex: 2, authorId: cachedValues.authorId, - capabilityType: "someCapability" + capabilityType: 'someCapability', }, - expected:{ + expected: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'coreOwnership', createdAt: cachedValues.createdAt, updatedAt: cachedValues.updatedAt, links: [], - action:"remove", + action: 'remove', coreId: cachedValues.coreId, projectId: cachedValues.projectId, - storeType:"blob", - signature:"mySig", + storeType: 'blob', + signature: 'mySig', authorIndex: 100, deviceIndex: 2, authorId: cachedValues.authorId, - capabilityType: "someCapability" - } - } + capabilityType: 'someCapability', + }, + }, ], goodDocsWithExtraFields: [ { - doc:{ + doc: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'observation', @@ -598,18 +614,20 @@ export const fixtures = { lat: 24.0424, lon: 21.0214, refs: [], - attachments: [{ - name: 'myFile', - type: 'photo', - driveId: cachedValues.attachments.driveId - }], + attachments: [ + { + name: 'myFile', + type: 'photo', + driveId: cachedValues.attachments.driveId, + }, + ], tags: { - someKeyForSingleVal: 'someVal' + someKeyForSingleVal: 'someVal', }, metadata: { manualLocation: true, position: { - IMInc0rRecT: Array(100).fill(Math.random()*1000), + IMInc0rRecT: Array(100).fill(Math.random() * 1000), timestamp: cachedValues.metadata.position.timestamp, mocked: false, coords: { @@ -619,8 +637,8 @@ export const fixtures = { heading: 1.2, speed: 0.7, acurracy: 1.3, - OTHeRValue: 'Am I a Number?' - } + OTHeRValue: 'Am I a Number?', + }, }, lastSavedPosition: { timestamp: cachedValues.metadata.position.timestamp, @@ -631,19 +649,19 @@ export const fixtures = { altitude: 1000.1, heading: 0.2, speed: 0.1, - acurracy: 8.3 - } + acurracy: 8.3, + }, }, positionProvider: { gpsAvailable: true, passiveAvailable: false, locationServicesEnabled: true, evilLairLocationEnabled: 'NOT', - networkAvailable: false - } - } + networkAvailable: false, + }, + }, }, - expected:{ + expected: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'observation', @@ -653,11 +671,13 @@ export const fixtures = { lat: 24.0424, lon: 21.0214, refs: [], - attachments: [{ - name: 'myFile', - type: 'photo', - driveId: cachedValues.attachments.driveId - }], + attachments: [ + { + name: 'myFile', + type: 'photo', + driveId: cachedValues.attachments.driveId, + }, + ], tags: { someKeyForSingleVal: 'someVal', }, @@ -672,8 +692,8 @@ export const fixtures = { altitude: 0.8, heading: 1.2, speed: 0.7, - acurracy: 1.3 - } + acurracy: 1.3, + }, }, lastSavedPosition: { timestamp: cachedValues.metadata.position.timestamp, @@ -684,20 +704,20 @@ export const fixtures = { altitude: 1000.1, heading: 0.2, speed: 0.1, - acurracy: 8.3 - } + acurracy: 8.3, + }, }, positionProvider: { gpsAvailable: true, passiveAvailable: false, locationServicesEnabled: true, - networkAvailable: false - } + networkAvailable: false, + }, }, - } - }, + }, + }, { - doc:{ + doc: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'project', @@ -711,11 +731,11 @@ export const fixtures = { vertex: cachedValues.defaultPresets.point, line: cachedValues.defaultPresets.point, relation: cachedValues.defaultPresets.point, - way: 'I DONT EXIST YET??' + way: 'I DONT EXIST YET??', }, - name: 'myProject' + name: 'myProject', }, - expected:{ + expected: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'project', @@ -729,11 +749,11 @@ export const fixtures = { line: cachedValues.defaultPresets.point, relation: cachedValues.defaultPresets.point, }, - name: 'myProject' - } + name: 'myProject', + }, }, { - doc:{ + doc: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'field', @@ -747,32 +767,37 @@ export const fixtures = { appearance: 'singleline', snakeCase: true, universal: true, - options: [{ - centipede: 'big', - label: 'someOtherLabel', - value: 'somePrimitiveTagValue' - }], + options: [ + { + centipede: 'big', + label: 'someOtherLabel', + value: 'somePrimitiveTagValue', + }, + ], }, - expected:{ + expected: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'field', createdAt: cachedValues.createdAt, updatedAt: cachedValues.updatedAt, links: [], - tagKey: "otherTagKey", - type: "number", - label: "differentLabel", + tagKey: 'otherTagKey', + type: 'number', + label: 'differentLabel', appearance: 'singleline', snakeCase: true, universal: true, - options: [{ - label: 'someOtherLabel', - value: 'somePrimitiveTagValue' - }], - }}, + options: [ + { + label: 'someOtherLabel', + value: 'somePrimitiveTagValue', + }, + ], + }, + }, { - doc:{ + doc: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'preset', @@ -780,81 +805,82 @@ export const fixtures = { createdAt: cachedValues.createdAt, updatedAt: cachedValues.updatedAt, links: [], - name:"myPreset", - geometry:["point", "vertex", "line"], + name: 'myPreset', + geometry: ['point', 'vertex', 'line'], tags: { - someKeyForArrVal: ['arr', 'of', 'strings'] + someKeyForArrVal: ['arr', 'of', 'strings'], }, - addTags:{ - heyAddThisTag: 'pleease' + addTags: { + heyAddThisTag: 'pleease', }, - removeTags:{ - deleteInmeditaly: ['this list', 'of things'] + removeTags: { + deleteInmeditaly: ['this list', 'of things'], }, - fieldIds:cachedValues.fieldIds, + fieldIds: cachedValues.fieldIds, iconId: cachedValues.iconId, - terms:["imastring"] + terms: ['imastring'], }, - expected:{ + expected: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'preset', createdAt: cachedValues.createdAt, updatedAt: cachedValues.updatedAt, links: [], - name:"myPreset", - geometry:["point", "vertex", "line"], + name: 'myPreset', + geometry: ['point', 'vertex', 'line'], tags: { - someKeyForArrVal: ['arr', 'of', 'strings'] + someKeyForArrVal: ['arr', 'of', 'strings'], }, - addTags:{ - heyAddThisTag: 'pleease' + addTags: { + heyAddThisTag: 'pleease', }, - removeTags:{ - deleteInmeditaly: ['this list', 'of things'] + removeTags: { + deleteInmeditaly: ['this list', 'of things'], }, - fieldIds:cachedValues.fieldIds, + fieldIds: cachedValues.fieldIds, iconId: cachedValues.iconId, - terms:["imastring"] - } + terms: ['imastring'], + }, }, { - doc:{ + doc: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'role', - areYouMasterOfTheUniverse: 'js doesnt allow question marks in identifiers, which saddens me...', + areYouMasterOfTheUniverse: + 'js doesnt allow question marks in identifiers, which saddens me...', createdAt: cachedValues.createdAt, updatedAt: cachedValues.updatedAt, links: [], - role:"author", + role: 'author', projectId: cachedValues.projectId, action: 'role_set', - signature:"signature", - authorIndex:8, - deviceIndex:7, + signature: 'signature', + authorIndex: 8, + deviceIndex: 7, authorId: cachedValues.authorId, - capabilityType:"capability" + capabilityType: 'capability', }, - expected:{ + expected: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'role', createdAt: cachedValues.createdAt, updatedAt: cachedValues.updatedAt, links: [], - role:"author", + role: 'author', projectId: cachedValues.projectId, action: 'role_set', - signature:"signature", - authorIndex:8, - deviceIndex:7, - authorId:cachedValues.authorId, - capabilityType:"capability" - } + signature: 'signature', + authorIndex: 8, + deviceIndex: 7, + authorId: cachedValues.authorId, + capabilityType: 'capability', + }, }, { - doc:{ + doc: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'device', @@ -862,32 +888,32 @@ export const fixtures = { createdAt: cachedValues.createdAt, updatedAt: cachedValues.updatedAt, links: [], - action:"ban", + action: 'ban', authorId: cachedValues.authorId, projectId: cachedValues.projectId, - signature:"mySignature", - authorIndex:1, - deviceIndex:10, - capabilityType:"someCapability", + signature: 'mySignature', + authorIndex: 1, + deviceIndex: 10, + capabilityType: 'someCapability', }, - expected:{ + expected: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'device', createdAt: cachedValues.createdAt, updatedAt: cachedValues.updatedAt, links: [], - action:"ban", + action: 'ban', authorId: cachedValues.authorId, projectId: cachedValues.projectId, - signature:"mySignature", - authorIndex:1, - deviceIndex:10, - capabilityType:"someCapability", - } + signature: 'mySignature', + authorIndex: 1, + deviceIndex: 10, + capabilityType: 'someCapability', + }, }, { - doc:{ + doc: { docId: cachedValues.docId, versionId: cachedValues.versionId, NOBODYOWNSANYTHINGOK: ':|', @@ -895,33 +921,33 @@ export const fixtures = { createdAt: cachedValues.createdAt, updatedAt: cachedValues.updatedAt, links: [], - action:"remove", + action: 'remove', coreId: cachedValues.coreId, projectId: cachedValues.projectId, - storeType:"blob", - signature:"mySig", + storeType: 'blob', + signature: 'mySig', authorIndex: 100, deviceIndex: 2, authorId: cachedValues.authorId, - capabilityType: "someCapability" + capabilityType: 'someCapability', }, - expected:{ + expected: { docId: cachedValues.docId, versionId: cachedValues.versionId, schemaName: 'coreOwnership', createdAt: cachedValues.createdAt, updatedAt: cachedValues.updatedAt, links: [], - action:"remove", + action: 'remove', coreId: cachedValues.coreId, projectId: cachedValues.projectId, - storeType:"blob", - signature:"mySig", + storeType: 'blob', + signature: 'mySig', authorIndex: 100, deviceIndex: 2, authorId: cachedValues.authorId, - capabilityType: "someCapability" - } - } - ] + capabilityType: 'someCapability', + }, + }, + ], } diff --git a/test/index.js b/test/index.js index ac8c957c..efb93875 100644 --- a/test/index.js +++ b/test/index.js @@ -7,8 +7,8 @@ import { and } from 'ajv/dist/compile/codegen/index.js' // import { randomBytes } from 'node:crypto' { - const {text,plan,doc} = fixtures.onlyId - test(text, async(t) => { + const { text, plan, doc } = fixtures.onlyId + test(text, async (t) => { t.plan(plan) t.throws(() => { encode(doc) @@ -17,8 +17,8 @@ import { and } from 'ajv/dist/compile/codegen/index.js' } { - const {text,plan, doc} = fixtures.badDocName - test(text,async(t) => { + const { text, plan, doc } = fixtures.badDocName + test(text, async (t) => { t.plan(plan) t.throws(() => { encode(doc) @@ -27,42 +27,51 @@ import { and } from 'ajv/dist/compile/codegen/index.js' } test(`testing encoding of doc with minimal required values, - then decoding and comparing the two objects`, - async(t) => { - for(let {doc,expected} of fixtures.goodDocsMinimal){ - let buf - t.doesNotThrow(() => { - buf = encode(doc) - }, `tested encoding of ${doc.schemaName}`) - let decodedDoc = stripUndef(decode(buf, parseVersionId(doc.versionId))) - t.deepEqual(decodedDoc,expected, `tested deep equal of ${doc.schemaName} with only required values`) - } + then decoding and comparing the two objects`, async (t) => { + for (let { doc, expected } of fixtures.goodDocsMinimal) { + let buf + t.doesNotThrow(() => { + buf = encode(doc) + }, `tested encoding of ${doc.schemaName}`) + let decodedDoc = stripUndef(decode(buf, parseVersionId(doc.versionId))) + t.deepEqual( + decodedDoc, + expected, + `tested deep equal of ${doc.schemaName} with only required values` + ) + } }) test(`testing encoding of doc with additional optional values, - then decoding and comparing the two objects`, - async(t) => { - for(let {doc,expected} of fixtures.goodDocsCompleted){ - let buf - t.doesNotThrow(() => { - buf = encode(doc) - }, `tested encoding of ${doc.schemaName}`) - let decodedDoc = stripUndef(decode(buf, parseVersionId(doc.versionId))) - t.deepEqual(decodedDoc,expected, `tested deep equal of ${doc.schemaName} with additional values`) - } + then decoding and comparing the two objects`, async (t) => { + for (let { doc, expected } of fixtures.goodDocsCompleted) { + let buf + t.doesNotThrow(() => { + buf = encode(doc) + }, `tested encoding of ${doc.schemaName}`) + let decodedDoc = stripUndef(decode(buf, parseVersionId(doc.versionId))) + t.deepEqual( + decodedDoc, + expected, + `tested deep equal of ${doc.schemaName} with additional values` + ) + } }) test(`testing encoding of doc with additional extra values, -then decoding and comparing the two objects - extra values shouldn't be present`, - async(t) => { - for(let {doc,expected} of fixtures.goodDocsWithExtraFields){ - let buf - t.doesNotThrow(() => { - buf = encode(doc) - }, `tested encoding of ${doc.schemaName}`) - let decodedDoc = stripUndef(decode(buf, parseVersionId(doc.versionId))) - t.deepEqual(decodedDoc,expected, `tested deep equal of ${doc.schemaName} with extra - non valid - values`) - } +then decoding and comparing the two objects - extra values shouldn't be present`, async (t) => { + for (let { doc, expected } of fixtures.goodDocsWithExtraFields) { + let buf + t.doesNotThrow(() => { + buf = encode(doc) + }, `tested encoding of ${doc.schemaName}`) + let decodedDoc = stripUndef(decode(buf, parseVersionId(doc.versionId))) + t.deepEqual( + decodedDoc, + expected, + `tested deep equal of ${doc.schemaName} with extra - non valid - values` + ) + } }) /** From f2dff2e3f12af99d263587a5bb4ce9ee28b68a03 Mon Sep 17 00:00:00 2001 From: Gregor MacLennan Date: Tue, 15 Aug 2023 10:08:01 +0100 Subject: [PATCH 14/23] remove unused import --- test/fixture.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/fixture.js b/test/fixture.js index 92e4e7d3..81c1f6af 100644 --- a/test/fixture.js +++ b/test/fixture.js @@ -1,5 +1,4 @@ import { randomBytes } from 'node:crypto' -import { cachedDataVersionTag } from 'node:v8' // For stuff like dates or generated random Ids, so I can compare with an expected doc const date = new Date().toJSON() From fa016ac1310ace4850ef7ce8441deedf07830da3 Mon Sep 17 00:00:00 2001 From: Gregor MacLennan Date: Tue, 15 Aug 2023 10:12:54 +0100 Subject: [PATCH 15/23] Make badDocs test more DRY --- test/fixture.js | 38 +++++++++++++++++++------------------- test/index.js | 25 ++++++------------------- 2 files changed, 25 insertions(+), 38 deletions(-) diff --git a/test/fixture.js b/test/fixture.js index 81c1f6af..cca1429b 100644 --- a/test/fixture.js +++ b/test/fixture.js @@ -28,26 +28,26 @@ const cachedValues = { } export const fixtures = { - onlyId: { - text: 'test encoding of record with missing fields', - plan: 1, - doc: { docId: randomBytes(32).toString('hex') }, - }, - badDocName: { - text: 'test encoding of wrong record type', - plan: 1, - doc: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'observOtion', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - refs: [], - attachments: [], - tags: {}, + badDocs: [ + { + text: 'test encoding of record with missing fields', + doc: { docId: randomBytes(32).toString('hex') }, }, - }, + { + text: 'test encoding of wrong record type', + doc: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'observOtion', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + refs: [], + attachments: [], + tags: {}, + }, + }, + ], /** @type {Array<{ * expected:import('../dist/types').MapeoDoc, * doc: import('../dist/types').MapeoDoc}> diff --git a/test/index.js b/test/index.js index efb93875..4ce947b3 100644 --- a/test/index.js +++ b/test/index.js @@ -3,28 +3,15 @@ import { fixtures } from './fixture.js' import { encode } from '../dist/encode.js' import { decode } from '../dist/decode.js' import { parseVersionId } from '../dist/lib/utils.js' -import { and } from 'ajv/dist/compile/codegen/index.js' -// import { randomBytes } from 'node:crypto' -{ - const { text, plan, doc } = fixtures.onlyId - test(text, async (t) => { - t.plan(plan) +test('Bad docs throw when encoding', (t) => { + for (const { text, doc } of fixtures.badDocs) { t.throws(() => { encode(doc) - }) - }) -} - -{ - const { text, plan, doc } = fixtures.badDocName - test(text, async (t) => { - t.plan(plan) - t.throws(() => { - encode(doc) - }) - }) -} + }, text) + } + t.end() +}) test(`testing encoding of doc with minimal required values, then decoding and comparing the two objects`, async (t) => { From 0ded478853b533be7c683ae0807f97fcacf8130e Mon Sep 17 00:00:00 2001 From: Gregor MacLennan Date: Tue, 15 Aug 2023 10:13:42 +0100 Subject: [PATCH 16/23] remove unused file --- test/docs.js | 104 --------------------------------------------------- 1 file changed, 104 deletions(-) delete mode 100644 test/docs.js diff --git a/test/docs.js b/test/docs.js deleted file mode 100644 index e6553431..00000000 --- a/test/docs.js +++ /dev/null @@ -1,104 +0,0 @@ -import { randomBytes } from 'node:crypto' - -export const docs = { - onlyId: { id: randomBytes(32).toString('hex') }, - badDocType: { - id: randomBytes(32).toString('hex'), - schemaType: 'doesnotexist', - links: [], - createdAt: new Date().toJSON(), - refs: [], - attachments: [], - metadata: { - manual_location: true, - }, - }, - badSchemaVersion: { - id: randomBytes(32).toString('hex'), - schemaType: 'observation', - schemaVersion: 2, - links: [], - createdAt: new Date().toJSON(), - refs: [], - attachments: [], - metadata: { - manual_location: true, - }, - }, - good: { - observation_5: { - id: randomBytes(32).toString('hex'), - schemaType: 'observation', - createdAt: new Date().toJSON(), - updatedAt: new Date().toJSON(), - links: [], - tags: { fields: { myTag: 10 } }, - }, - preset: { - id: randomBytes(32).toString('hex'), - createdAt: new Date().toJSON(), - updatedAt: new Date().toJSON(), - schemaType: 'preset', - links: [], - tags: { nature: 'tree' }, - geometry: ['point'], - name: 'john', - }, - field: { - id: randomBytes(32).toString('hex'), - createdAt: new Date().toJSON(), - updatedAt: new Date().toJSON(), - schemaType: 'field', - tagKey: 'hi', - type: 'text', - links: [], - }, - coreOwnership: { - schemaType: 'coreOwnership', - id: randomBytes(32).toString('hex'), - coreId: randomBytes(32).toString('hex'), - projectId: randomBytes(32).toString('hex'), - storeType: 'blob', - authorIndex: 10, - deviceIndex: 10, - action: 'core:owner', - createdAt: new Date().toJSON(), - updatedAt: new Date().toJSON(), - links: [], - }, - device: { - schemaType: 'device', - id: randomBytes(32).toString('hex'), - action: 'device:add', - authorId: randomBytes(32).toString('hex'), - projectId: randomBytes(32).toString('hex'), - signature: 'hi', - authorIndex: 10, - deviceIndex: 10, - createdAt: new Date().toJSON(), - updatedAt: new Date().toJSON(), - links: [], - }, - role: { - id: randomBytes(32).toString('hex'), - schemaType: 'role', - role: 'project-creator', - projectId: randomBytes(32).toString('hex'), - action: 'role:set', - signature: 'hi', - authorIndex: 10, - deviceIndex: 10, - createdAt: new Date().toJSON(), - updatedAt: new Date().toJSON(), - links: [], - }, - project: { - id: randomBytes(32).toString('hex'), - schemaType: 'project', - createdAt: new Date().toJSON(), - updatedAt: new Date().toJSON(), - name: 'My Project', - links: [], - }, - }, -} From d8b508da0cf651c4dcf2ec7291a4c5b18db611f4 Mon Sep 17 00:00:00 2001 From: Gregor MacLennan Date: Tue, 15 Aug 2023 10:30:41 +0100 Subject: [PATCH 17/23] move fixtures into separate files --- test/fixture.js | 952 ------------------------ test/fixtures/bad-docs.js | 26 + test/fixtures/cached.js | 28 + test/fixtures/good-docs-completed.js | 350 +++++++++ test/fixtures/good-docs-extra-fields.js | 359 +++++++++ test/fixtures/good-docs-minimal.js | 217 ++++++ test/fixtures/index.js | 4 + test/index.js | 22 +- 8 files changed, 998 insertions(+), 960 deletions(-) delete mode 100644 test/fixture.js create mode 100644 test/fixtures/bad-docs.js create mode 100644 test/fixtures/cached.js create mode 100644 test/fixtures/good-docs-completed.js create mode 100644 test/fixtures/good-docs-extra-fields.js create mode 100644 test/fixtures/good-docs-minimal.js create mode 100644 test/fixtures/index.js diff --git a/test/fixture.js b/test/fixture.js deleted file mode 100644 index cca1429b..00000000 --- a/test/fixture.js +++ /dev/null @@ -1,952 +0,0 @@ -import { randomBytes } from 'node:crypto' - -// For stuff like dates or generated random Ids, so I can compare with an expected doc -const date = new Date().toJSON() -const cachedValues = { - docId: randomBytes(32).toString('hex'), - versionId: `${randomBytes(32).toString('hex')}/0`, - projectId: randomBytes(32).toString('hex'), - authorId: randomBytes(32).toString('hex'), - coreId: randomBytes(32).toString('hex'), - createdAt: date, - updatedAt: date, - attachments: { driveId: randomBytes(32).toString('hex') }, - metadata: { - position: { - timestamp: date, - }, - }, - defaultPresets: { - point: [randomBytes(32).toString('hex')], - area: [randomBytes(32).toString('hex')], - vertex: [randomBytes(32).toString('hex')], - line: [randomBytes(32).toString('hex')], - relation: [randomBytes(32).toString('hex')], - }, - fieldIds: [randomBytes(32).toString('hex')], - iconId: randomBytes(32).toString('hex'), -} - -export const fixtures = { - badDocs: [ - { - text: 'test encoding of record with missing fields', - doc: { docId: randomBytes(32).toString('hex') }, - }, - { - text: 'test encoding of wrong record type', - doc: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'observOtion', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - refs: [], - attachments: [], - tags: {}, - }, - }, - ], - /** @type {Array<{ - * expected:import('../dist/types').MapeoDoc, - * doc: import('../dist/types').MapeoDoc}> - * } */ - goodDocsMinimal: [ - { - doc: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'observation', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - refs: [], - attachments: [], - tags: {}, - }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'observation', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - refs: [], - attachments: [], - tags: {}, - metadata: {}, - }, - }, - { - doc: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'project', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - defaultPresets: {}, - name: 'myProject', - }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'project', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - defaultPresets: { - point: [], - area: [], - vertex: [], - line: [], - relation: [], - }, - name: 'myProject', - }, - }, - { - doc: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'field', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - tagKey: 'myTagKey', - }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'field', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - tagKey: 'myTagKey', - type: 'UNRECOGNIZED', - label: 'myTagKey', // see src/lib/decode-conversions.js:77 - appearance: 'multiline', - snakeCase: false, - options: [], - universal: false, - }, - }, - { - doc: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'preset', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - name: 'myPreset', - geometry: ['point'], - tags: {}, - addTags: {}, - removeTags: {}, - fieldIds: [], - terms: [], - }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'preset', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - name: 'myPreset', - geometry: ['point'], - tags: {}, - addTags: {}, - removeTags: {}, - fieldIds: [], - terms: [], - }, - }, - { - doc: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'role', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - role: 'author', - projectId: cachedValues.projectId, - signature: 'signature', - authorId: cachedValues.authorId, - capabilityType: 'capability', - }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'role', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - role: 'author', - projectId: cachedValues.projectId, - action: 'UNRECOGNIZED', - signature: 'signature', - authorIndex: 0, - deviceIndex: 0, - authorId: cachedValues.authorId, - capabilityType: 'capability', - }, - }, - { - doc: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'device', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - action: 'ban', - authorId: cachedValues.authorId, - projectId: cachedValues.projectId, - signature: 'mySignature', - capabilityType: 'someCapability', - }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'device', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - action: 'ban', - authorId: cachedValues.authorId, - projectId: cachedValues.projectId, - signature: 'mySignature', - authorIndex: 0, - deviceIndex: 0, - capabilityType: 'someCapability', - }, - }, - { - doc: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'coreOwnership', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - action: 'remove', - coreId: cachedValues.coreId, - projectId: cachedValues.projectId, - storeType: 'blob', - signature: 'mySig', - authorId: cachedValues.authorId, - capabilityType: 'someCapability', - }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'coreOwnership', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - action: 'remove', - coreId: cachedValues.coreId, - projectId: cachedValues.projectId, - storeType: 'blob', - signature: 'mySig', - authorIndex: 0, - deviceIndex: 0, - authorId: cachedValues.authorId, - capabilityType: 'someCapability', - }, - }, - ], - goodDocsCompleted: [ - { - doc: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'observation', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - lat: 24.0424, - lon: 21.0214, - refs: [], - attachments: [ - { - name: 'myFile', - type: 'photo', - driveId: cachedValues.attachments.driveId, - }, - ], - tags: { - someKeyForSingleVal: 'someVal', - someKeyForArrVal: ['arr', 'of', 'strings'], - }, - metadata: { - manualLocation: true, - position: { - timestamp: cachedValues.metadata.position.timestamp, - mocked: false, - coords: { - latitude: 0.5, - longitude: -0.2, - altitude: 0.8, - heading: 1.2, - speed: 0.7, - acurracy: 1.3, - }, - }, - lastSavedPosition: { - timestamp: cachedValues.metadata.position.timestamp, - mocked: true, - coords: { - latitude: 1.5, - longitude: -2.3, - altitude: 1000.1, - heading: 0.2, - speed: 0.1, - acurracy: 8.3, - }, - }, - positionProvider: { - gpsAvailable: true, - passiveAvailable: false, - locationServicesEnabled: true, - networkAvailable: false, - }, - }, - }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'observation', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - lat: 24.0424, - lon: 21.0214, - refs: [], - attachments: [ - { - name: 'myFile', - type: 'photo', - driveId: cachedValues.attachments.driveId, - }, - ], - tags: { - someKeyForSingleVal: 'someVal', - someKeyForArrVal: ['arr', 'of', 'strings'], - }, - metadata: { - manualLocation: true, - position: { - timestamp: cachedValues.metadata.position.timestamp, - mocked: false, - coords: { - latitude: 0.5, - longitude: -0.2, - altitude: 0.8, - heading: 1.2, - speed: 0.7, - acurracy: 1.3, - }, - }, - lastSavedPosition: { - timestamp: cachedValues.metadata.position.timestamp, - mocked: true, - coords: { - latitude: 1.5, - longitude: -2.3, - altitude: 1000.1, - heading: 0.2, - speed: 0.1, - acurracy: 8.3, - }, - }, - positionProvider: { - gpsAvailable: true, - passiveAvailable: false, - locationServicesEnabled: true, - networkAvailable: false, - }, - }, - }, - }, - { - doc: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'project', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - defaultPresets: { - point: cachedValues.defaultPresets.point, - area: cachedValues.defaultPresets.point, - vertex: cachedValues.defaultPresets.point, - line: cachedValues.defaultPresets.point, - relation: cachedValues.defaultPresets.point, - }, - name: 'myProject', - }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'project', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - defaultPresets: { - point: cachedValues.defaultPresets.point, - area: cachedValues.defaultPresets.point, - vertex: cachedValues.defaultPresets.point, - line: cachedValues.defaultPresets.point, - relation: cachedValues.defaultPresets.point, - }, - name: 'myProject', - }, - }, - { - doc: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'field', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - tagKey: 'otherTagKey', - type: 'number', - label: 'differentLabel', - appearance: 'singleline', - snakeCase: true, - universal: true, - options: [ - { - label: 'someOtherLabel', - value: 'somePrimitiveTagValue', - }, - ], - }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'field', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - tagKey: 'otherTagKey', - type: 'number', - label: 'differentLabel', - appearance: 'singleline', - snakeCase: true, - universal: true, - options: [ - { - label: 'someOtherLabel', - value: 'somePrimitiveTagValue', - }, - ], - }, - }, - { - doc: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'preset', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - name: 'myPreset', - geometry: ['point', 'vertex', 'line'], - tags: { - someKeyForArrVal: ['arr', 'of', 'strings'], - }, - addTags: { - heyAddThisTag: 'pleease', - orMaybeThis: ['right?', '', 'that was empty'], - }, - removeTags: { - deleteInmeditaly: ['this list', 'of things'], - }, - fieldIds: cachedValues.fieldIds, - iconId: cachedValues.iconId, - terms: ['imastring'], - }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'preset', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - name: 'myPreset', - geometry: ['point', 'vertex', 'line'], - tags: { - someKeyForArrVal: ['arr', 'of', 'strings'], - }, - addTags: { - heyAddThisTag: 'pleease', - orMaybeThis: ['right?', '', 'that was empty'], - }, - removeTags: { - deleteInmeditaly: ['this list', 'of things'], - }, - fieldIds: cachedValues.fieldIds, - iconId: cachedValues.iconId, - terms: ['imastring'], - }, - }, - { - doc: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'role', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - role: 'author', - projectId: cachedValues.projectId, - action: 'role_set', - signature: 'signature', - authorIndex: 8, - deviceIndex: 7, - authorId: cachedValues.authorId, - capabilityType: 'capability', - }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'role', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - role: 'author', - projectId: cachedValues.projectId, - action: 'role_set', - signature: 'signature', - authorIndex: 8, - deviceIndex: 7, - authorId: cachedValues.authorId, - capabilityType: 'capability', - }, - }, - { - doc: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'device', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - action: 'ban', - authorId: cachedValues.authorId, - projectId: cachedValues.projectId, - signature: 'mySignature', - authorIndex: 1, - deviceIndex: 10, - capabilityType: 'someCapability', - }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'device', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - action: 'ban', - authorId: cachedValues.authorId, - projectId: cachedValues.projectId, - signature: 'mySignature', - authorIndex: 1, - deviceIndex: 10, - capabilityType: 'someCapability', - }, - }, - { - doc: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'coreOwnership', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - action: 'remove', - coreId: cachedValues.coreId, - projectId: cachedValues.projectId, - storeType: 'blob', - signature: 'mySig', - authorIndex: 100, - deviceIndex: 2, - authorId: cachedValues.authorId, - capabilityType: 'someCapability', - }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'coreOwnership', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - action: 'remove', - coreId: cachedValues.coreId, - projectId: cachedValues.projectId, - storeType: 'blob', - signature: 'mySig', - authorIndex: 100, - deviceIndex: 2, - authorId: cachedValues.authorId, - capabilityType: 'someCapability', - }, - }, - ], - goodDocsWithExtraFields: [ - { - doc: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'observation', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - zodiacSign: 'banana', - links: [], - lat: 24.0424, - lon: 21.0214, - refs: [], - attachments: [ - { - name: 'myFile', - type: 'photo', - driveId: cachedValues.attachments.driveId, - }, - ], - tags: { - someKeyForSingleVal: 'someVal', - }, - metadata: { - manualLocation: true, - position: { - IMInc0rRecT: Array(100).fill(Math.random() * 1000), - timestamp: cachedValues.metadata.position.timestamp, - mocked: false, - coords: { - latitude: 0.5, - longitude: -0.2, - altitude: 0.8, - heading: 1.2, - speed: 0.7, - acurracy: 1.3, - OTHeRValue: 'Am I a Number?', - }, - }, - lastSavedPosition: { - timestamp: cachedValues.metadata.position.timestamp, - mocked: true, - coords: { - latitude: 1.5, - longitude: -2.3, - altitude: 1000.1, - heading: 0.2, - speed: 0.1, - acurracy: 8.3, - }, - }, - positionProvider: { - gpsAvailable: true, - passiveAvailable: false, - locationServicesEnabled: true, - evilLairLocationEnabled: 'NOT', - networkAvailable: false, - }, - }, - }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'observation', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - lat: 24.0424, - lon: 21.0214, - refs: [], - attachments: [ - { - name: 'myFile', - type: 'photo', - driveId: cachedValues.attachments.driveId, - }, - ], - tags: { - someKeyForSingleVal: 'someVal', - }, - metadata: { - manualLocation: true, - position: { - timestamp: cachedValues.metadata.position.timestamp, - mocked: false, - coords: { - latitude: 0.5, - longitude: -0.2, - altitude: 0.8, - heading: 1.2, - speed: 0.7, - acurracy: 1.3, - }, - }, - lastSavedPosition: { - timestamp: cachedValues.metadata.position.timestamp, - mocked: true, - coords: { - latitude: 1.5, - longitude: -2.3, - altitude: 1000.1, - heading: 0.2, - speed: 0.1, - acurracy: 8.3, - }, - }, - positionProvider: { - gpsAvailable: true, - passiveAvailable: false, - locationServicesEnabled: true, - networkAvailable: false, - }, - }, - }, - }, - { - doc: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'project', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - numberOfPeople: 'glove', - defaultPresets: { - point: cachedValues.defaultPresets.point, - area: cachedValues.defaultPresets.point, - vertex: cachedValues.defaultPresets.point, - line: cachedValues.defaultPresets.point, - relation: cachedValues.defaultPresets.point, - way: 'I DONT EXIST YET??', - }, - name: 'myProject', - }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'project', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - defaultPresets: { - point: cachedValues.defaultPresets.point, - area: cachedValues.defaultPresets.point, - vertex: cachedValues.defaultPresets.point, - line: cachedValues.defaultPresets.point, - relation: cachedValues.defaultPresets.point, - }, - name: 'myProject', - }, - }, - { - doc: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'field', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - destroyedAt: new Date().toJSON(), - links: [], - tagKey: 'otherTagKey', - type: 'number', - label: 'differentLabel', - appearance: 'singleline', - snakeCase: true, - universal: true, - options: [ - { - centipede: 'big', - label: 'someOtherLabel', - value: 'somePrimitiveTagValue', - }, - ], - }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'field', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - tagKey: 'otherTagKey', - type: 'number', - label: 'differentLabel', - appearance: 'singleline', - snakeCase: true, - universal: true, - options: [ - { - label: 'someOtherLabel', - value: 'somePrimitiveTagValue', - }, - ], - }, - }, - { - doc: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'preset', - keyboardSound: 'rad', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - name: 'myPreset', - geometry: ['point', 'vertex', 'line'], - tags: { - someKeyForArrVal: ['arr', 'of', 'strings'], - }, - addTags: { - heyAddThisTag: 'pleease', - }, - removeTags: { - deleteInmeditaly: ['this list', 'of things'], - }, - fieldIds: cachedValues.fieldIds, - iconId: cachedValues.iconId, - terms: ['imastring'], - }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'preset', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - name: 'myPreset', - geometry: ['point', 'vertex', 'line'], - tags: { - someKeyForArrVal: ['arr', 'of', 'strings'], - }, - addTags: { - heyAddThisTag: 'pleease', - }, - removeTags: { - deleteInmeditaly: ['this list', 'of things'], - }, - fieldIds: cachedValues.fieldIds, - iconId: cachedValues.iconId, - terms: ['imastring'], - }, - }, - { - doc: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'role', - areYouMasterOfTheUniverse: - 'js doesnt allow question marks in identifiers, which saddens me...', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - role: 'author', - projectId: cachedValues.projectId, - action: 'role_set', - signature: 'signature', - authorIndex: 8, - deviceIndex: 7, - authorId: cachedValues.authorId, - capabilityType: 'capability', - }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'role', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - role: 'author', - projectId: cachedValues.projectId, - action: 'role_set', - signature: 'signature', - authorIndex: 8, - deviceIndex: 7, - authorId: cachedValues.authorId, - capabilityType: 'capability', - }, - }, - { - doc: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'device', - programmedObsolescense: 'well, duh...', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - action: 'ban', - authorId: cachedValues.authorId, - projectId: cachedValues.projectId, - signature: 'mySignature', - authorIndex: 1, - deviceIndex: 10, - capabilityType: 'someCapability', - }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'device', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - action: 'ban', - authorId: cachedValues.authorId, - projectId: cachedValues.projectId, - signature: 'mySignature', - authorIndex: 1, - deviceIndex: 10, - capabilityType: 'someCapability', - }, - }, - { - doc: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - NOBODYOWNSANYTHINGOK: ':|', - schemaName: 'coreOwnership', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - action: 'remove', - coreId: cachedValues.coreId, - projectId: cachedValues.projectId, - storeType: 'blob', - signature: 'mySig', - authorIndex: 100, - deviceIndex: 2, - authorId: cachedValues.authorId, - capabilityType: 'someCapability', - }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'coreOwnership', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - action: 'remove', - coreId: cachedValues.coreId, - projectId: cachedValues.projectId, - storeType: 'blob', - signature: 'mySig', - authorIndex: 100, - deviceIndex: 2, - authorId: cachedValues.authorId, - capabilityType: 'someCapability', - }, - }, - ], -} diff --git a/test/fixtures/bad-docs.js b/test/fixtures/bad-docs.js new file mode 100644 index 00000000..083934c5 --- /dev/null +++ b/test/fixtures/bad-docs.js @@ -0,0 +1,26 @@ +// @ts-check +import { randomBytes } from 'node:crypto' +import { cachedValues } from './cached.js' + +export const badDocs = [ + { + text: 'test encoding of record with missing fields', + doc: { docId: randomBytes(32).toString('hex') }, + }, + { + text: 'test encoding of wrong schema name', + /** @type Omit & { schemaName: string }} */ + doc: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'observOtion', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + refs: [], + attachments: [], + tags: {}, + metadata: {}, + }, + }, +] diff --git a/test/fixtures/cached.js b/test/fixtures/cached.js new file mode 100644 index 00000000..58c5b950 --- /dev/null +++ b/test/fixtures/cached.js @@ -0,0 +1,28 @@ +import { randomBytes } from 'node:crypto' + +// For stuff like dates or generated random Ids, so I can compare with an expected doc +const date = new Date().toJSON() +export const cachedValues = { + docId: randomBytes(32).toString('hex'), + versionId: `${randomBytes(32).toString('hex')}/0`, + projectId: randomBytes(32).toString('hex'), + authorId: randomBytes(32).toString('hex'), + coreId: randomBytes(32).toString('hex'), + createdAt: date, + updatedAt: date, + attachments: { driveId: randomBytes(32).toString('hex') }, + metadata: { + position: { + timestamp: date, + }, + }, + defaultPresets: { + point: [randomBytes(32).toString('hex')], + area: [randomBytes(32).toString('hex')], + vertex: [randomBytes(32).toString('hex')], + line: [randomBytes(32).toString('hex')], + relation: [randomBytes(32).toString('hex')], + }, + fieldIds: [randomBytes(32).toString('hex')], + iconId: randomBytes(32).toString('hex'), +} diff --git a/test/fixtures/good-docs-completed.js b/test/fixtures/good-docs-completed.js new file mode 100644 index 00000000..4c047147 --- /dev/null +++ b/test/fixtures/good-docs-completed.js @@ -0,0 +1,350 @@ +// @ts-check + +import { cachedValues } from './cached.js' + +/** + * @type {Array<{ + * expected: import('../../dist/types').MapeoDoc, + * doc: import('../../dist/types').MapeoDoc + * }>} + */ +export const goodDocsCompleted = [ + { + doc: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'observation', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + lat: 24.0424, + lon: 21.0214, + refs: [], + attachments: [ + { + name: 'myFile', + type: 'photo', + driveId: cachedValues.attachments.driveId, + }, + ], + tags: { + someKeyForSingleVal: 'someVal', + someKeyForArrVal: ['arr', 'of', 'strings'], + }, + metadata: { + manualLocation: true, + position: { + timestamp: cachedValues.metadata.position.timestamp, + mocked: false, + coords: { + latitude: 0.5, + longitude: -0.2, + altitude: 0.8, + heading: 1.2, + speed: 0.7, + acurracy: 1.3, + }, + }, + lastSavedPosition: { + timestamp: cachedValues.metadata.position.timestamp, + mocked: true, + coords: { + latitude: 1.5, + longitude: -2.3, + altitude: 1000.1, + heading: 0.2, + speed: 0.1, + acurracy: 8.3, + }, + }, + positionProvider: { + gpsAvailable: true, + passiveAvailable: false, + locationServicesEnabled: true, + networkAvailable: false, + }, + }, + }, + expected: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'observation', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + lat: 24.0424, + lon: 21.0214, + refs: [], + attachments: [ + { + name: 'myFile', + type: 'photo', + driveId: cachedValues.attachments.driveId, + }, + ], + tags: { + someKeyForSingleVal: 'someVal', + someKeyForArrVal: ['arr', 'of', 'strings'], + }, + metadata: { + manualLocation: true, + position: { + timestamp: cachedValues.metadata.position.timestamp, + mocked: false, + coords: { + latitude: 0.5, + longitude: -0.2, + altitude: 0.8, + heading: 1.2, + speed: 0.7, + acurracy: 1.3, + }, + }, + lastSavedPosition: { + timestamp: cachedValues.metadata.position.timestamp, + mocked: true, + coords: { + latitude: 1.5, + longitude: -2.3, + altitude: 1000.1, + heading: 0.2, + speed: 0.1, + acurracy: 8.3, + }, + }, + positionProvider: { + gpsAvailable: true, + passiveAvailable: false, + locationServicesEnabled: true, + networkAvailable: false, + }, + }, + }, + }, + { + doc: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'project', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + defaultPresets: { + point: cachedValues.defaultPresets.point, + area: cachedValues.defaultPresets.point, + vertex: cachedValues.defaultPresets.point, + line: cachedValues.defaultPresets.point, + relation: cachedValues.defaultPresets.point, + }, + name: 'myProject', + }, + expected: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'project', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + defaultPresets: { + point: cachedValues.defaultPresets.point, + area: cachedValues.defaultPresets.point, + vertex: cachedValues.defaultPresets.point, + line: cachedValues.defaultPresets.point, + relation: cachedValues.defaultPresets.point, + }, + name: 'myProject', + }, + }, + { + doc: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'field', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + tagKey: 'otherTagKey', + type: 'number', + label: 'differentLabel', + appearance: 'singleline', + snakeCase: true, + universal: true, + options: [ + { + label: 'someOtherLabel', + value: 'somePrimitiveTagValue', + }, + ], + }, + expected: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'field', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + tagKey: 'otherTagKey', + type: 'number', + label: 'differentLabel', + appearance: 'singleline', + snakeCase: true, + universal: true, + options: [ + { + label: 'someOtherLabel', + value: 'somePrimitiveTagValue', + }, + ], + }, + }, + { + doc: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'preset', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + name: 'myPreset', + geometry: ['point', 'vertex', 'line'], + tags: { + someKeyForArrVal: ['arr', 'of', 'strings'], + }, + addTags: { + heyAddThisTag: 'pleease', + orMaybeThis: ['right?', '', 'that was empty'], + }, + removeTags: { + deleteInmeditaly: ['this list', 'of things'], + }, + fieldIds: cachedValues.fieldIds, + iconId: cachedValues.iconId, + terms: ['imastring'], + }, + expected: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'preset', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + name: 'myPreset', + geometry: ['point', 'vertex', 'line'], + tags: { + someKeyForArrVal: ['arr', 'of', 'strings'], + }, + addTags: { + heyAddThisTag: 'pleease', + orMaybeThis: ['right?', '', 'that was empty'], + }, + removeTags: { + deleteInmeditaly: ['this list', 'of things'], + }, + fieldIds: cachedValues.fieldIds, + iconId: cachedValues.iconId, + terms: ['imastring'], + }, + }, + { + doc: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'role', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + role: 'author', + projectId: cachedValues.projectId, + action: 'role_set', + signature: 'signature', + authorIndex: 8, + deviceIndex: 7, + authorId: cachedValues.authorId, + capabilityType: 'capability', + }, + expected: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'role', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + role: 'author', + projectId: cachedValues.projectId, + action: 'role_set', + signature: 'signature', + authorIndex: 8, + deviceIndex: 7, + authorId: cachedValues.authorId, + capabilityType: 'capability', + }, + }, + { + doc: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'device', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + action: 'ban', + authorId: cachedValues.authorId, + projectId: cachedValues.projectId, + signature: 'mySignature', + authorIndex: 1, + deviceIndex: 10, + capabilityType: 'someCapability', + }, + expected: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'device', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + action: 'ban', + authorId: cachedValues.authorId, + projectId: cachedValues.projectId, + signature: 'mySignature', + authorIndex: 1, + deviceIndex: 10, + capabilityType: 'someCapability', + }, + }, + { + doc: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'coreOwnership', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + action: 'remove', + coreId: cachedValues.coreId, + projectId: cachedValues.projectId, + storeType: 'blob', + signature: 'mySig', + authorIndex: 100, + deviceIndex: 2, + authorId: cachedValues.authorId, + capabilityType: 'someCapability', + }, + expected: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'coreOwnership', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + action: 'remove', + coreId: cachedValues.coreId, + projectId: cachedValues.projectId, + storeType: 'blob', + signature: 'mySig', + authorIndex: 100, + deviceIndex: 2, + authorId: cachedValues.authorId, + capabilityType: 'someCapability', + }, + }, +] diff --git a/test/fixtures/good-docs-extra-fields.js b/test/fixtures/good-docs-extra-fields.js new file mode 100644 index 00000000..50c4a9e9 --- /dev/null +++ b/test/fixtures/good-docs-extra-fields.js @@ -0,0 +1,359 @@ +// @ts-check + +import { cachedValues } from './cached.js' + +/** + * @type {Array<{ + * expected: import('../../dist/types').MapeoDoc, + * doc: import('../../dist/types').MapeoDoc + * }>} + */ +export const goodDocsWithExtraFields = [ + { + doc: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'observation', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + zodiacSign: 'banana', + links: [], + lat: 24.0424, + lon: 21.0214, + refs: [], + attachments: [ + { + name: 'myFile', + type: 'photo', + driveId: cachedValues.attachments.driveId, + }, + ], + tags: { + someKeyForSingleVal: 'someVal', + }, + metadata: { + manualLocation: true, + position: { + IMInc0rRecT: Array(100).fill(Math.random() * 1000), + timestamp: cachedValues.metadata.position.timestamp, + mocked: false, + coords: { + latitude: 0.5, + longitude: -0.2, + altitude: 0.8, + heading: 1.2, + speed: 0.7, + acurracy: 1.3, + OTHeRValue: 'Am I a Number?', + }, + }, + lastSavedPosition: { + timestamp: cachedValues.metadata.position.timestamp, + mocked: true, + coords: { + latitude: 1.5, + longitude: -2.3, + altitude: 1000.1, + heading: 0.2, + speed: 0.1, + acurracy: 8.3, + }, + }, + positionProvider: { + gpsAvailable: true, + passiveAvailable: false, + locationServicesEnabled: true, + evilLairLocationEnabled: 'NOT', + networkAvailable: false, + }, + }, + }, + expected: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'observation', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + lat: 24.0424, + lon: 21.0214, + refs: [], + attachments: [ + { + name: 'myFile', + type: 'photo', + driveId: cachedValues.attachments.driveId, + }, + ], + tags: { + someKeyForSingleVal: 'someVal', + }, + metadata: { + manualLocation: true, + position: { + timestamp: cachedValues.metadata.position.timestamp, + mocked: false, + coords: { + latitude: 0.5, + longitude: -0.2, + altitude: 0.8, + heading: 1.2, + speed: 0.7, + acurracy: 1.3, + }, + }, + lastSavedPosition: { + timestamp: cachedValues.metadata.position.timestamp, + mocked: true, + coords: { + latitude: 1.5, + longitude: -2.3, + altitude: 1000.1, + heading: 0.2, + speed: 0.1, + acurracy: 8.3, + }, + }, + positionProvider: { + gpsAvailable: true, + passiveAvailable: false, + locationServicesEnabled: true, + networkAvailable: false, + }, + }, + }, + }, + { + doc: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'project', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + numberOfPeople: 'glove', + defaultPresets: { + point: cachedValues.defaultPresets.point, + area: cachedValues.defaultPresets.point, + vertex: cachedValues.defaultPresets.point, + line: cachedValues.defaultPresets.point, + relation: cachedValues.defaultPresets.point, + way: 'I DONT EXIST YET??', + }, + name: 'myProject', + }, + expected: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'project', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + defaultPresets: { + point: cachedValues.defaultPresets.point, + area: cachedValues.defaultPresets.point, + vertex: cachedValues.defaultPresets.point, + line: cachedValues.defaultPresets.point, + relation: cachedValues.defaultPresets.point, + }, + name: 'myProject', + }, + }, + { + doc: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'field', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + destroyedAt: new Date().toJSON(), + links: [], + tagKey: 'otherTagKey', + type: 'number', + label: 'differentLabel', + appearance: 'singleline', + snakeCase: true, + universal: true, + options: [ + { + centipede: 'big', + label: 'someOtherLabel', + value: 'somePrimitiveTagValue', + }, + ], + }, + expected: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'field', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + tagKey: 'otherTagKey', + type: 'number', + label: 'differentLabel', + appearance: 'singleline', + snakeCase: true, + universal: true, + options: [ + { + label: 'someOtherLabel', + value: 'somePrimitiveTagValue', + }, + ], + }, + }, + { + doc: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'preset', + keyboardSound: 'rad', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + name: 'myPreset', + geometry: ['point', 'vertex', 'line'], + tags: { + someKeyForArrVal: ['arr', 'of', 'strings'], + }, + addTags: { + heyAddThisTag: 'pleease', + }, + removeTags: { + deleteInmeditaly: ['this list', 'of things'], + }, + fieldIds: cachedValues.fieldIds, + iconId: cachedValues.iconId, + terms: ['imastring'], + }, + expected: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'preset', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + name: 'myPreset', + geometry: ['point', 'vertex', 'line'], + tags: { + someKeyForArrVal: ['arr', 'of', 'strings'], + }, + addTags: { + heyAddThisTag: 'pleease', + }, + removeTags: { + deleteInmeditaly: ['this list', 'of things'], + }, + fieldIds: cachedValues.fieldIds, + iconId: cachedValues.iconId, + terms: ['imastring'], + }, + }, + { + doc: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'role', + areYouMasterOfTheUniverse: + 'js doesnt allow question marks in identifiers, which saddens me...', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + role: 'author', + projectId: cachedValues.projectId, + action: 'role_set', + signature: 'signature', + authorIndex: 8, + deviceIndex: 7, + authorId: cachedValues.authorId, + capabilityType: 'capability', + }, + expected: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'role', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + role: 'author', + projectId: cachedValues.projectId, + action: 'role_set', + signature: 'signature', + authorIndex: 8, + deviceIndex: 7, + authorId: cachedValues.authorId, + capabilityType: 'capability', + }, + }, + { + doc: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'device', + programmedObsolescense: 'well, duh...', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + action: 'ban', + authorId: cachedValues.authorId, + projectId: cachedValues.projectId, + signature: 'mySignature', + authorIndex: 1, + deviceIndex: 10, + capabilityType: 'someCapability', + }, + expected: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'device', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + action: 'ban', + authorId: cachedValues.authorId, + projectId: cachedValues.projectId, + signature: 'mySignature', + authorIndex: 1, + deviceIndex: 10, + capabilityType: 'someCapability', + }, + }, + { + doc: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + NOBODYOWNSANYTHINGOK: ':|', + schemaName: 'coreOwnership', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + action: 'remove', + coreId: cachedValues.coreId, + projectId: cachedValues.projectId, + storeType: 'blob', + signature: 'mySig', + authorIndex: 100, + deviceIndex: 2, + authorId: cachedValues.authorId, + capabilityType: 'someCapability', + }, + expected: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'coreOwnership', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + action: 'remove', + coreId: cachedValues.coreId, + projectId: cachedValues.projectId, + storeType: 'blob', + signature: 'mySig', + authorIndex: 100, + deviceIndex: 2, + authorId: cachedValues.authorId, + capabilityType: 'someCapability', + }, + }, +] diff --git a/test/fixtures/good-docs-minimal.js b/test/fixtures/good-docs-minimal.js new file mode 100644 index 00000000..9ebb7135 --- /dev/null +++ b/test/fixtures/good-docs-minimal.js @@ -0,0 +1,217 @@ +// @ts-check +import { cachedValues } from './cached.js'; + +/** + * @type {Array<{ + * expected: import('../../dist/types').MapeoDoc, + * doc: import('../../dist/types').MapeoDoc + * }>} + */ +export const goodDocsMinimal = [ + { + doc: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'observation', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + refs: [], + attachments: [], + tags: {}, + }, + expected: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'observation', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + refs: [], + attachments: [], + tags: {}, + metadata: {}, + }, + }, + { + doc: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'project', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + defaultPresets: {}, + name: 'myProject', + }, + expected: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'project', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + defaultPresets: { + point: [], + area: [], + vertex: [], + line: [], + relation: [], + }, + name: 'myProject', + }, + }, + { + doc: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'field', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + tagKey: 'myTagKey', + }, + expected: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'field', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + tagKey: 'myTagKey', + type: 'UNRECOGNIZED', + label: 'myTagKey', // see src/lib/decode-conversions.js:77 + appearance: 'multiline', + snakeCase: false, + options: [], + universal: false, + }, + }, + { + doc: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'preset', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + name: 'myPreset', + geometry: ['point'], + tags: {}, + addTags: {}, + removeTags: {}, + fieldIds: [], + terms: [], + }, + expected: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'preset', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + name: 'myPreset', + geometry: ['point'], + tags: {}, + addTags: {}, + removeTags: {}, + fieldIds: [], + terms: [], + }, + }, + { + doc: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'role', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + role: 'author', + projectId: cachedValues.projectId, + signature: 'signature', + authorId: cachedValues.authorId, + capabilityType: 'capability', + }, + expected: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'role', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + role: 'author', + projectId: cachedValues.projectId, + action: 'UNRECOGNIZED', + signature: 'signature', + authorIndex: 0, + deviceIndex: 0, + authorId: cachedValues.authorId, + capabilityType: 'capability', + }, + }, + { + doc: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'device', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + action: 'ban', + authorId: cachedValues.authorId, + projectId: cachedValues.projectId, + signature: 'mySignature', + capabilityType: 'someCapability', + }, + expected: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'device', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + action: 'ban', + authorId: cachedValues.authorId, + projectId: cachedValues.projectId, + signature: 'mySignature', + authorIndex: 0, + deviceIndex: 0, + capabilityType: 'someCapability', + }, + }, + { + doc: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'coreOwnership', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + action: 'remove', + coreId: cachedValues.coreId, + projectId: cachedValues.projectId, + storeType: 'blob', + signature: 'mySig', + authorId: cachedValues.authorId, + capabilityType: 'someCapability', + }, + expected: { + docId: cachedValues.docId, + versionId: cachedValues.versionId, + schemaName: 'coreOwnership', + createdAt: cachedValues.createdAt, + updatedAt: cachedValues.updatedAt, + links: [], + action: 'remove', + coreId: cachedValues.coreId, + projectId: cachedValues.projectId, + storeType: 'blob', + signature: 'mySig', + authorIndex: 0, + deviceIndex: 0, + authorId: cachedValues.authorId, + capabilityType: 'someCapability', + }, + }, +], diff --git a/test/fixtures/index.js b/test/fixtures/index.js new file mode 100644 index 00000000..5e9fd8d9 --- /dev/null +++ b/test/fixtures/index.js @@ -0,0 +1,4 @@ +export * from './bad-docs.js' +export * from './good-docs-completed.js' +export * from './good-docs-extra-fields.js' +export * from './good-docs-minimal.js' diff --git a/test/index.js b/test/index.js index 4ce947b3..1e7dffcb 100644 --- a/test/index.js +++ b/test/index.js @@ -1,12 +1,18 @@ +// @ts-check import test from 'tape' -import { fixtures } from './fixture.js' -import { encode } from '../dist/encode.js' -import { decode } from '../dist/decode.js' -import { parseVersionId } from '../dist/lib/utils.js' +import { encode, decode } from '../dist/index.js' +import { parseVersionId } from '../dist/index.js' +import { + goodDocsMinimal, + goodDocsCompleted, + goodDocsWithExtraFields, + badDocs, +} from './fixtures/index.js' test('Bad docs throw when encoding', (t) => { - for (const { text, doc } of fixtures.badDocs) { + for (const { text, doc } of badDocs) { t.throws(() => { + // @ts-expect-error encode(doc) }, text) } @@ -15,7 +21,7 @@ test('Bad docs throw when encoding', (t) => { test(`testing encoding of doc with minimal required values, then decoding and comparing the two objects`, async (t) => { - for (let { doc, expected } of fixtures.goodDocsMinimal) { + for (let { doc, expected } of goodDocsMinimal) { let buf t.doesNotThrow(() => { buf = encode(doc) @@ -31,7 +37,7 @@ test(`testing encoding of doc with minimal required values, test(`testing encoding of doc with additional optional values, then decoding and comparing the two objects`, async (t) => { - for (let { doc, expected } of fixtures.goodDocsCompleted) { + for (let { doc, expected } of goodDocsCompleted) { let buf t.doesNotThrow(() => { buf = encode(doc) @@ -47,7 +53,7 @@ test(`testing encoding of doc with additional optional values, test(`testing encoding of doc with additional extra values, then decoding and comparing the two objects - extra values shouldn't be present`, async (t) => { - for (let { doc, expected } of fixtures.goodDocsWithExtraFields) { + for (let { doc, expected } of goodDocsWithExtraFields) { let buf t.doesNotThrow(() => { buf = encode(doc) From 756d345621554dcce3f77883258870511bc0a688 Mon Sep 17 00:00:00 2001 From: Gregor MacLennan Date: Tue, 15 Aug 2023 10:35:57 +0100 Subject: [PATCH 18/23] fix typos and make goodDocs correct types --- test/fixtures/good-docs-completed.js | 8 ++++---- test/fixtures/good-docs-extra-fields.js | 8 ++++---- test/fixtures/good-docs-minimal.js | 18 ++++++++++++++---- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/test/fixtures/good-docs-completed.js b/test/fixtures/good-docs-completed.js index 4c047147..52bd04b6 100644 --- a/test/fixtures/good-docs-completed.js +++ b/test/fixtures/good-docs-completed.js @@ -42,7 +42,7 @@ export const goodDocsCompleted = [ altitude: 0.8, heading: 1.2, speed: 0.7, - acurracy: 1.3, + accuracy: 1.3, }, }, lastSavedPosition: { @@ -54,7 +54,7 @@ export const goodDocsCompleted = [ altitude: 1000.1, heading: 0.2, speed: 0.1, - acurracy: 8.3, + accuracy: 8.3, }, }, positionProvider: { @@ -97,7 +97,7 @@ export const goodDocsCompleted = [ altitude: 0.8, heading: 1.2, speed: 0.7, - acurracy: 1.3, + accuracy: 1.3, }, }, lastSavedPosition: { @@ -109,7 +109,7 @@ export const goodDocsCompleted = [ altitude: 1000.1, heading: 0.2, speed: 0.1, - acurracy: 8.3, + accuracy: 8.3, }, }, positionProvider: { diff --git a/test/fixtures/good-docs-extra-fields.js b/test/fixtures/good-docs-extra-fields.js index 50c4a9e9..2007f383 100644 --- a/test/fixtures/good-docs-extra-fields.js +++ b/test/fixtures/good-docs-extra-fields.js @@ -43,7 +43,7 @@ export const goodDocsWithExtraFields = [ altitude: 0.8, heading: 1.2, speed: 0.7, - acurracy: 1.3, + accuracy: 1.3, OTHeRValue: 'Am I a Number?', }, }, @@ -56,7 +56,7 @@ export const goodDocsWithExtraFields = [ altitude: 1000.1, heading: 0.2, speed: 0.1, - acurracy: 8.3, + accuracy: 8.3, }, }, positionProvider: { @@ -99,7 +99,7 @@ export const goodDocsWithExtraFields = [ altitude: 0.8, heading: 1.2, speed: 0.7, - acurracy: 1.3, + accuracy: 1.3, }, }, lastSavedPosition: { @@ -111,7 +111,7 @@ export const goodDocsWithExtraFields = [ altitude: 1000.1, heading: 0.2, speed: 0.1, - acurracy: 8.3, + accuracy: 8.3, }, }, positionProvider: { diff --git a/test/fixtures/good-docs-minimal.js b/test/fixtures/good-docs-minimal.js index 9ebb7135..05426033 100644 --- a/test/fixtures/good-docs-minimal.js +++ b/test/fixtures/good-docs-minimal.js @@ -1,5 +1,5 @@ // @ts-check -import { cachedValues } from './cached.js'; +import { cachedValues } from './cached.js' /** * @type {Array<{ @@ -19,6 +19,7 @@ export const goodDocsMinimal = [ refs: [], attachments: [], tags: {}, + metadata: {}, }, expected: { docId: cachedValues.docId, @@ -70,6 +71,8 @@ export const goodDocsMinimal = [ updatedAt: cachedValues.updatedAt, links: [], tagKey: 'myTagKey', + label: 'my label', + type: 'text', }, expected: { docId: cachedValues.docId, @@ -79,8 +82,8 @@ export const goodDocsMinimal = [ updatedAt: cachedValues.updatedAt, links: [], tagKey: 'myTagKey', - type: 'UNRECOGNIZED', - label: 'myTagKey', // see src/lib/decode-conversions.js:77 + type: 'text', + label: 'my label', // see src/lib/decode-conversions.js:77 appearance: 'multiline', snakeCase: false, options: [], @@ -132,6 +135,9 @@ export const goodDocsMinimal = [ signature: 'signature', authorId: cachedValues.authorId, capabilityType: 'capability', + authorIndex: 0, + deviceIndex: 0, + action: 'UNRECOGNIZED', }, expected: { docId: cachedValues.docId, @@ -163,6 +169,8 @@ export const goodDocsMinimal = [ projectId: cachedValues.projectId, signature: 'mySignature', capabilityType: 'someCapability', + authorIndex: 0, + deviceIndex: 0, }, expected: { docId: cachedValues.docId, @@ -195,6 +203,8 @@ export const goodDocsMinimal = [ signature: 'mySig', authorId: cachedValues.authorId, capabilityType: 'someCapability', + authorIndex: 0, + deviceIndex: 0, }, expected: { docId: cachedValues.docId, @@ -214,4 +224,4 @@ export const goodDocsMinimal = [ capabilityType: 'someCapability', }, }, -], +] From a1793dc445eb62ca05495948287fb253ed73daf1 Mon Sep 17 00:00:00 2001 From: Gregor MacLennan Date: Tue, 15 Aug 2023 10:42:38 +0100 Subject: [PATCH 19/23] fix typo acurracy -> accuracy --- proto/observation/v5.proto | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/proto/observation/v5.proto b/proto/observation/v5.proto index 1c14e480..dd807e29 100644 --- a/proto/observation/v5.proto +++ b/proto/observation/v5.proto @@ -52,7 +52,7 @@ message Observation_5 { double altitude = 3; double heading = 4; double speed = 5; - double acurracy = 6; + double accuracy = 6; } optional Coords coords = 3; } From 2838ad75b1ee5806696760ff29798f2d09c40851 Mon Sep 17 00:00:00 2001 From: Gregor MacLennan Date: Tue, 15 Aug 2023 10:43:30 +0100 Subject: [PATCH 20/23] remove extra fields fixtures - not necessary --- test/fixtures/good-docs-extra-fields.js | 359 ------------------------ test/fixtures/index.js | 1 - test/index.js | 9 +- 3 files changed, 6 insertions(+), 363 deletions(-) delete mode 100644 test/fixtures/good-docs-extra-fields.js diff --git a/test/fixtures/good-docs-extra-fields.js b/test/fixtures/good-docs-extra-fields.js deleted file mode 100644 index 2007f383..00000000 --- a/test/fixtures/good-docs-extra-fields.js +++ /dev/null @@ -1,359 +0,0 @@ -// @ts-check - -import { cachedValues } from './cached.js' - -/** - * @type {Array<{ - * expected: import('../../dist/types').MapeoDoc, - * doc: import('../../dist/types').MapeoDoc - * }>} - */ -export const goodDocsWithExtraFields = [ - { - doc: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'observation', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - zodiacSign: 'banana', - links: [], - lat: 24.0424, - lon: 21.0214, - refs: [], - attachments: [ - { - name: 'myFile', - type: 'photo', - driveId: cachedValues.attachments.driveId, - }, - ], - tags: { - someKeyForSingleVal: 'someVal', - }, - metadata: { - manualLocation: true, - position: { - IMInc0rRecT: Array(100).fill(Math.random() * 1000), - timestamp: cachedValues.metadata.position.timestamp, - mocked: false, - coords: { - latitude: 0.5, - longitude: -0.2, - altitude: 0.8, - heading: 1.2, - speed: 0.7, - accuracy: 1.3, - OTHeRValue: 'Am I a Number?', - }, - }, - lastSavedPosition: { - timestamp: cachedValues.metadata.position.timestamp, - mocked: true, - coords: { - latitude: 1.5, - longitude: -2.3, - altitude: 1000.1, - heading: 0.2, - speed: 0.1, - accuracy: 8.3, - }, - }, - positionProvider: { - gpsAvailable: true, - passiveAvailable: false, - locationServicesEnabled: true, - evilLairLocationEnabled: 'NOT', - networkAvailable: false, - }, - }, - }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'observation', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - lat: 24.0424, - lon: 21.0214, - refs: [], - attachments: [ - { - name: 'myFile', - type: 'photo', - driveId: cachedValues.attachments.driveId, - }, - ], - tags: { - someKeyForSingleVal: 'someVal', - }, - metadata: { - manualLocation: true, - position: { - timestamp: cachedValues.metadata.position.timestamp, - mocked: false, - coords: { - latitude: 0.5, - longitude: -0.2, - altitude: 0.8, - heading: 1.2, - speed: 0.7, - accuracy: 1.3, - }, - }, - lastSavedPosition: { - timestamp: cachedValues.metadata.position.timestamp, - mocked: true, - coords: { - latitude: 1.5, - longitude: -2.3, - altitude: 1000.1, - heading: 0.2, - speed: 0.1, - accuracy: 8.3, - }, - }, - positionProvider: { - gpsAvailable: true, - passiveAvailable: false, - locationServicesEnabled: true, - networkAvailable: false, - }, - }, - }, - }, - { - doc: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'project', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - numberOfPeople: 'glove', - defaultPresets: { - point: cachedValues.defaultPresets.point, - area: cachedValues.defaultPresets.point, - vertex: cachedValues.defaultPresets.point, - line: cachedValues.defaultPresets.point, - relation: cachedValues.defaultPresets.point, - way: 'I DONT EXIST YET??', - }, - name: 'myProject', - }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'project', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - defaultPresets: { - point: cachedValues.defaultPresets.point, - area: cachedValues.defaultPresets.point, - vertex: cachedValues.defaultPresets.point, - line: cachedValues.defaultPresets.point, - relation: cachedValues.defaultPresets.point, - }, - name: 'myProject', - }, - }, - { - doc: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'field', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - destroyedAt: new Date().toJSON(), - links: [], - tagKey: 'otherTagKey', - type: 'number', - label: 'differentLabel', - appearance: 'singleline', - snakeCase: true, - universal: true, - options: [ - { - centipede: 'big', - label: 'someOtherLabel', - value: 'somePrimitiveTagValue', - }, - ], - }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'field', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - tagKey: 'otherTagKey', - type: 'number', - label: 'differentLabel', - appearance: 'singleline', - snakeCase: true, - universal: true, - options: [ - { - label: 'someOtherLabel', - value: 'somePrimitiveTagValue', - }, - ], - }, - }, - { - doc: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'preset', - keyboardSound: 'rad', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - name: 'myPreset', - geometry: ['point', 'vertex', 'line'], - tags: { - someKeyForArrVal: ['arr', 'of', 'strings'], - }, - addTags: { - heyAddThisTag: 'pleease', - }, - removeTags: { - deleteInmeditaly: ['this list', 'of things'], - }, - fieldIds: cachedValues.fieldIds, - iconId: cachedValues.iconId, - terms: ['imastring'], - }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'preset', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - name: 'myPreset', - geometry: ['point', 'vertex', 'line'], - tags: { - someKeyForArrVal: ['arr', 'of', 'strings'], - }, - addTags: { - heyAddThisTag: 'pleease', - }, - removeTags: { - deleteInmeditaly: ['this list', 'of things'], - }, - fieldIds: cachedValues.fieldIds, - iconId: cachedValues.iconId, - terms: ['imastring'], - }, - }, - { - doc: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'role', - areYouMasterOfTheUniverse: - 'js doesnt allow question marks in identifiers, which saddens me...', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - role: 'author', - projectId: cachedValues.projectId, - action: 'role_set', - signature: 'signature', - authorIndex: 8, - deviceIndex: 7, - authorId: cachedValues.authorId, - capabilityType: 'capability', - }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'role', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - role: 'author', - projectId: cachedValues.projectId, - action: 'role_set', - signature: 'signature', - authorIndex: 8, - deviceIndex: 7, - authorId: cachedValues.authorId, - capabilityType: 'capability', - }, - }, - { - doc: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'device', - programmedObsolescense: 'well, duh...', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - action: 'ban', - authorId: cachedValues.authorId, - projectId: cachedValues.projectId, - signature: 'mySignature', - authorIndex: 1, - deviceIndex: 10, - capabilityType: 'someCapability', - }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'device', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - action: 'ban', - authorId: cachedValues.authorId, - projectId: cachedValues.projectId, - signature: 'mySignature', - authorIndex: 1, - deviceIndex: 10, - capabilityType: 'someCapability', - }, - }, - { - doc: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - NOBODYOWNSANYTHINGOK: ':|', - schemaName: 'coreOwnership', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - action: 'remove', - coreId: cachedValues.coreId, - projectId: cachedValues.projectId, - storeType: 'blob', - signature: 'mySig', - authorIndex: 100, - deviceIndex: 2, - authorId: cachedValues.authorId, - capabilityType: 'someCapability', - }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'coreOwnership', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - action: 'remove', - coreId: cachedValues.coreId, - projectId: cachedValues.projectId, - storeType: 'blob', - signature: 'mySig', - authorIndex: 100, - deviceIndex: 2, - authorId: cachedValues.authorId, - capabilityType: 'someCapability', - }, - }, -] diff --git a/test/fixtures/index.js b/test/fixtures/index.js index 5e9fd8d9..1b5bd89b 100644 --- a/test/fixtures/index.js +++ b/test/fixtures/index.js @@ -1,4 +1,3 @@ export * from './bad-docs.js' export * from './good-docs-completed.js' -export * from './good-docs-extra-fields.js' export * from './good-docs-minimal.js' diff --git a/test/index.js b/test/index.js index 1e7dffcb..66a36fc9 100644 --- a/test/index.js +++ b/test/index.js @@ -5,7 +5,6 @@ import { parseVersionId } from '../dist/index.js' import { goodDocsMinimal, goodDocsCompleted, - goodDocsWithExtraFields, badDocs, } from './fixtures/index.js' @@ -53,10 +52,14 @@ test(`testing encoding of doc with additional optional values, test(`testing encoding of doc with additional extra values, then decoding and comparing the two objects - extra values shouldn't be present`, async (t) => { - for (let { doc, expected } of goodDocsWithExtraFields) { + for (let { doc, expected } of goodDocsCompleted) { let buf t.doesNotThrow(() => { - buf = encode(doc) + buf = encode({ + ...doc, + // @ts-expect-error + extraFieldNotInSchema: 'whatever', + }) }, `tested encoding of ${doc.schemaName}`) let decodedDoc = stripUndef(decode(buf, parseVersionId(doc.versionId))) t.deepEqual( From 8a6113de5ce2aa5481399ad5396701422082be36 Mon Sep 17 00:00:00 2001 From: Gregor MacLennan Date: Tue, 15 Aug 2023 10:44:49 +0100 Subject: [PATCH 21/23] remove doesNotThrow check - test will fail anyway if it throws, and that's not what we're testing here --- test/index.js | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/test/index.js b/test/index.js index 66a36fc9..6bdbbc7f 100644 --- a/test/index.js +++ b/test/index.js @@ -21,10 +21,7 @@ test('Bad docs throw when encoding', (t) => { test(`testing encoding of doc with minimal required values, then decoding and comparing the two objects`, async (t) => { for (let { doc, expected } of goodDocsMinimal) { - let buf - t.doesNotThrow(() => { - buf = encode(doc) - }, `tested encoding of ${doc.schemaName}`) + const buf = encode(doc) let decodedDoc = stripUndef(decode(buf, parseVersionId(doc.versionId))) t.deepEqual( decodedDoc, @@ -37,10 +34,7 @@ test(`testing encoding of doc with minimal required values, test(`testing encoding of doc with additional optional values, then decoding and comparing the two objects`, async (t) => { for (let { doc, expected } of goodDocsCompleted) { - let buf - t.doesNotThrow(() => { - buf = encode(doc) - }, `tested encoding of ${doc.schemaName}`) + const buf = encode(doc) let decodedDoc = stripUndef(decode(buf, parseVersionId(doc.versionId))) t.deepEqual( decodedDoc, @@ -53,14 +47,11 @@ test(`testing encoding of doc with additional optional values, test(`testing encoding of doc with additional extra values, then decoding and comparing the two objects - extra values shouldn't be present`, async (t) => { for (let { doc, expected } of goodDocsCompleted) { - let buf - t.doesNotThrow(() => { - buf = encode({ - ...doc, - // @ts-expect-error - extraFieldNotInSchema: 'whatever', - }) - }, `tested encoding of ${doc.schemaName}`) + const buf = encode({ + ...doc, + // @ts-expect-error + extraFieldNotInSchema: 'whatever', + }) let decodedDoc = stripUndef(decode(buf, parseVersionId(doc.versionId))) t.deepEqual( decodedDoc, From d7fd299b02d7f6f993187e513e002e740a54208b Mon Sep 17 00:00:00 2001 From: Gregor MacLennan Date: Tue, 15 Aug 2023 10:52:42 +0100 Subject: [PATCH 22/23] only define fields that are expected to change Re-writing the whole doc as `expected` is subject to typos, and it's not clear what we are actually testing. Switch to just defining fields expected to change in `expected` docs --- test/fixtures/good-docs-completed.js | 171 ++------------------------- test/fixtures/good-docs-minimal.js | 101 ++-------------- test/index.js | 6 +- 3 files changed, 20 insertions(+), 258 deletions(-) diff --git a/test/fixtures/good-docs-completed.js b/test/fixtures/good-docs-completed.js index 52bd04b6..327983bc 100644 --- a/test/fixtures/good-docs-completed.js +++ b/test/fixtures/good-docs-completed.js @@ -4,7 +4,7 @@ import { cachedValues } from './cached.js' /** * @type {Array<{ - * expected: import('../../dist/types').MapeoDoc, + * expected: Partial, * doc: import('../../dist/types').MapeoDoc * }>} */ @@ -65,61 +65,7 @@ export const goodDocsCompleted = [ }, }, }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'observation', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - lat: 24.0424, - lon: 21.0214, - refs: [], - attachments: [ - { - name: 'myFile', - type: 'photo', - driveId: cachedValues.attachments.driveId, - }, - ], - tags: { - someKeyForSingleVal: 'someVal', - someKeyForArrVal: ['arr', 'of', 'strings'], - }, - metadata: { - manualLocation: true, - position: { - timestamp: cachedValues.metadata.position.timestamp, - mocked: false, - coords: { - latitude: 0.5, - longitude: -0.2, - altitude: 0.8, - heading: 1.2, - speed: 0.7, - accuracy: 1.3, - }, - }, - lastSavedPosition: { - timestamp: cachedValues.metadata.position.timestamp, - mocked: true, - coords: { - latitude: 1.5, - longitude: -2.3, - altitude: 1000.1, - heading: 0.2, - speed: 0.1, - accuracy: 8.3, - }, - }, - positionProvider: { - gpsAvailable: true, - passiveAvailable: false, - locationServicesEnabled: true, - networkAvailable: false, - }, - }, - }, + expected: {}, }, { doc: { @@ -138,22 +84,7 @@ export const goodDocsCompleted = [ }, name: 'myProject', }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'project', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - defaultPresets: { - point: cachedValues.defaultPresets.point, - area: cachedValues.defaultPresets.point, - vertex: cachedValues.defaultPresets.point, - line: cachedValues.defaultPresets.point, - relation: cachedValues.defaultPresets.point, - }, - name: 'myProject', - }, + expected: {}, }, { doc: { @@ -176,26 +107,7 @@ export const goodDocsCompleted = [ }, ], }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'field', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - tagKey: 'otherTagKey', - type: 'number', - label: 'differentLabel', - appearance: 'singleline', - snakeCase: true, - universal: true, - options: [ - { - label: 'someOtherLabel', - value: 'somePrimitiveTagValue', - }, - ], - }, + expected: {}, }, { doc: { @@ -221,29 +133,7 @@ export const goodDocsCompleted = [ iconId: cachedValues.iconId, terms: ['imastring'], }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'preset', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - name: 'myPreset', - geometry: ['point', 'vertex', 'line'], - tags: { - someKeyForArrVal: ['arr', 'of', 'strings'], - }, - addTags: { - heyAddThisTag: 'pleease', - orMaybeThis: ['right?', '', 'that was empty'], - }, - removeTags: { - deleteInmeditaly: ['this list', 'of things'], - }, - fieldIds: cachedValues.fieldIds, - iconId: cachedValues.iconId, - terms: ['imastring'], - }, + expected: {}, }, { doc: { @@ -262,22 +152,7 @@ export const goodDocsCompleted = [ authorId: cachedValues.authorId, capabilityType: 'capability', }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'role', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - role: 'author', - projectId: cachedValues.projectId, - action: 'role_set', - signature: 'signature', - authorIndex: 8, - deviceIndex: 7, - authorId: cachedValues.authorId, - capabilityType: 'capability', - }, + expected: {}, }, { doc: { @@ -295,21 +170,7 @@ export const goodDocsCompleted = [ deviceIndex: 10, capabilityType: 'someCapability', }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'device', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - action: 'ban', - authorId: cachedValues.authorId, - projectId: cachedValues.projectId, - signature: 'mySignature', - authorIndex: 1, - deviceIndex: 10, - capabilityType: 'someCapability', - }, + expected: {}, }, { doc: { @@ -329,22 +190,6 @@ export const goodDocsCompleted = [ authorId: cachedValues.authorId, capabilityType: 'someCapability', }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'coreOwnership', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - action: 'remove', - coreId: cachedValues.coreId, - projectId: cachedValues.projectId, - storeType: 'blob', - signature: 'mySig', - authorIndex: 100, - deviceIndex: 2, - authorId: cachedValues.authorId, - capabilityType: 'someCapability', - }, + expected: {}, }, ] diff --git a/test/fixtures/good-docs-minimal.js b/test/fixtures/good-docs-minimal.js index 05426033..865e1f46 100644 --- a/test/fixtures/good-docs-minimal.js +++ b/test/fixtures/good-docs-minimal.js @@ -2,8 +2,11 @@ import { cachedValues } from './cached.js' /** + * The `expected` is a partial doc of the extra fields that we expect to be set + * by the encode/decode round-trip + * * @type {Array<{ - * expected: import('../../dist/types').MapeoDoc, + * expected: Partial, * doc: import('../../dist/types').MapeoDoc * }>} */ @@ -21,18 +24,7 @@ export const goodDocsMinimal = [ tags: {}, metadata: {}, }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'observation', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - refs: [], - attachments: [], - tags: {}, - metadata: {}, - }, + expected: {}, }, { doc: { @@ -46,12 +38,6 @@ export const goodDocsMinimal = [ name: 'myProject', }, expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'project', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], defaultPresets: { point: [], area: [], @@ -59,7 +45,6 @@ export const goodDocsMinimal = [ line: [], relation: [], }, - name: 'myProject', }, }, { @@ -75,15 +60,6 @@ export const goodDocsMinimal = [ type: 'text', }, expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'field', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - tagKey: 'myTagKey', - type: 'text', - label: 'my label', // see src/lib/decode-conversions.js:77 appearance: 'multiline', snakeCase: false, options: [], @@ -106,21 +82,7 @@ export const goodDocsMinimal = [ fieldIds: [], terms: [], }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'preset', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - name: 'myPreset', - geometry: ['point'], - tags: {}, - addTags: {}, - removeTags: {}, - fieldIds: [], - terms: [], - }, + expected: {}, }, { doc: { @@ -139,22 +101,7 @@ export const goodDocsMinimal = [ deviceIndex: 0, action: 'UNRECOGNIZED', }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'role', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - role: 'author', - projectId: cachedValues.projectId, - action: 'UNRECOGNIZED', - signature: 'signature', - authorIndex: 0, - deviceIndex: 0, - authorId: cachedValues.authorId, - capabilityType: 'capability', - }, + expected: {}, }, { doc: { @@ -172,21 +119,7 @@ export const goodDocsMinimal = [ authorIndex: 0, deviceIndex: 0, }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'device', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - action: 'ban', - authorId: cachedValues.authorId, - projectId: cachedValues.projectId, - signature: 'mySignature', - authorIndex: 0, - deviceIndex: 0, - capabilityType: 'someCapability', - }, + expected: {}, }, { doc: { @@ -206,22 +139,6 @@ export const goodDocsMinimal = [ authorIndex: 0, deviceIndex: 0, }, - expected: { - docId: cachedValues.docId, - versionId: cachedValues.versionId, - schemaName: 'coreOwnership', - createdAt: cachedValues.createdAt, - updatedAt: cachedValues.updatedAt, - links: [], - action: 'remove', - coreId: cachedValues.coreId, - projectId: cachedValues.projectId, - storeType: 'blob', - signature: 'mySig', - authorIndex: 0, - deviceIndex: 0, - authorId: cachedValues.authorId, - capabilityType: 'someCapability', - }, + expected: {}, }, ] diff --git a/test/index.js b/test/index.js index 6bdbbc7f..f8e24a09 100644 --- a/test/index.js +++ b/test/index.js @@ -25,7 +25,7 @@ test(`testing encoding of doc with minimal required values, let decodedDoc = stripUndef(decode(buf, parseVersionId(doc.versionId))) t.deepEqual( decodedDoc, - expected, + { ...doc, ...expected }, `tested deep equal of ${doc.schemaName} with only required values` ) } @@ -38,7 +38,7 @@ test(`testing encoding of doc with additional optional values, let decodedDoc = stripUndef(decode(buf, parseVersionId(doc.versionId))) t.deepEqual( decodedDoc, - expected, + { ...doc, ...expected }, `tested deep equal of ${doc.schemaName} with additional values` ) } @@ -55,7 +55,7 @@ then decoding and comparing the two objects - extra values shouldn't be present` let decodedDoc = stripUndef(decode(buf, parseVersionId(doc.versionId))) t.deepEqual( decodedDoc, - expected, + { ...doc, ...expected }, `tested deep equal of ${doc.schemaName} with extra - non valid - values` ) } From 17706d246ba25e3f352c76cc337dc5136f602e3b Mon Sep 17 00:00:00 2001 From: Gregor MacLennan Date: Tue, 15 Aug 2023 10:55:35 +0100 Subject: [PATCH 23/23] let -> const --- test/index.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/index.js b/test/index.js index f8e24a09..f8157595 100644 --- a/test/index.js +++ b/test/index.js @@ -20,9 +20,9 @@ test('Bad docs throw when encoding', (t) => { test(`testing encoding of doc with minimal required values, then decoding and comparing the two objects`, async (t) => { - for (let { doc, expected } of goodDocsMinimal) { + for (const { doc, expected } of goodDocsMinimal) { const buf = encode(doc) - let decodedDoc = stripUndef(decode(buf, parseVersionId(doc.versionId))) + const decodedDoc = stripUndef(decode(buf, parseVersionId(doc.versionId))) t.deepEqual( decodedDoc, { ...doc, ...expected }, @@ -33,9 +33,9 @@ test(`testing encoding of doc with minimal required values, test(`testing encoding of doc with additional optional values, then decoding and comparing the two objects`, async (t) => { - for (let { doc, expected } of goodDocsCompleted) { + for (const { doc, expected } of goodDocsCompleted) { const buf = encode(doc) - let decodedDoc = stripUndef(decode(buf, parseVersionId(doc.versionId))) + const decodedDoc = stripUndef(decode(buf, parseVersionId(doc.versionId))) t.deepEqual( decodedDoc, { ...doc, ...expected }, @@ -46,13 +46,13 @@ test(`testing encoding of doc with additional optional values, test(`testing encoding of doc with additional extra values, then decoding and comparing the two objects - extra values shouldn't be present`, async (t) => { - for (let { doc, expected } of goodDocsCompleted) { + for (const { doc, expected } of goodDocsCompleted) { const buf = encode({ ...doc, // @ts-expect-error extraFieldNotInSchema: 'whatever', }) - let decodedDoc = stripUndef(decode(buf, parseVersionId(doc.versionId))) + const decodedDoc = stripUndef(decode(buf, parseVersionId(doc.versionId))) t.deepEqual( decodedDoc, { ...doc, ...expected },