From aa6d851e499d8c0298c43bfdaef9cf88e03d3ae2 Mon Sep 17 00:00:00 2001 From: Andrzej Stepien Date: Sun, 13 Aug 2023 16:49:25 +0200 Subject: [PATCH] Note object and testing --- package.json | 2 +- social-interaction/Note.mjs | 110 +++++++++++++++++ social-interaction/receiveMention.mjs | 21 ---- social-interaction/receiveMention.test.mjs | 1 - test/Note.test.mjs | 133 +++++++++++++++++++++ test/receiveMention.test.mjs | 80 +++++++++++++ 6 files changed, 324 insertions(+), 23 deletions(-) create mode 100644 social-interaction/Note.mjs delete mode 100644 social-interaction/receiveMention.test.mjs create mode 100644 test/Note.test.mjs create mode 100644 test/receiveMention.test.mjs diff --git a/package.json b/package.json index 2da2009..3b1b38c 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "a server that delivers daily writing prompts via REST API", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "mocha" }, "author": "Andrzej Stepien", "license": "GPL-3.0-or-later", diff --git a/social-interaction/Note.mjs b/social-interaction/Note.mjs new file mode 100644 index 0000000..470c603 --- /dev/null +++ b/social-interaction/Note.mjs @@ -0,0 +1,110 @@ +import { isMisspelled } from "spellchecker" +const sampleNote = { + "id": "9id213fllx9y189f", + "createdAt": "2023-08-13T13:37:09.537Z", + "userId": "9i5z4skgqvv58swy", + "user": { + "id": "9i5z4skgqvv58swy", + "name": null, + "username": "admin", + "host": null, + "avatarUrl": "https://localhost:3000/identicon/9i5z4skgqvv58swy", + "avatarBlurhash": null, + "avatarColor": null, + "isAdmin": true, + "isLocked": false, + "speakAsCat": true, + "emojis": [], + "onlineStatus": "online", + "driveCapacityOverrideMb": null + }, + "text": "@micro365 1", + "cw": "Today's #micro365 prompt is:", + "visibility": "public", + "renoteCount": 0, + "repliesCount": 0, + "reactions": {}, + "reactionEmojis": [], + "emojis": [], + "tags": [ + "micro365" + ], + "fileIds": [], + "files": [], + "replyId": "9id1ffugrao33bm4", + "renoteId": null, + "mentions": [ + "9i5z5o9zy11l3skl" + ], + "reply": { + "id": "9id1ffugrao33bm4", + "createdAt": "2023-08-13T13:20:19.192Z", + "userId": "9i5z5o9zy11l3skl", + "user": { + "id": "9i5z5o9zy11l3skl", + "name": null, + "username": "micro365", + "host": null, + "avatarUrl": "https://localhost:3000/files/thumbnail-4e0e8b82-df72-48f7-8100-b7515173da9d", + "avatarBlurhash": "ySPjGct7xu%M-;xu-;%MRjWBoffQofWB~qRjRjayRjfQM{M{t7ofWBt7ayfQ~qj[WBj[M{WBof?bofayfQM{WBfQt7xuofWBofofM{", + "avatarColor": null, + "isLocked": false, + "speakAsCat": true, + "emojis": [], + "onlineStatus": "active", + "driveCapacityOverrideMb": null + }, + "text": "# $[x2 $[font.serif **nudism**]]\n/njˈuːdɪzəm/\n**noun**:\n- The belief in or practice of going nude in social, nonsexualized and frequently mixed-gender groups specifically in cultures where going nude in the social situation is not the norm.\n#writing #microfiction #vss #nudism", + "cw": "Today's #micro365 prompt is:", + "visibility": "public", + "renoteCount": 0, + "repliesCount": 0, + "reactions": {}, + "reactionEmojis": [], + "emojis": [], + "tags": [ + "writing", + "microfiction", + "vss", + "nudism", + "micro365" + ], + "fileIds": [], + "files": [], + "replyId": null, + "renoteId": null + } + } + +export default class { + constructor(raw){ + this.raw = raw + } + +#handle = /@[a-z,A-Z,0-9]* /g + +get text() { + return this.raw.text +} + +get cleanText() { + return this.raw.text.replace(this.#handle, "").trim() +} + +get mentioned(){ + return this.raw.text.match(this.#handle) +} + +get id(){ + return this.raw.id +} + +get isSingleWord() { + return this.cleanText.match(/[a-z]+/ig).length===1 +} + +get isRealWord(){ + return !isMisspelled(this.cleanText) +} + +} \ No newline at end of file diff --git a/social-interaction/receiveMention.mjs b/social-interaction/receiveMention.mjs index 0763f14..9e5992d 100644 --- a/social-interaction/receiveMention.mjs +++ b/social-interaction/receiveMention.mjs @@ -3,27 +3,6 @@ import { checkSpelling } from "spellchecker" import { wordIsAlreadyInBuffer, getAcceptablePrompts } from "../database-calls/db.mjs" export default async function (note) { - const childLogger = logger.child({note}) - childLogger.trace("receiveMention called") - const textArray = note.text - .replace(/@[a-z,A-Z,0-9]* /g, "") - .trim() - .match(/[a-z]*/ig) - if(textArray.length===1){ - const word = textArray[0] - if(checkSpelling(word)){ - if(await getAcceptablePrompts().indexOf(word)!=-1){ - if(!wordIsAlreadyInBuffer(word)){ - // } - } - } - - - //ETC - }else if(textArray.length>1){ - //"Please reply with one word, I'm only a bot etc etc" - } -} } \ No newline at end of file diff --git a/social-interaction/receiveMention.test.mjs b/social-interaction/receiveMention.test.mjs deleted file mode 100644 index 2b6f57b..0000000 --- a/social-interaction/receiveMention.test.mjs +++ /dev/null @@ -1 +0,0 @@ -import { expect } from "chai"; \ No newline at end of file diff --git a/test/Note.test.mjs b/test/Note.test.mjs new file mode 100644 index 0000000..0b3c042 --- /dev/null +++ b/test/Note.test.mjs @@ -0,0 +1,133 @@ +import Note from "../social-interaction/Note.mjs"; +import { expect } from "chai"; +const sampleNote = { + "id": "9id213fllx9y189f", + "createdAt": "2023-08-13T13:37:09.537Z", + "userId": "9i5z4skgqvv58swy", + "user": { + "id": "9i5z4skgqvv58swy", + "name": null, + "username": "admin", + "host": null, + "avatarUrl": "https://localhost:3000/identicon/9i5z4skgqvv58swy", + "avatarBlurhash": null, + "avatarColor": null, + "isAdmin": true, + "isLocked": false, + "speakAsCat": true, + "emojis": [], + "onlineStatus": "online", + "driveCapacityOverrideMb": null + }, + "text": "@micro365 1", + "cw": "Today's #micro365 prompt is:", + "visibility": "public", + "renoteCount": 0, + "repliesCount": 0, + "reactions": {}, + "reactionEmojis": [], + "emojis": [], + "tags": [ + "micro365" + ], + "fileIds": [], + "files": [], + "replyId": "9id1ffugrao33bm4", + "renoteId": null, + "mentions": [ + "9i5z5o9zy11l3skl" + ], + "reply": { + "id": "9id1ffugrao33bm4", + "createdAt": "2023-08-13T13:20:19.192Z", + "userId": "9i5z5o9zy11l3skl", + "user": { + "id": "9i5z5o9zy11l3skl", + "name": null, + "username": "micro365", + "host": null, + "avatarUrl": "https://localhost:3000/files/thumbnail-4e0e8b82-df72-48f7-8100-b7515173da9d", + "avatarBlurhash": "ySPjGct7xu%M-;xu-;%MRjWBoffQofWB~qRjRjayRjfQM{M{t7ofWBt7ayfQ~qj[WBj[M{WBof?bofayfQM{WBfQt7xuofWBofofM{", + "avatarColor": null, + "isLocked": false, + "speakAsCat": true, + "emojis": [], + "onlineStatus": "active", + "driveCapacityOverrideMb": null + }, + "text": "# $[x2 $[font.serif **nudism**]]\n/njˈuːdɪzəm/\n**noun**:\n- The belief in or practice of going nude in social, nonsexualized and frequently mixed-gender groups specifically in cultures where going nude in the social situation is not the norm.\n#writing #microfiction #vss #nudism", + "cw": "Today's #micro365 prompt is:", + "visibility": "public", + "renoteCount": 0, + "repliesCount": 0, + "reactions": {}, + "reactionEmojis": [], + "emojis": [], + "tags": [ + "writing", + "microfiction", + "vss", + "nudism", + "micro365" + ], + "fileIds": [], + "files": [], + "replyId": null, + "renoteId": null + } + } +const N1 = new Note(sampleNote) +describe("Testing Note getters", function(){ + it("1. .text returns a string", function(done){ + expect(N1.text).to.be.a("string") + done() + }) + it("2. .cleanText returns a string", function(done){ + expect(N1.cleanText).to.be.a("string") + done() + }) + it("3. .cleanText contains no @s", function(done){ + expect(/@/.test(N1.cleanText)).to.equal(false) + done() + }) + it("4 .mentioned should be array", function(done){ + expect(N1.mentioned).to.be.a('array') + done() + }) + it("5. .mentioned should have length 4 when text = '@george @paul @ringo @john how about a reunion?'", function(done){ + N1.raw.text = "@george @paul @ringo @john how about a reunion?" + expect(N1.mentioned.length).to.equal(4) + done() + }) + it("6. .mentioned should have length 2 when text = '@laurel @hardy how about a reunion?'", function(done){ + N1.raw.text = "@laurel @hardy how about a reunion?" + expect(N1.mentioned.length).to.equal(2) + done() + }) + it("7. isSingleWord should return false when text = '@laurel @hardy how about a reunion?'", function(done){ + N1.raw.text = "@laurel @hardy how about a reunion?" + expect(N1.isSingleWord).to.equal(false) + done() + }) + it("8. isSingleWord should return true when text = '@laurel @me no'", function(done){ + N1.raw.text = "@laurel @me no" + expect(N1.isSingleWord).to.equal(true) + done() + }) + it("9. isSingleWord should return true when text = 'word'", function(done){ + N1.raw.text = "word" + expect(N1.isSingleWord).to.equal(true) + done() + }) + it("10. isRealWord should return true when text = 'word'", function(done){ + N1.raw.text = "word" + expect(N1.isRealWord).to.equal(true) + done() + }) + it("11. isRealWord should return false when text = 'embiggensly'", function(done){ + N1.raw.text = "embiggensly" + expect(N1.isRealWord).to.equal(false) + done() + }) + +}) \ No newline at end of file diff --git a/test/receiveMention.test.mjs b/test/receiveMention.test.mjs new file mode 100644 index 0000000..365a644 --- /dev/null +++ b/test/receiveMention.test.mjs @@ -0,0 +1,80 @@ +import { expect } from "chai"; +import receiveMention from "../social-interaction/receiveMention.mjs"; +const sampleNote = { + "id": "9id213fllx9y189f", + "createdAt": "2023-08-13T13:37:09.537Z", + "userId": "9i5z4skgqvv58swy", + "user": { + "id": "9i5z4skgqvv58swy", + "name": null, + "username": "admin", + "host": null, + "avatarUrl": "https://localhost:3000/identicon/9i5z4skgqvv58swy", + "avatarBlurhash": null, + "avatarColor": null, + "isAdmin": true, + "isLocked": false, + "speakAsCat": true, + "emojis": [], + "onlineStatus": "online", + "driveCapacityOverrideMb": null + }, + "text": "@micro365 1", + "cw": "Today's #micro365 prompt is:", + "visibility": "public", + "renoteCount": 0, + "repliesCount": 0, + "reactions": {}, + "reactionEmojis": [], + "emojis": [], + "tags": [ + "micro365" + ], + "fileIds": [], + "files": [], + "replyId": "9id1ffugrao33bm4", + "renoteId": null, + "mentions": [ + "9i5z5o9zy11l3skl" + ], + "reply": { + "id": "9id1ffugrao33bm4", + "createdAt": "2023-08-13T13:20:19.192Z", + "userId": "9i5z5o9zy11l3skl", + "user": { + "id": "9i5z5o9zy11l3skl", + "name": null, + "username": "micro365", + "host": null, + "avatarUrl": "https://localhost:3000/files/thumbnail-4e0e8b82-df72-48f7-8100-b7515173da9d", + "avatarBlurhash": "ySPjGct7xu%M-;xu-;%MRjWBoffQofWB~qRjRjayRjfQM{M{t7ofWBt7ayfQ~qj[WBj[M{WBof?bofayfQM{WBfQt7xuofWBofofM{", + "avatarColor": null, + "isLocked": false, + "speakAsCat": true, + "emojis": [], + "onlineStatus": "active", + "driveCapacityOverrideMb": null + }, + "text": "# $[x2 $[font.serif **nudism**]]\n/njˈuːdɪzəm/\n**noun**:\n- The belief in or practice of going nude in social, nonsexualized and frequently mixed-gender groups specifically in cultures where going nude in the social situation is not the norm.\n#writing #microfiction #vss #nudism", + "cw": "Today's #micro365 prompt is:", + "visibility": "public", + "renoteCount": 0, + "repliesCount": 0, + "reactions": {}, + "reactionEmojis": [], + "emojis": [], + "tags": [ + "writing", + "microfiction", + "vss", + "nudism", + "micro365" + ], + "fileIds": [], + "files": [], + "replyId": null, + "renoteId": null + } + } + +