|
| 1 | +import{Node}from'../Node'; |
| 2 | +import{bufferFrom}from'../../vendor/node/internal/buffer'; |
| 3 | + |
| 4 | +describe('Node',()=>{ |
| 5 | +describe('buffer management with capacity tracking',()=>{ |
| 6 | +it('should handle sequential small writes efficiently',()=>{ |
| 7 | +constnode=newNode(1,0o666); |
| 8 | +constsmallBuf=bufferFrom('x'); |
| 9 | +for(leti=0;i<100;i++)node.write(smallBuf,0,1,i); |
| 10 | +expect(node.getSize()).toBe(100); |
| 11 | +constresult=node.getBuffer(); |
| 12 | +expect(result.length).toBe(100); |
| 13 | +expect(result.toString()).toBe('x'.repeat(100)); |
| 14 | +}); |
| 15 | + |
| 16 | +it('should handle writes at various positions',()=>{ |
| 17 | +constnode=newNode(1,0o666); |
| 18 | +node.write(bufferFrom('hello'),0,5,0); |
| 19 | +expect(node.getSize()).toBe(5); |
| 20 | +expect(node.getBuffer().toString()).toBe('hello'); |
| 21 | +node.write(bufferFrom('world'),0,5,5); |
| 22 | +expect(node.getSize()).toBe(10); |
| 23 | +expect(node.getBuffer().toString()).toBe('helloworld'); |
| 24 | +}); |
| 25 | + |
| 26 | +it('overwrites and extends file size',()=>{ |
| 27 | +constnode=newNode(1,0o666); |
| 28 | +node.write(bufferFrom('hello'),0,5,0); |
| 29 | +expect(node.getSize()).toBe(5); |
| 30 | +expect(node.getBuffer().toString()).toBe('hello'); |
| 31 | +node.write(bufferFrom('0 world'),0,7,4); |
| 32 | +expect(node.getSize()).toBe('hell0 world'.length); |
| 33 | +expect(node.getBuffer().toString()).toBe('hell0 world'); |
| 34 | +}); |
| 35 | + |
| 36 | +it('should handle overwriting existing data',()=>{ |
| 37 | +constnode=newNode(1,0o666); |
| 38 | +node.write(bufferFrom('hello world'),0,11,0); |
| 39 | +expect(node.getSize()).toBe(11); |
| 40 | +node.write(bufferFrom('HELLO'),0,5,0); |
| 41 | +expect(node.getSize()).toBe(11); |
| 42 | +expect(node.getBuffer().toString()).toBe('HELLO world'); |
| 43 | +}); |
| 44 | + |
| 45 | +it('should handle truncate to smaller size',()=>{ |
| 46 | +constnode=newNode(1,0o666); |
| 47 | +node.write(bufferFrom('hello world'),0,11,0); |
| 48 | +expect(node.getSize()).toBe(11); |
| 49 | +node.truncate(5); |
| 50 | +expect(node.getSize()).toBe(5); |
| 51 | +expect(node.getBuffer().toString()).toBe('hello'); |
| 52 | +}); |
| 53 | + |
| 54 | +it('should handle truncate to larger size with zero-fill',()=>{ |
| 55 | +constnode=newNode(1,0o666); |
| 56 | +node.write(bufferFrom('hello'),0,5,0); |
| 57 | +expect(node.getSize()).toBe(5); |
| 58 | +node.truncate(10); |
| 59 | +expect(node.getSize()).toBe(10); |
| 60 | +constresult=node.getBuffer(); |
| 61 | +expect(result.toString('utf8',0,5)).toBe('hello'); |
| 62 | +expect(result[5]).toBe(0); |
| 63 | +expect(result[9]).toBe(0); |
| 64 | +}); |
| 65 | + |
| 66 | +it('should handle truncate to zero',()=>{ |
| 67 | +constnode=newNode(1,0o666); |
| 68 | +node.write(bufferFrom('hello'),0,5,0); |
| 69 | +expect(node.getSize()).toBe(5); |
| 70 | +node.truncate(0); |
| 71 | +expect(node.getSize()).toBe(0); |
| 72 | +expect(node.getBuffer().length).toBe(0); |
| 73 | +}); |
| 74 | + |
| 75 | +it('should handle read from various positions',()=>{ |
| 76 | +constnode=newNode(1,0o666); |
| 77 | +node.write(bufferFrom('hello world'),0,11,0); |
| 78 | +constbuf1=Buffer.alloc(5); |
| 79 | +constread1=node.read(buf1,0,5,0); |
| 80 | +expect(read1).toBe(5); |
| 81 | +expect(buf1.toString()).toBe('hello'); |
| 82 | +constbuf2=Buffer.alloc(5); |
| 83 | +constread2=node.read(buf2,0,5,6); |
| 84 | +expect(read2).toBe(5); |
| 85 | +expect(buf2.toString()).toBe('world'); |
| 86 | +}); |
| 87 | + |
| 88 | +it('should handle read past end of file',()=>{ |
| 89 | +constnode=newNode(1,0o666); |
| 90 | +node.write(bufferFrom('hello'),0,5,0); |
| 91 | +constbuf=Buffer.alloc(10); |
| 92 | +constread=node.read(buf,0,10,0); |
| 93 | +expect(read).toBe(5); |
| 94 | +expect(buf.toString('utf8',0,5)).toBe('hello'); |
| 95 | +}); |
| 96 | + |
| 97 | +it('should handle setBuffer and getBuffer',()=>{ |
| 98 | +constnode=newNode(1,0o666); |
| 99 | +consttestBuf=bufferFrom('test data'); |
| 100 | +node.setBuffer(testBuf); |
| 101 | +expect(node.getSize()).toBe(9); |
| 102 | +expect(node.getBuffer().toString()).toBe('test data'); |
| 103 | +}); |
| 104 | + |
| 105 | +it('should handle setString and getString',()=>{ |
| 106 | +constnode=newNode(1,0o666); |
| 107 | +node.setString('test string'); |
| 108 | +expect(node.getSize()).toBe(11); |
| 109 | +expect(node.getString()).toBe('test string'); |
| 110 | +}); |
| 111 | + |
| 112 | +it('should return zero size for empty node',()=>{ |
| 113 | +constnode=newNode(1,0o666); |
| 114 | +expect(node.getSize()).toBe(0); |
| 115 | +}); |
| 116 | + |
| 117 | +it('should handle multiple appends efficiently',()=>{ |
| 118 | +constnode=newNode(1,0o666); |
| 119 | +constchunk=bufferFrom('chunk'); |
| 120 | +for(leti=0;i<20;i++)node.write(chunk,0,5,i*5); |
| 121 | +expect(node.getSize()).toBe(100); |
| 122 | +expect(node.getBuffer().toString()).toBe('chunk'.repeat(20)); |
| 123 | +}); |
| 124 | + |
| 125 | +it('should handle write at position beyond current size with zero-fill',()=>{ |
| 126 | +constnode=newNode(1,0o666); |
| 127 | +node.write(bufferFrom('hello'),0,5,10); |
| 128 | +expect(node.getSize()).toBe(15); |
| 129 | +constresult=node.getBuffer(); |
| 130 | +// First 10 bytes should be zero-filled |
| 131 | +for(leti=0;i<10;i++){ |
| 132 | +expect(result[i]).toBe(0); |
| 133 | +} |
| 134 | +expect(result.toString('utf8',10,15)).toBe('hello'); |
| 135 | +}); |
| 136 | +}); |
| 137 | +}); |