Best JavaScript code snippet using argos
multi-sig-wallet.js
Source:multi-sig-wallet.js
...15 describe("constructor", () => {16 it("should deploy a contract", async () => { 17 18 for (let i = 0; i < owners.length; i++) {19 assert.equal(await wallet.owners(i), owners[i])20 }21 22 assert.equal(23 await wallet.numConfirmationsRequired(),24 NUM_CONFIRMATIONS_REQUIRED25 )26 }); 27 28 it("should reject if no owners", async () => {29 await expect(MultiSigWallet.new([], NUM_CONFIRMATIONS_REQUIRED)).to.be30 .rejected31 });32 33 it("should reject if number of confrmations required > owners", async () => {...
ProjectRegistry-test.ts
Source:ProjectRegistry-test.ts
1const hre = require("hardhat");2const { expect } = require("chai");3const { ethers } = require("hardhat");4const testMetadata = { protocol: 1, pointer: "test-metadata" };5const updatedMetadata = { protocol: 1, pointer: "updated-metadata" };6const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";7const OWNERS_LIST_SENTINEL = "0x0000000000000000000000000000000000000001";8describe("ProjectRegistry", function () {9 before(async function () {10 [this.owner, this.nonOwner, ...this.accounts] = await ethers.getSigners();11 const ProjectRegistry = await hre.ethers.getContractFactory("ProjectRegistry", this.owner);12 this.contract = await ProjectRegistry.deploy();13 await this.contract.deployed();14 });15 it("allows to initilize once", async function () {16 await this.contract.connect(this.owner).initialize();17 });18 it("doesn't allow to initilize again", async function () {19 await expect(this.contract.connect(this.owner).initialize()).to.be.revertedWith("contract is already initialized");20 });21 it("creates a new project and adds it to the projects list", async function () {22 expect(await this.contract.projectsCount()).to.equal("0");23 await this.contract.createProject(testMetadata);24 expect(await this.contract.projectsCount()).to.equal("1");25 const project = await this.contract.projects(0);26 expect(project.id).to.equal("0");27 const [protocol, pointer] = project.metadata;28 expect(protocol).to.equal(testMetadata.protocol);29 expect(pointer).to.equal(testMetadata.pointer);30 const owners = await this.contract.getProjectOwners(project.id);31 expect(owners.length).to.equal(1);32 expect(owners[0]).to.equal(this.owner.address);33 });34 it("does not allow update of project metadata if not owner", async function () {35 const project = await this.contract.projects(0);36 await expect(this.contract.connect(this.nonOwner).updateProjectMetadata(project.id, updatedMetadata)).to.be.revertedWith("PR000");37 });38 it("updates project metadata", async function () {39 const project = await this.contract.projects(0);40 await this.contract.connect(this.owner).updateProjectMetadata(project.id, updatedMetadata);41 const updatedProject = await this.contract.projects(0);42 const [protocol, pointer] = updatedProject.metadata;43 expect(protocol).to.equal(updatedMetadata.protocol);44 expect(pointer).to.equal(updatedMetadata.pointer);45 });46 it("does not allow to add an owner if not owner", async function () {47 const projectID = 0;48 await expect(this.contract.connect(this.nonOwner).addProjectOwner(projectID, this.nonOwner.address)).to.be.revertedWith("PR000");49 });50 it("emits AddedOwner and RemovedOwner when OwnerList is modified", async function () {51 const projectID = 0;52 const addTx = await this.contract.connect(this.owner).addProjectOwner(projectID, this.accounts[1].address);53 const { events: addEvents } = await addTx.wait();54 const [emittedProject0, addedOwner] = addEvents[0].args;55 expect(emittedProject0).to.equal(projectID);56 expect(addedOwner).to.equal(this.accounts[1].address);57 expect(addEvents[0].event).to.equal("OwnerAdded");58 const removeTx = await this.contract.connect(this.owner).removeProjectOwner(projectID, OWNERS_LIST_SENTINEL, this.accounts[1].address);59 const { events } = await removeTx.wait();60 const [emittedProject1, removedOwner] = events[0].args;61 expect(emittedProject1).to.equal(projectID);62 expect(removedOwner).to.equal(this.accounts[1].address);63 expect(events[0].event).to.equal("OwnerRemoved");64 });65 it("adds owner to project", async function () {66 const projectID = 0;67 expect(await this.contract.connect(this.owner).projectOwnersCount(projectID)).to.equal("1");68 const prevOwners = await this.contract.getProjectOwners(projectID);69 expect(prevOwners.length).to.equal(1);70 expect(prevOwners[0]).to.equal(this.owner.address);71 for (let i = 0; i < 3; i++) {72 await this.contract.connect(this.owner).addProjectOwner(projectID, this.accounts[i].address);73 }74 expect(await this.contract.projectOwnersCount(projectID)).to.equal("4");75 const owners = await this.contract.getProjectOwners(projectID);76 expect(owners.length).to.equal(4);77 expect(owners[0]).to.equal(this.accounts[2].address);78 expect(owners[1]).to.equal(this.accounts[1].address);79 expect(owners[2]).to.equal(this.accounts[0].address);80 expect(owners[3]).to.equal(this.owner.address);81 });82 it("does not allow to remove an owner if not owner", async function () {83 const projectID = 0;84 await expect(this.contract.connect(this.nonOwner).removeProjectOwner(projectID, this.owner.address, this.owner.address)).to.be.revertedWith(85 "PR000"86 );87 });88 it("does not allow to remove owner 0", async function () {89 const projectID = 0;90 await expect(this.contract.connect(this.owner).removeProjectOwner(projectID, this.owner.address, ZERO_ADDRESS)).to.be.revertedWith("PR001");91 });92 it("does not allow to remove owner equal to OWNERS_LIST_SENTINEL", async function () {93 const projectID = 0;94 await expect(this.contract.connect(this.owner).removeProjectOwner(projectID, this.owner.address, OWNERS_LIST_SENTINEL)).to.be.revertedWith(95 "PR001"96 );97 });98 it("does not allow to remove owner with prevOwner must equal owner", async function () {99 const projectID = 0;100 await expect(this.contract.connect(this.owner).removeProjectOwner(projectID, this.nonOwner.address, this.owner.address)).to.be.revertedWith(101 "PR003"102 );103 });104 it("removes owner", async function () {105 const projectID = 0;106 const currentOwners = await this.contract.getProjectOwners(projectID);107 expect(await this.contract.projectOwnersCount(projectID)).to.equal("4");108 const owners = await this.contract.getProjectOwners(projectID);109 expect(owners.length).to.equal(4);110 expect(currentOwners[0]).to.equal(this.accounts[2].address);111 expect(currentOwners[1]).to.equal(this.accounts[1].address);112 expect(currentOwners[2]).to.equal(this.accounts[0].address);113 expect(currentOwners[3]).to.equal(this.owner.address);114 await this.contract.connect(this.owner).removeProjectOwner(projectID, this.accounts[1].address, this.accounts[0].address);115 expect(await this.contract.projectOwnersCount(projectID)).to.equal("3");116 let newOwners = await this.contract.getProjectOwners(projectID);117 expect(newOwners.length).to.equal(3);118 await this.contract.connect(this.owner).removeProjectOwner(projectID, this.accounts[1].address, this.owner.address);119 expect(await this.contract.projectOwnersCount(projectID)).to.equal("2");120 newOwners = await this.contract.getProjectOwners(projectID);121 expect(newOwners.length).to.equal(2);122 await this.contract.connect(this.accounts[2]).removeProjectOwner(projectID, OWNERS_LIST_SENTINEL, this.accounts[2].address);123 expect(await this.contract.projectOwnersCount(projectID)).to.equal("1");124 newOwners = await this.contract.getProjectOwners(projectID);125 expect(newOwners.length).to.equal(1);126 expect(newOwners[0]).to.eq(this.accounts[1].address);127 });128 it("does not allow to remove owner if single owner", async function () {129 const projectID = 0;130 expect(await this.contract.projectOwnersCount(projectID)).to.equal("1");131 const currentOwners = await this.contract.getProjectOwners(projectID);132 expect(currentOwners[0]).to.eq(this.accounts[1].address);133 await expect(134 this.contract.connect(this.accounts[1]).removeProjectOwner(projectID, OWNERS_LIST_SENTINEL, this.accounts[1].address)135 ).to.be.revertedWith("PR004");136 });...
server.js
Source:server.js
1var express = require('express');2var bodyParser = require('body-parser');3var app = express();4app.use(bodyParser.json());5app.use(bodyParser.urlencoded({ extended: false }));6var owners = [7 {8 id: 1,9 name: "Adam",10 pets: [11 {12 id: 1,13 name: "Vera",14 type: "Dog"15 },16 {17 id: 2,18 name: "Felix",19 type: "Cat"20 }21 ]22 },23 {24 id: 2,25 name: "Kamilah",26 pets: [27 {28 id: 3,29 name: "Doug",30 type: "Dog"31 }32 ]33 }34];35// I cheated here and made each pet have a unique ID ;)36let ownerId = 337let petId = 438// Helper function39function getOwnerById(ownerId) {40 return owners.find( (owner) => owner.id === parseInt(ownerId))41}42function getPetById(owner, petId) {43 if (owner.pets){44 return owner.pets.find( (pet) => pet.id === parseInt(petId))45 }46 return undefined47}48// GET /api/owners49app.get('/api/owners', (req, res) => {50 res.send(owners)51})52// GET /api/owners/:id53app.get('/api/owners/:id', (req, res) => {54 // This could just as easily be `find` instead of `filter`55 let result = owners.filter( (owner) => {56 return owner.id === parseInt(req.params.id)57 })58 if (result.length === 1) {59 res.send(result[0])60 } else if (result.length === 0) {61 res.status(404).send("CLIENT ERROR: Owner doesn't exist.")62 } else {63 res.status(500).send("SERVER ERROR: Multiple records for ID.")64 }65})66// POST /api/owners67app.post('/api/owners', (req, res) => {68 let newOwner = req.body69 newOwner["id"] = ownerId++70 // In a real application, we would need to check nested objects71 // like Pets and make sure they exist or create them in their own table.72 owners.push(newOwner)73 res.send(newOwner)74})75// PUT /api/owners/:id76app.put('/api/owners/:id', (req, res) => {77 let ownerIndex = owners.findIndex( (owner) => {78 return owner.id === parseInt(req.params.id)79 }) 80 if(ownerIndex >= 0) {81 // Overwrite the id in the body with the one from the URL82 req.body.id = owners[ownerIndex].id83 owners[ownerIndex] = req.body 84 res.send(owners[ownerIndex])85 } else {86 res.status(404).send("ERROR: Owner does not exist") 87 }88})89// DELETE /api/owners/:id90app.delete('/api/owners/:id', (req, res) => {91 let originalLength = owners.length92 owners = owners.filter( (owner) => {93 return owner.id !== parseInt(req.params.id)94 })95 if (owners.length === originalLength){96 // Changed this to 404, which is technically right: 97 // https://stackoverflow.com/questions/4088350/is-rest-delete-really-idempotent98 res.status(404).send("ERROR: Owner does not exist")99 }100 res.send("SUCCESS: Deleted this owner")101})102// GET /api/owners/:id/pets103app.get('/api/owners/:id/pets', (req, res) => {104 owner = getOwnerById(req.params.id)105 if(!owner){106 res.status(404).send('CLIENT ERROR: Owner does not exist')107 }108 else if(!owner.pets) {109 res.send([])110 } 111 else {112 res.send(owner.pets)113 }114})115// GET /api/owners/:id/pets/:petId116app.get('/api/owners/:id/pets/:petId', (req, res) => {117 owner = getOwnerById(req.params.id)118 if(!owner){119 res.status(404).send('CLIENT ERROR: Owner does not exist')120 }121 pet = getPetById(owner, req.params.petId)122 if(!pet) {123 res.status(404).send('CLIENT ERROR: Pet does not exist')124 } 125 else {126 res.send(pet)127 }128})129// POST /api/owners/:id/pets130app.post('/api/owners/:id/pets', (req, res) => {131 owner = getOwnerById(req.params.id)132 if(!owner){133 res.status(404).send('CLIENT ERROR: Owner does not exist')134 }135 let newPet = req.body136 newPet["id"] = petId++137 owner.pets.push(newPet)138 res.send(newPet)139})140// PUT /api/owners/:id/pets/:petId141app.put('/api/owners/:id/pets/:petId', (req, res) => {142 owner = getOwnerById(req.params.id)143 if(!owner){144 res.status(404).send('CLIENT ERROR: Owner does not exist')145 }146 else if(!owner.pets){147 res.status(404).send('CLIENT ERROR: Owner does not have any pets')148 }149 let petIndex = owner.pets.findIndex( pet => pet.id === parseInt(req.params.petId)) 150 if(petIndex >= 0) {151 // Overwrite the id in the body with the one from the URL152 req.body.id = owner.pets[petIndex].id153 owner.pets[petIndex] = req.body 154 res.send(owner.pets[petIndex])155 } else {156 res.status(404).send("ERROR: Pet does not exist") 157 }158})159// DELETE /api/owners/:id/pets/:petId160app.delete('/api/owners/:id/pets/:petId', (req, res) => {161 162 owner = getOwnerById(req.params.id)163 if(!owner){164 res.status(404).send('CLIENT ERROR: Owner does not exist')165 }166 pet = getPetById(owner, req.params.petId)167 if(!pet) {168 res.status(404).send('CLIENT ERROR: Pet does not exist')169 } 170 else {171 owner.pets = owner.pets.filter( (pet) => {172 return pet.id !== parseInt(req.params.petId)173 })174 res.send("SUCCESS: Deleted this pet")175 }176})177app.listen(3000, function(){178 console.log('Pets API is now listening on port 3000...');...
Using AI Code Generation
1var argosy = require('argosy')2var pattern = require('argosy-pattern')3var service = argosy()4service.accept({5 test: owners('test')6})7service.listen(3000)8var argosy = require('argosy')9var pattern = require('argosy-pattern')10var service = argosy()11service.accept({12 test: owners('test')13})14service.listen(3001)
Using AI Code Generation
1var argosy = require('argosy')2var owners = require('argosy-pattern/owners')3var pattern = require('argosy-pattern')4var service = argosy()5service.accept(pattern({6 'owners': owners('name')7}), function (msg, cb) {8 cb(null, { name: 'John' })9})10service.listen(8000)11var argosy = require('argosy')12var owners = require('argosy-pattern/owners')13var pattern = require('argosy-pattern')14var service = argosy()15service.accept(pattern({16 'owners': owners('name')17}), function (msg, cb) {18 cb(null, { name: 'John' })19})20service.listen(8001)21var argosy = require('argosy')22var owners = require('argosy-pattern/owners')23var pattern = require('argosy-pattern')24var service = argosy()25service.accept(pattern({26 'owners': owners('name')27}), function (msg, cb) {28 cb(null, { name: 'John' })29})30service.listen(8002)31var argosy = require('argosy')32var owners = require('argosy-pattern/owners')33var pattern = require('argosy-pattern')34var service = argosy()35service.accept(pattern({36 'owners': owners('name')37}), function (msg, cb) {38 cb(null, { name: 'John' })39})40service.listen(8003)41var argosy = require('argosy')42var owners = require('argosy-pattern/owners')43var pattern = require('argosy-pattern')44var service = argosy()45service.accept(pattern({46 'owners': owners('name')47}), function (msg, cb) {48 cb(null, { name: 'John' })49})50service.listen(8004)51var argosy = require('argosy')
Using AI Code Generation
1var argosy = require('../')2var argosyPatt = require('argosy-pattern')3var owners = require('argosy-owners')4var argosyService = argosy()5argosyService.use('owner', owners(argosyService))6argosyService.accept({ owner: argosyPatt.value }).on('data', function (msg) {7 console.log('owner: ', msg.owner)8})9argosyService.accept({ owner: argosyPatt.value }).on('data', function (msg) {10 console.log('owner: ', msg.owner)11})12argosyService.accept({ owner: argosyPatt.value }).on('data', function (msg) {13 console.log('owner: ', msg.owner)14})15argosyService.accept({ owner: argosyPatt.value }).on('data', function (msg) {16 console.log('owner: ', msg.owner)17})18argosyService.accept({ owner: argosyPatt.value }).on('data', function (msg) {19 console.log('owner: ', msg.owner)20})21argosyService.accept({ owner: argosyPatt.value }).on('data', function (msg) {22 console.log('owner: ', msg.owner)23})24argosyService.accept({ owner: argosyPatt.value }).on('data', function (msg) {25 console.log('owner: ', msg.owner)26})27argosyService.accept({ owner: argosyPatt.value }).on('data', function (msg) {28 console.log('owner: ', msg.owner)29})30argosyService.accept({ owner: argosyPatt.value }).on('data', function (msg) {31 console.log('owner: ', msg.owner)32})33argosyService.accept({ owner: argosyPatt.value }).on('data', function (msg) {34 console.log('owner: ', msg.owner)35})36argosyService.accept({ owner: argosyPatt.value }).on('data', function (msg) {37 console.log('owner: ', msg.owner)38})39argosyService.accept({ owner: argosyPatt.value }).on('data', function (msg) {40 console.log('
Using AI Code Generation
1function owners () {2 return argosy.owners().then(function (owners) {3 console.log(owners)4 })5}6owners()7function owners () {8 return argosy.owners().then(function (owners) {9 console.log(owners)10 })11}12owners()13function owners () {14 return argosy.owners().then(function (owners) {15 console.log(owners)16 })17}18owners()19function owners () {20 return argosy.owners().then(function (owners) {21 console.log(owners)22 })23}24owners()25function owners () {26 return argosy.owners().then(function (owners) {27 console.log(owners)28 })29}30owners()31function owners () {32 return argosy.owners().then(function (owners) {33 console.log(owners)34 })35}36owners()37function owners () {38 return argosy.owners().then(function (owners) {39 console.log(owners)40 })41}42owners()43function owners () {44 return argosy.owners().then(function (owners) {45 console.log(owners)46 })47}48owners()49function owners () {50 return argosy.owners().then(function (owners) {51 console.log(owners)52 })53}54owners()55function owners () {56 return argosy.owners().then(function (owners) {57 console.log(owners)58 })59}60owners()61function owners () {62 return argosy.owners().then(function (owners) {63 console.log(owners)64 })65}66owners()
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!