How to use ws2 method in wpt

Best JavaScript code snippet using wpt

workbook-xlsx-writer.spec.js

Source:workbook-xlsx-writer.spec.js Github

copy

Full Screen

1const fs = require('fs');2const {promisify} = require('util');3const testUtils = require('../../utils/index');4const ExcelJS = verquire('exceljs');5const TEST_XLSX_FILE_NAME = './spec/out/wb.test.xlsx';6const IMAGE_FILENAME = `${__dirname}/../data/image.png`;7const fsReadFileAsync = promisify(fs.readFile);8describe('WorkbookWriter', () => {9 it('creates sheets with correct names', () => {10 const wb = new ExcelJS.stream.xlsx.WorkbookWriter();11 const ws1 = wb.addWorksheet('Hello, World!');12 expect(ws1.name).to.equal('Hello, World!');13 const ws2 = wb.addWorksheet();14 expect(ws2.name).to.match(/sheet\d+/);15 });16 describe('Serialise', () => {17 it('xlsx file', () => {18 const options = {19 filename: TEST_XLSX_FILE_NAME,20 useStyles: true,21 };22 const wb = testUtils.createTestBook(23 new ExcelJS.stream.xlsx.WorkbookWriter(options),24 'xlsx'25 );26 return wb27 .commit()28 .then(() => {29 const wb2 = new ExcelJS.Workbook();30 return wb2.xlsx.readFile(TEST_XLSX_FILE_NAME);31 })32 .then(wb2 => {33 testUtils.checkTestBook(wb2, 'xlsx');34 });35 });36 it('shared formula', () => {37 const options = {38 filename: TEST_XLSX_FILE_NAME,39 useStyles: false,40 };41 const wb = new ExcelJS.stream.xlsx.WorkbookWriter(options);42 const ws = wb.addWorksheet('Hello');43 ws.getCell('A1').value = {44 formula: 'ROW()+COLUMN()',45 ref: 'A1:B2',46 result: 2,47 };48 ws.getCell('B1').value = {sharedFormula: 'A1', result: 3};49 ws.getCell('A2').value = {sharedFormula: 'A1', result: 3};50 ws.getCell('B2').value = {sharedFormula: 'A1', result: 4};51 ws.commit();52 return wb53 .commit()54 .then(() => {55 const wb2 = new ExcelJS.Workbook();56 return wb2.xlsx.readFile(TEST_XLSX_FILE_NAME);57 })58 .then(wb2 => {59 const ws2 = wb2.getWorksheet('Hello');60 expect(ws2.getCell('A1').value).to.deep.equal({61 formula: 'ROW()+COLUMN()',62 shareType: 'shared',63 ref: 'A1:B2',64 result: 2,65 });66 expect(ws2.getCell('B1').value).to.deep.equal({67 sharedFormula: 'A1',68 result: 3,69 });70 expect(ws2.getCell('A2').value).to.deep.equal({71 sharedFormula: 'A1',72 result: 3,73 });74 expect(ws2.getCell('B2').value).to.deep.equal({75 sharedFormula: 'A1',76 result: 4,77 });78 });79 });80 it('auto filter', () => {81 const options = {82 filename: TEST_XLSX_FILE_NAME,83 useStyles: false,84 };85 const wb = new ExcelJS.stream.xlsx.WorkbookWriter(options);86 const ws = wb.addWorksheet('Hello');87 ws.getCell('A1').value = 1;88 ws.getCell('B1').value = 1;89 ws.getCell('A2').value = 2;90 ws.getCell('B2').value = 2;91 ws.getCell('A3').value = 3;92 ws.getCell('B3').value = 3;93 ws.autoFilter = 'A1:B1';94 ws.commit();95 return wb96 .commit()97 .then(() => {98 const wb2 = new ExcelJS.Workbook();99 return wb2.xlsx.readFile(TEST_XLSX_FILE_NAME);100 })101 .then(wb2 => {102 const ws2 = wb2.getWorksheet('Hello');103 expect(ws2.autoFilter).to.equal('A1:B1');104 });105 });106 it('Without styles', () => {107 const options = {108 filename: TEST_XLSX_FILE_NAME,109 useStyles: false,110 };111 const wb = testUtils.createTestBook(112 new ExcelJS.stream.xlsx.WorkbookWriter(options),113 'xlsx'114 );115 return wb116 .commit()117 .then(() => {118 const wb2 = new ExcelJS.Workbook();119 return wb2.xlsx.readFile(TEST_XLSX_FILE_NAME);120 })121 .then(wb2 => {122 testUtils.checkTestBook(wb2, 'xlsx', undefined, {123 checkStyles: false,124 });125 });126 });127 it('serializes row styles and columns properly', () => {128 const options = {129 filename: TEST_XLSX_FILE_NAME,130 useStyles: true,131 };132 const wb = new ExcelJS.stream.xlsx.WorkbookWriter(options);133 const ws = wb.addWorksheet('blort');134 const colStyle = {135 font: testUtils.styles.fonts.comicSansUdB16,136 alignment: testUtils.styles.namedAlignments.middleCentre,137 };138 ws.columns = [139 {header: 'A1', width: 10},140 {header: 'B1', style: colStyle},141 {header: 'C1', width: 30},142 {header: 'D1'},143 ];144 ws.getRow(2).font = testUtils.styles.fonts.broadwayRedOutline20;145 ws.getCell('A2').value = 'A2';146 ws.getCell('B2').value = 'B2';147 ws.getCell('C2').value = 'C2';148 ws.getCell('A3').value = 'A3';149 ws.getCell('B3').value = 'B3';150 ws.getCell('C3').value = 'C3';151 return wb152 .commit()153 .then(() => {154 const wb2 = new ExcelJS.Workbook();155 return wb2.xlsx.readFile(TEST_XLSX_FILE_NAME);156 })157 .then(wb2 => {158 const ws2 = wb2.getWorksheet('blort');159 ['A1', 'B1', 'C1', 'A2', 'B2', 'C2', 'A3', 'B3', 'C3'].forEach(160 address => {161 expect(ws2.getCell(address).value).to.equal(address);162 }163 );164 expect(ws2.getCell('B1').font).to.deep.equal(165 testUtils.styles.fonts.comicSansUdB16166 );167 expect(ws2.getCell('B1').alignment).to.deep.equal(168 testUtils.styles.namedAlignments.middleCentre169 );170 expect(ws2.getCell('A2').font).to.deep.equal(171 testUtils.styles.fonts.broadwayRedOutline20172 );173 expect(ws2.getCell('B2').font).to.deep.equal(174 testUtils.styles.fonts.broadwayRedOutline20175 );176 expect(ws2.getCell('C2').font).to.deep.equal(177 testUtils.styles.fonts.broadwayRedOutline20178 );179 expect(ws2.getCell('B3').font).to.deep.equal(180 testUtils.styles.fonts.comicSansUdB16181 );182 expect(ws2.getCell('B3').alignment).to.deep.equal(183 testUtils.styles.namedAlignments.middleCentre184 );185 expect(ws2.getColumn(2).font).to.deep.equal(186 testUtils.styles.fonts.comicSansUdB16187 );188 expect(ws2.getColumn(2).alignment).to.deep.equal(189 testUtils.styles.namedAlignments.middleCentre190 );191 expect(ws2.getColumn(2).width).to.equal(9);192 expect(ws2.getColumn(4).width).to.equal(undefined);193 expect(ws2.getRow(2).font).to.deep.equal(194 testUtils.styles.fonts.broadwayRedOutline20195 );196 });197 });198 it('rich text', () => {199 const options = {200 filename: TEST_XLSX_FILE_NAME,201 useStyles: true,202 };203 const wb = new ExcelJS.stream.xlsx.WorkbookWriter(options);204 const ws = wb.addWorksheet('Hello');205 ws.getCell('A1').value = {206 richText: [207 {208 font: {color: {argb: 'FF0000'}},209 text: 'red ',210 },211 {212 font: {color: {argb: '00FF00'}, bold: true},213 text: ' bold green',214 },215 ],216 };217 ws.getCell('B1').value = 'plain text';218 ws.commit();219 return wb220 .commit()221 .then(() => {222 const wb2 = new ExcelJS.Workbook();223 return wb2.xlsx.readFile(TEST_XLSX_FILE_NAME);224 })225 .then(wb2 => {226 const ws2 = wb2.getWorksheet('Hello');227 expect(ws2.getCell('A1').value).to.deep.equal({228 richText: [229 {230 font: {color: {argb: 'FF0000'}},231 text: 'red ',232 },233 {234 font: {color: {argb: '00FF00'}, bold: true},235 text: ' bold green',236 },237 ],238 });239 expect(ws2.getCell('B1').value).to.equal('plain text');240 });241 });242 it('A lot of sheets', function() {243 this.timeout(5000);244 let i;245 const wb = new ExcelJS.stream.xlsx.WorkbookWriter({246 filename: TEST_XLSX_FILE_NAME,247 });248 const numSheets = 90;249 // add numSheets sheets250 for (i = 1; i <= numSheets; i++) {251 const ws = wb.addWorksheet(`sheet${i}`);252 ws.getCell('A1').value = i;253 }254 return wb255 .commit()256 .then(() => {257 const wb2 = new ExcelJS.Workbook();258 return wb2.xlsx.readFile(TEST_XLSX_FILE_NAME);259 })260 .then(wb2 => {261 for (i = 1; i <= numSheets; i++) {262 const ws2 = wb2.getWorksheet(`sheet${i}`);263 expect(ws2).to.be.ok();264 expect(ws2.getCell('A1').value).to.equal(i);265 }266 });267 });268 it('addRow', () => {269 const options = {270 stream: fs.createWriteStream(TEST_XLSX_FILE_NAME, {flags: 'w'}),271 useStyles: true,272 useSharedStrings: true,273 };274 const workbook = new ExcelJS.stream.xlsx.WorkbookWriter(options);275 const worksheet = workbook.addWorksheet('test');276 const newRow = worksheet.addRow(['hello']);277 newRow.commit();278 worksheet.commit();279 return workbook.commit();280 });281 it('defined names', () => {282 const wb = new ExcelJS.stream.xlsx.WorkbookWriter({283 filename: TEST_XLSX_FILE_NAME,284 });285 const ws = wb.addWorksheet('blort');286 ws.getCell('A1').value = 5;287 ws.getCell('A1').name = 'five';288 ws.getCell('A3').value = 'drei';289 ws.getCell('A3').name = 'threes';290 ws.getCell('B3').value = 'trois';291 ws.getCell('B3').name = 'threes';292 ws.getCell('B3').value = 'san';293 ws.getCell('B3').name = 'threes';294 ws.getCell('E1').value = 'grün';295 ws.getCell('E1').name = 'greens';296 ws.getCell('E2').value = 'vert';297 ws.getCell('E2').name = 'greens';298 ws.getCell('E3').value = 'verde';299 ws.getCell('E3').name = 'greens';300 return wb301 .commit()302 .then(() => {303 const wb2 = new ExcelJS.Workbook();304 return wb2.xlsx.readFile(TEST_XLSX_FILE_NAME);305 })306 .then(wb2 => {307 const ws2 = wb2.getWorksheet('blort');308 expect(ws2.getCell('A1').name).to.equal('five');309 expect(ws2.getCell('A3').name).to.equal('threes');310 expect(ws2.getCell('B3').name).to.equal('threes');311 expect(ws2.getCell('B3').name).to.equal('threes');312 expect(ws2.getCell('E1').name).to.equal('greens');313 expect(ws2.getCell('E2').name).to.equal('greens');314 expect(ws2.getCell('E3').name).to.equal('greens');315 });316 });317 it('does not escape special xml characters', () => {318 const wb = new ExcelJS.stream.xlsx.WorkbookWriter({319 filename: TEST_XLSX_FILE_NAME,320 useSharedStrings: true,321 });322 const ws = wb.addWorksheet('blort');323 const xmlCharacters = 'xml characters: & < > "';324 ws.getCell('A1').value = xmlCharacters;325 return wb326 .commit()327 .then(() => {328 const wb2 = new ExcelJS.Workbook();329 return wb2.xlsx.readFile(TEST_XLSX_FILE_NAME);330 })331 .then(wb2 => {332 const ws2 = wb2.getWorksheet('blort');333 expect(ws2.getCell('A1').value).to.equal(xmlCharacters);334 });335 });336 it('serializes and deserializes dataValidations', () => {337 const options = {filename: TEST_XLSX_FILE_NAME};338 const wb = testUtils.createTestBook(339 new ExcelJS.stream.xlsx.WorkbookWriter(options),340 'xlsx',341 ['dataValidations']342 );343 return wb344 .commit()345 .then(() => {346 const wb2 = new ExcelJS.Workbook();347 return wb2.xlsx.readFile(TEST_XLSX_FILE_NAME);348 })349 .then(wb2 => {350 testUtils.checkTestBook(wb2, 'xlsx', ['dataValidations']);351 });352 });353 it('with zip compression option', () => {354 const options = {355 filename: TEST_XLSX_FILE_NAME,356 useStyles: true,357 zip: {358 zlib: {level: 9}, // Sets the compression level.359 },360 };361 const wb = testUtils.createTestBook(362 new ExcelJS.stream.xlsx.WorkbookWriter(options),363 'xlsx',364 ['dataValidations']365 );366 return wb367 .commit()368 .then(() => {369 const wb2 = new ExcelJS.Workbook();370 return wb2.xlsx.readFile(TEST_XLSX_FILE_NAME);371 })372 .then(wb2 => {373 testUtils.checkTestBook(wb2, 'xlsx', ['dataValidations']);374 });375 });376 it('writes notes', async () => {377 const options = {378 filename: TEST_XLSX_FILE_NAME,379 };380 const wb = new ExcelJS.stream.xlsx.WorkbookWriter(options);381 const ws = wb.addWorksheet('Hello');382 ws.getCell('B2').value = 5;383 ws.getCell('B2').note = 'five';384 const note = {385 texts: [386 {387 font: {388 size: 12,389 color: {argb: 'FFFF6600'},390 name: 'Calibri',391 scheme: 'minor',392 },393 text: 'seven',394 },395 ],396 margins: {397 insetmode: 'auto',398 inset: [0.13, 0.13, 0.25, 0.25],399 },400 protection: {401 locked: 'True',402 lockText: 'True',403 },404 editAs: 'twoCells',405 };406 ws.getCell('D2').value = 7;407 ws.getCell('D2').note = note;408 await wb.commit();409 const wb2 = new ExcelJS.Workbook();410 await wb2.xlsx.readFile(TEST_XLSX_FILE_NAME);411 const ws2 = wb2.getWorksheet('Hello');412 expect(ws2.getCell('B2').value).to.equal(5);413 expect(ws2.getCell('B2').note).to.equal('five');414 expect(ws2.getCell('D2').value).to.equal(7);415 expect(ws2.getCell('D2').note.texts).to.deep.equal(note.texts);416 expect(ws2.getCell('D2').note.margins).to.deep.equal(note.margins);417 expect(ws2.getCell('D2').note.protection).to.deep.equal(note.protection);418 expect(ws2.getCell('D2').note.editAs).to.deep.equal(note.editAs);419 });420 it('Cell annotation supports setting margins and protection properties', async () => {421 const options = {422 filename: TEST_XLSX_FILE_NAME,423 };424 const wb = new ExcelJS.stream.xlsx.WorkbookWriter(options);425 const ws = wb.addWorksheet('Hello');426 ws.getCell('B2').value = 5;427 ws.getCell('B2').note = 'five';428 const note = {429 texts: [430 {431 font: {432 size: 12,433 color: {argb: 'FFFF6600'},434 name: 'Calibri',435 scheme: 'minor',436 },437 text: 'seven',438 },439 ],440 margins: {441 insetmode: 'custom',442 inset: [0.25, 0.25, 0.35, 0.35],443 },444 protection: {445 locked: 'False',446 lockText: 'False',447 },448 editAs: 'oneCells',449 };450 ws.getCell('D2').value = 7;451 ws.getCell('D2').note = note;452 await wb.commit();453 const wb2 = new ExcelJS.Workbook();454 await wb2.xlsx.readFile(TEST_XLSX_FILE_NAME);455 const ws2 = wb2.getWorksheet('Hello');456 expect(ws2.getCell('B2').value).to.equal(5);457 expect(ws2.getCell('B2').note).to.equal('five');458 expect(ws2.getCell('D2').value).to.equal(7);459 expect(ws2.getCell('D2').note.texts).to.deep.equal(note.texts);460 expect(ws2.getCell('D2').note.margins).to.deep.equal(note.margins);461 expect(ws2.getCell('D2').note.protection).to.deep.equal(note.protection);462 expect(ws2.getCell('D2').note.editAs).to.deep.equal(note.editAs);463 });464 it('with background image', async () => {465 const options = {466 filename: TEST_XLSX_FILE_NAME,467 };468 const wb = new ExcelJS.stream.xlsx.WorkbookWriter(options);469 const ws = wb.addWorksheet('Hello');470 const imageId = wb.addImage({471 filename: IMAGE_FILENAME,472 extension: 'jpeg',473 });474 ws.getCell('A1').value = 'Hello, World!';475 ws.addBackgroundImage(imageId);476 await wb.commit();477 const wb2 = new ExcelJS.Workbook();478 await wb2.xlsx.readFile(TEST_XLSX_FILE_NAME);479 const ws2 = wb2.getWorksheet('Hello');480 const backgroundId2 = ws2.getBackgroundImageId();481 const image = wb2.getImage(backgroundId2);482 const imageData = await fsReadFileAsync(IMAGE_FILENAME);483 expect(Buffer.compare(imageData, image.buffer)).to.equal(0);484 });485 it('with background image where worksheet is commited in advance', async () => {486 const options = {487 filename: TEST_XLSX_FILE_NAME,488 };489 const wb = new ExcelJS.stream.xlsx.WorkbookWriter(options);490 const ws = wb.addWorksheet('Hello');491 const imageId = wb.addImage({492 filename: IMAGE_FILENAME,493 extension: 'jpeg',494 });495 ws.getCell('A1').value = 'Hello, World!';496 ws.addBackgroundImage(imageId);497 await ws.commit();498 await wb.commit();499 const wb2 = new ExcelJS.Workbook();500 await wb2.xlsx.readFile(TEST_XLSX_FILE_NAME);501 const ws2 = wb2.getWorksheet('Hello');502 const backgroundId2 = ws2.getBackgroundImageId();503 const image = wb2.getImage(backgroundId2);504 const imageData = await fsReadFileAsync(IMAGE_FILENAME);505 expect(Buffer.compare(imageData, image.buffer)).to.equal(0);506 });507 it('with conditional formatting', async () => {508 const options = {509 filename: TEST_XLSX_FILE_NAME,510 useStyles: true,511 useSharedStrings: true,512 };513 const wb = testUtils.createTestBook(514 new ExcelJS.stream.xlsx.WorkbookWriter(options),515 'xlsx',516 ['conditionalFormatting']517 );518 return wb519 .commit()520 .then(() => {521 const wb2 = new ExcelJS.Workbook();522 return wb2.xlsx.readFile(TEST_XLSX_FILE_NAME);523 })524 .then(wb2 => {525 testUtils.checkTestBook(wb2, 'xlsx', ['conditionalFormatting']);526 });527 });528 it('with conditional formatting that contains numFmt (#1814)', async () => {529 const sheet = 'conditionalFormatting';530 const options = {filename: TEST_XLSX_FILE_NAME, useStyles: true};531 // generate file with conditional formatting that contains styles with numFmt532 const wb1 = new ExcelJS.stream.xlsx.WorkbookWriter(options);533 const ws1 = wb1.addWorksheet(sheet);534 const cf1 = testUtils.conditionalFormatting.abbreviation;535 ws1.addConditionalFormatting(cf1);536 await wb1.commit();537 // read generated file and extract saved conditional formatting rule538 const wb2 = new ExcelJS.Workbook();539 await wb2.xlsx.readFile(TEST_XLSX_FILE_NAME);540 const ws2 = wb2.getWorksheet(sheet);541 const [cf2] = ws2.conditionalFormattings;542 // verify that rules from generated file contain styles with valid numFmt543 cf2.rules.forEach(rule => {544 expect(rule.style.numFmt).to.exist();545 expect(rule.style.numFmt.id).to.be.a('number');546 expect(rule.style.numFmt.formatCode).to.be.a('string');547 });548 });549 });...

Full Screen

Full Screen

forks.ts

Source:forks.ts Github

copy

Full Screen

1import ava from 'ava'2import { setupTwo } from './util/util.js'3ava('conflicting and merging writes to individual file', async t => {4 const VALUES = [5 Buffer.from('writer1', 'utf-8'),6 Buffer.from('writer2', 'utf-8')7 ]8 const {sim, ws1, ws2, writer1, writer2} = await setupTwo(t)9 // conflicting writes10 // HACK sync state prior to disconnect, works around https://github.com/hypercore-protocol/autobase/issues/711 await ws1.indexCore.update()12 await ws2.indexCore.update()13 console.log('\nDISCONNECT\n')14 sim.disconnect(sim.stores[0], sim.stores[1])15 await ws1.writeFile('/test.txt', VALUES[0])16 await ws2.writeFile('/test.txt', VALUES[1])17 // not yet synced18 t.deepEqual(await ws1.readFile('/test.txt'), VALUES[0])19 t.deepEqual(await ws2.readFile('/test.txt'), VALUES[1])20 // synced but in conflict state21 console.log('\nCONNECT\n')22 sim.connect(sim.stores[0], sim.stores[1])23 t.deepEqual(24 await ws1.readFile('/test.txt'),25 await ws2.readFile('/test.txt')26 )27 {28 const info1 = await ws1.statFile('/test.txt')29 t.truthy(info1)30 const info2 = await ws2.statFile('/test.txt')31 t.truthy(info2)32 if (info1 && info2) {33 t.deepEqual(info1, info2)34 }35 if (info1) {36 t.is(info1.conflict, true)37 t.is(info1.otherChanges?.length, 1)38 }39 }40 // merging write41 await ws1.writeFile('/test.txt', VALUES[0])42 t.deepEqual(43 await ws1.readFile('/test.txt'),44 await ws2.readFile('/test.txt')45 )46 {47 const info1 = await ws1.statFile('/test.txt')48 t.truthy(info1)49 const info2 = await ws2.statFile('/test.txt')50 t.truthy(info2)51 if (info1 && info2) {52 t.deepEqual(info1, info2)53 }54 if (info1) {55 t.is(info1.conflict, false)56 t.is(info1.otherChanges?.length, 0) // no conflicts57 }58 }59})60ava('conflicting and merging writes & deletes to individual file', async t => {61 const VALUES = [62 Buffer.from('first write', 'utf-8'),63 Buffer.from('second write', 'utf-8'),64 Buffer.from('third write', 'utf-8')65 ]66 const {sim, ws1, ws2, writer1, writer2} = await setupTwo(t)67 // create a file68 await ws1.writeFile('/test.txt', VALUES[0])69 // conflicting write & delete70 // HACK sync state prior to disconnect, works around https://github.com/hypercore-protocol/autobase/issues/771 await ws1.indexCore.update()72 await ws2.indexCore.update()73 console.log('\nDISCONNECT\n')74 sim.disconnect(sim.stores[0], sim.stores[1])75 await ws1.deleteFile('/test.txt')76 await ws2.writeFile('/test.txt', VALUES[1])77 // not yet synced78 t.deepEqual(await ws1.readFile('/test.txt'), undefined)79 t.deepEqual(await ws2.readFile('/test.txt'), VALUES[1])80 // synced but in conflict state81 console.log('\nCONNECT\n')82 sim.connect(sim.stores[0], sim.stores[1])83 t.deepEqual(84 await ws1.readFile('/test.txt'),85 await ws2.readFile('/test.txt')86 )87 {88 const info1 = await ws1.statFile('/test.txt')89 t.truthy(info1)90 const info2 = await ws2.statFile('/test.txt')91 t.truthy(info2)92 if (info1 && info2) {93 t.deepEqual(info1, info2)94 }95 if (info1) {96 t.is(info1.conflict, true)97 t.is(info1.otherChanges?.length, 1)98 }99 }100 // file is still present in listing even though it may be in a "deleted" state101 t.is((await ws1.listFiles('/')).length, 1)102 t.is((await ws2.listFiles('/')).length, 1)103 // merging write104 await ws1.writeFile('/test.txt', VALUES[2])105 t.deepEqual(106 await ws1.readFile('/test.txt'),107 await ws2.readFile('/test.txt')108 )109 {110 const info1 = await ws1.statFile('/test.txt')111 t.truthy(info1)112 const info2 = await ws2.statFile('/test.txt')113 t.truthy(info2)114 if (info1 && info2) {115 t.deepEqual(info1, info2)116 }117 if (info1){118 t.is(info1.conflict, false)119 t.is(info1.otherChanges?.length, 0) // no conflicts120 }121 }122})123ava('conflicting and merging writes & moves to individual file', async t => {124 const VALUES = [125 Buffer.from('first write', 'utf-8'),126 Buffer.from('second write', 'utf-8'),127 Buffer.from('third write', 'utf-8')128 ]129 const {sim, ws1, ws2, writer1, writer2} = await setupTwo(t)130 // create a file131 await ws1.writeFile('/test.txt', VALUES[0])132 // conflicting write & delete133 // HACK sync state prior to disconnect, works around https://github.com/hypercore-protocol/autobase/issues/7134 await ws1.indexCore.update()135 await ws2.indexCore.update()136 console.log('\nDISCONNECT\n')137 sim.disconnect(sim.stores[0], sim.stores[1])138 await ws1.moveFile('/test.txt', '/test2.txt')139 await ws2.writeFile('/test.txt', VALUES[1])140 // not yet synced141 t.deepEqual(await ws1.readFile('/test.txt'), undefined)142 t.deepEqual(await ws1.readFile('/test2.txt'), VALUES[0])143 t.deepEqual(await ws2.readFile('/test.txt'), VALUES[1])144 // synced but in conflict state145 console.log('\nCONNECT\n')146 sim.connect(sim.stores[0], sim.stores[1])147 t.deepEqual(148 await ws1.readFile('/test.txt'),149 await ws2.readFile('/test.txt')150 )151 {152 const info1 = await ws1.statFile('/test.txt')153 t.truthy(info1)154 const info2 = await ws2.statFile('/test.txt')155 t.truthy(info2)156 if (info1 && info2) {157 t.deepEqual(info1, info2)158 }159 if (info1){160 t.is(info1.conflict, true)161 t.is(info1.otherChanges?.length, 1)162 }163 }164 t.deepEqual(165 await ws1.readFile('/test2.txt'),166 await ws2.readFile('/test2.txt')167 )168 {169 const info1 = await ws1.statFile('/test2.txt')170 t.truthy(info1)171 const info2 = await ws2.statFile('/test2.txt')172 t.truthy(info2)173 if (info1 && info2) {174 t.deepEqual(info1, info2)175 }176 if (info1){177 t.is(info1.conflict, false)178 t.is(info1.otherChanges?.length, 0)179 }180 }181 // file is still present in listing even though it may be in a "deleted" state182 t.is((await ws1.listFiles('/')).length, 2)183 t.is((await ws2.listFiles('/')).length, 2)184})185ava('conflicting and merging writes & copies to individual file', async t => {186 const VALUES = [187 Buffer.from('first write', 'utf-8'),188 Buffer.from('second write', 'utf-8'),189 Buffer.from('third write', 'utf-8')190 ]191 const {sim, ws1, ws2, writer1, writer2} = await setupTwo(t)192 // create two file193 await ws1.writeFile('/test.txt', VALUES[0])194 await ws1.writeFile('/test2.txt', VALUES[1])195 // conflicting write & delete196 // HACK sync state prior to disconnect, works around https://github.com/hypercore-protocol/autobase/issues/7197 await ws1.indexCore.update()198 await ws2.indexCore.update()199 console.log('\nDISCONNECT\n')200 sim.disconnect(sim.stores[0], sim.stores[1])201 await ws1.copyFile('/test2.txt', '/test.txt')202 await ws2.writeFile('/test.txt', VALUES[2])203 // not yet synced204 t.deepEqual(await ws1.readFile('/test.txt'), VALUES[1])205 t.deepEqual(await ws1.readFile('/test2.txt'), VALUES[1])206 t.deepEqual(await ws2.readFile('/test.txt'), VALUES[2])207 // synced but in conflict state208 console.log('\nCONNECT\n')209 sim.connect(sim.stores[0], sim.stores[1])210 t.deepEqual(211 await ws1.readFile('/test.txt'),212 await ws2.readFile('/test.txt')213 )214 {215 const info1 = await ws1.statFile('/test.txt')216 t.truthy(info1)217 const info2 = await ws2.statFile('/test.txt')218 t.truthy(info2)219 if (info1 && info2) {220 t.deepEqual(info1, info2)221 }222 if (info1){223 t.is(info1.conflict, true)224 t.is(info1.otherChanges?.length, 1)225 }226 }227 t.deepEqual(228 await ws1.readFile('/test2.txt'),229 await ws2.readFile('/test2.txt')230 )231 {232 const info1 = await ws1.statFile('/test2.txt')233 t.truthy(info1)234 const info2 = await ws2.statFile('/test2.txt')235 t.truthy(info2)236 if (info1 && info2) {237 t.deepEqual(info1, info2)238 }239 if (info1){240 t.is(info1.conflict, false)241 t.is(info1.otherChanges?.length, 0)242 }243 }244 // file is still present in listing even though it may be in a "deleted" state245 t.is((await ws1.listFiles('/')).length, 2)246 t.is((await ws2.listFiles('/')).length, 2)...

Full Screen

Full Screen

writer-management.ts

Source:writer-management.ts Github

copy

Full Screen

1import ava from 'ava'2// @ts-ignore no types3import Hypercore from 'hypercore'4// @ts-ignore no types5import ram from 'random-access-memory'6// @ts-ignore no types7import Corestore from 'corestore'8// @ts-ignore no types9import crypto from 'hypercore-crypto'10import * as sfw from '../src/index.js'11function inst () {12 const swarmKeyPair = crypto.keyPair()13 const store = new Corestore(ram)14 return {swarmKeyPair, store}15}16function connect (inst1: any, inst2: any) {17 const s = inst1.store.replicate(Hypercore.createProtocolStream(true, {keyPair: inst1.swarmKeyPair}))18 s.pipe(inst2.store.replicate(Hypercore.createProtocolStream(false, {keyPair: inst2.swarmKeyPair}))).pipe(s)19}20ava('writer invite flow', async t => {21 const inst1 = inst()22 const inst2 = inst()23 connect(inst1, inst2)24 const ws1 = await sfw.Workspace.createNew(inst1.store, inst1.swarmKeyPair)25 const ws2 = await sfw.Workspace.load(inst2.store, inst2.swarmKeyPair, ws1.key)26 t.deepEqual(ws1.key, ws2.key)27 t.is(ws1.writers.length, 1)28 t.is(ws2.writers.length, 1)29 const invite = await ws1.createInvite('user two')30 await ws2.useInvite(invite)31 t.is(ws1.writers.length, 2)32 t.is(ws2.writers.length, 2)33 t.is(ws2.getMyWriter()?.name, 'user two')34})35ava('writer perms: admins can edit all, non-admins can only edit self and cant self-promote to admin', async t => {36 const inst1 = inst()37 const inst2 = inst()38 connect(inst1, inst2)39 const ws1 = await sfw.Workspace.createNew(inst1.store, inst1.swarmKeyPair)40 const ws2 = await sfw.Workspace.load(inst2.store, inst2.swarmKeyPair, ws1.key)41 t.deepEqual(ws1.key, ws2.key)42 t.is(ws1.writers.length, 1)43 t.is(ws2.writers.length, 1)44 const invite = await ws1.createInvite('user two')45 await ws2.useInvite(invite)46 t.is(ws1.writers.length, 2)47 t.is(ws2.writers.length, 2)48 const writer1 = ws1.getMyWriter()49 const writer2 = ws2.getMyWriter()50 t.truthy(writer1)51 t.truthy(writer2)52 if (writer1 && writer2) {53 await t.throwsAsync(ws2.putWriter(writer1.publicKey, {name: 'bob'}))54 await ws2.putWriter(writer2.publicKey, {name: 'bob'})55 await t.throwsAsync(ws2.putWriter(writer2.publicKey, {admin: true}))56 for (const ws of [ws1, ws2]) {57 const writers = await ws.listWriters()58 const w2 = writers.find(w => w.publicKey.equals(writer2.publicKey))59 t.truthy(w2)60 if (w2) {61 t.is(w2.name, 'bob')62 t.falsy(w2.isAdmin)63 t.falsy(w2.isFrozen)64 }65 }66 await ws1.putWriter(writer1.publicKey, {name: 'alice'})67 await ws1.putWriter(writer2.publicKey, {name: 'robert'})68 await ws1.putWriter(writer2.publicKey, {admin: true})69 for (const ws of [ws1, ws2]) {70 const writers = await ws.listWriters()71 const w1 = writers.find(w => w.publicKey.equals(writer1.publicKey))72 t.truthy(w1)73 if (w1) {74 t.is(w1.name, 'alice')75 t.truthy(w1.isAdmin)76 t.falsy(w1.isFrozen)77 }78 const w2 = writers.find(w => w.publicKey.equals(writer2.publicKey))79 t.truthy(w2)80 if (w2) {81 t.is(w2.name, 'robert')82 t.truthy(w2.isAdmin)83 t.falsy(w2.isFrozen)84 }85 }86 // writer2 is now admin87 await ws2.putWriter(writer1.publicKey, {name: 'ALICE'})88 await ws2.putWriter(writer2.publicKey, {name: 'ROBERT'})89 await ws2.putWriter(writer1.publicKey, {admin: false})90 for (const ws of [ws1, ws2]) {91 const writers = await ws.listWriters()92 const w1 = writers.find(w => w.publicKey.equals(writer1.publicKey))93 t.truthy(w1)94 if (w1) {95 t.is(w1.name, 'ALICE')96 t.falsy(w1.isAdmin)97 t.falsy(w1.isFrozen)98 }99 const w2 = writers.find(w => w.publicKey.equals(writer2.publicKey))100 t.truthy(w2)101 if (w2) {102 t.is(w2.name, 'ROBERT')103 t.truthy(w2.isAdmin)104 t.falsy(w2.isFrozen)105 }106 }107 // writer1 is no longer admin108 await t.throwsAsync(ws1.putWriter(writer2.publicKey, {name: 'bob'}))109 await ws2.putWriter(writer1.publicKey, {name: 'ALICIA'})110 await t.throwsAsync(ws1.putWriter(writer1.publicKey, {admin: true}))111 for (const ws of [ws1, ws2]) {112 const writers = await ws.listWriters()113 const w1 = writers.find(w => w.publicKey.equals(writer1.publicKey))114 t.truthy(w1)115 if (w1) {116 t.is(w1.name, 'ALICIA')117 t.falsy(w1.isAdmin)118 t.falsy(w1.isFrozen)119 }120 const w2 = writers.find(w => w.publicKey.equals(writer2.publicKey))121 t.truthy(w2)122 if (w2) {123 t.is(w2.name, 'ROBERT')124 t.truthy(w2.isAdmin)125 t.falsy(w2.isFrozen)126 }127 }128 }129})130ava('cant write without being a writer', async t => {131 const inst1 = inst()132 const inst2 = inst()133 connect(inst1, inst2)134 const ws1 = await sfw.Workspace.createNew(inst1.store, inst1.swarmKeyPair)135 const ws2 = await sfw.Workspace.load(inst2.store, inst2.swarmKeyPair, ws1.key)136 t.deepEqual(ws1.key, ws2.key)137 t.is(ws1.writers.length, 1)138 t.is(ws2.writers.length, 1)139 await t.throwsAsync(ws2.writeFile('/foo.txt', 'bar'), {message: 'Not a writer'})140})141ava('invalid invites', async t => {142 const inst1 = inst()143 const inst2 = inst()144 connect(inst1, inst2)145 const ws1 = await sfw.Workspace.createNew(inst1.store, inst1.swarmKeyPair)146 const ws2 = await sfw.Workspace.load(inst2.store, inst2.swarmKeyPair, ws1.key)147 t.deepEqual(ws1.key, ws2.key)148 t.is(ws1.writers.length, 1)149 t.is(ws2.writers.length, 1)150 const invite = await ws1.createInvite('user two')151 const [prefix, key, token] = invite.split(':')152 await t.throwsAsync(ws2.useInvite(`foo`), {message: 'Not an invite code'})153 await t.throwsAsync(ws2.useInvite(`${prefix}:${crypto.keyPair().publicKey.toString('hex')}:${token}`), {message: 'Can\'t find the user that created this invite. Are they online? (Are you?)'})154 await t.throwsAsync(ws2.useInvite(`${prefix}:${key}:12345`), {message: 'Invalid invite code (12345)'})...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var api = new wpt('www.webpagetest.org');3 if (err) {4 console.log(err);5 }6 else {7 console.log(data);8 }9});

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3var options = {4};5 if (err) return console.error(err);6 console.log('Test started: ' + data.data.jsonUrl);7 console.log('User can view the test at: ' + data.data.userUrl);8 wpt.getTestResults(data.data.testId, function(err, data) {9 if (err) return console.error(err);10 console.log('Test completed:');11 console.log(data);12 });13});14{ statusCode: 200,15 { testId: '170920_1H_1c9e9f2d8c2a0e0b1a7c7a2f8d2c2a9f',

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var server = new wpt('www.webpagetest.org', 'A.12345678901234567890123456789012');3var options = {4};5 if (err) return console.error(err);6 console.log(data);7});8### `var wpt = require('webpagetest')(server, apiKey)`9### `wpt.runTest(url, options, callback)`

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var wpt2 = new wpt('www.webpagetest.org');3var fs = require('fs');4var location = 'Dulles:Chrome';5var runs = 1;6var timeout = 600;7var video = 1;8var webpagetest = require('webpagetest');9var options = {10};11var test = new webpagetest('www.webpagetest.org', options);12test.runTest(url, function(err, data) {13 if (err) return console.error(err);14 console.log('Test started: ' + data.data.testId);15 console.log('View your test at: ' + data.data.userUrl);16});17var testId = '151018_5V_1d0d8f0a2c2e2f1e1b1a1e1a1c1f1e1f';18test.getTestResults(testId, function(err, data) {19 if (err) return console.error(err);20 console.log('Test completed');21 console.log(data.data.median.firstView.SpeedIndex);22 console.log(data.data.median.firstView.TTFB);23 console.log(data.data.median.firstView.render);24 console.log(data.data.median.firstView.fullyLoaded);25 console.log(data.data.median.firstView.docTime);26 console.log(data.data.median.firstView.SpeedIndex);27 console.log(data.data.median.firstView.TTFB);28 console.log(data.data.median.firstView.render);29 console.log(data.data.median.firstView.fullyLoaded);30 console.log(data.data.median.firstView.docTime);31 console.log(data.data.median.firstView.SpeedIndex);32 console.log(data.data.median.firstView.TTFB);33 console.log(data.data.median.firstView.render);34 console.log(data.data.median.firstView.fullyLoaded);35 console.log(data.data.median.firstView.docTime);36 console.log(data.data.median.firstView.SpeedIndex);37 console.log(data.data.median.firstView.TTFB);38 console.log(data.data.median.firstView.render);

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var options = {3};4var test = new wpt(options);5 videoParams: {6 }7}, function(err, data) {8 if (err) {9 console.error(err);10 } else {11 console.log(data.data.runs[1].firstView.videoFrames);12 }13});14var wpt = require('webpagetest');15var options = {16};17var test = new wpt(options);18 videoParams: {19 }20}, function(err, data) {21 if (err) {22 console.error(err);23 } else {24 console.log(data.data.runs[1].firstView.videoFrames);25 }26});27var wpt = require('webpagetest');28var options = {

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful