|
| 1 | +/* |
| 2 | + Copyright (c) 2025, Oracle and/or its affiliates. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import { expect } from 'chai'; |
| 18 | +import { Buffer } from 'buffer'; |
| 19 | +import { NotebookCellOutputItem } from 'vscode'; |
| 20 | +import { MimeTypeHandler } from '../../../notebooks/mimeTypeHandler'; |
| 21 | +import { mimeTypes } from '../../../notebooks/constants'; |
| 22 | +import { IMimeBundle } from '../../../notebooks/types'; |
| 23 | +import { describe, it } from 'mocha'; |
| 24 | + |
| 25 | +describe('MimeTypeHandler', () => { |
| 26 | + |
| 27 | + function decodeData(item: NotebookCellOutputItem): string { |
| 28 | + const bytes: Uint8Array = (item as any).data; |
| 29 | + return new TextDecoder().decode(bytes); |
| 30 | + } |
| 31 | + |
| 32 | + describe('getters isText / isImage', () => { |
| 33 | + it('isText ⇢ true only for mimeTypes.TEXT', () => { |
| 34 | + expect(new MimeTypeHandler(mimeTypes.TEXT).isText).to.be.true; |
| 35 | + expect(new MimeTypeHandler('image/jpeg').isText).to.be.false; |
| 36 | + }); |
| 37 | + |
| 38 | + it('isImage ⇢ true only for image/*', () => { |
| 39 | + expect(new MimeTypeHandler('image/png').isImage).to.be.true; |
| 40 | + expect(new MimeTypeHandler(mimeTypes.TEXT).isImage).to.be.false; |
| 41 | + }); |
| 42 | + |
| 43 | + }); |
| 44 | + |
| 45 | + describe('static toBytes()', () => { |
| 46 | + it('decodes a base64 string into the original Uint8Array', () => { |
| 47 | + const text = 'Hello, 世界!'; |
| 48 | + const b64 = Buffer.from(text).toString('base64'); |
| 49 | + const out = MimeTypeHandler.toBytes(b64); |
| 50 | + expect(out).to.be.instanceOf(Uint8Array); |
| 51 | + expect(Buffer.from(out).toString()).to.equal(text); |
| 52 | + }); |
| 53 | + |
| 54 | + it('returns the same Uint8Array when passed through', () => { |
| 55 | + const arr = new Uint8Array([1, 2, 3]); |
| 56 | + expect(MimeTypeHandler.toBytes(arr)).to.equal(arr); |
| 57 | + }); |
| 58 | + |
| 59 | + it('decodes empty string to empty Uint8Array', () => { |
| 60 | + const out = MimeTypeHandler.toBytes(''); |
| 61 | + expect(out).to.be.instanceOf(Uint8Array); |
| 62 | + expect(out.length).to.equal(0); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + describe('static toString()', () => { |
| 67 | + it('returns the same string if input is already string', () => { |
| 68 | + const s = 'just a test'; |
| 69 | + expect(MimeTypeHandler.toString(s)).to.equal(s); |
| 70 | + }); |
| 71 | + |
| 72 | + it('decodes a Uint8Array into a string via TextDecoder', () => { |
| 73 | + const text = '¡Hola!'; |
| 74 | + const encoder = new TextEncoder(); |
| 75 | + const arr = encoder.encode(text); |
| 76 | + expect(MimeTypeHandler.toString(arr)).to.equal(text); |
| 77 | + }); |
| 78 | + |
| 79 | + it('decodes empty Uint8Array to empty string', () => { |
| 80 | + const arr = new Uint8Array([]); |
| 81 | + expect(MimeTypeHandler.toString(arr)).to.equal(''); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe('makeOutputItem()', () => { |
| 86 | + it('for TEXT builds a NotebookCellOutputItem containing the UTF-8 bytes of the string', () => { |
| 87 | + const handler = new MimeTypeHandler(mimeTypes.TEXT); |
| 88 | + const item = handler.makeOutputItem('plain text'); |
| 89 | + expect(item).to.be.instanceOf(NotebookCellOutputItem); |
| 90 | + expect((item as any).mime).to.equal(mimeTypes.TEXT); |
| 91 | + expect(decodeData(item)).to.equal('plain text'); |
| 92 | + }); |
| 93 | + |
| 94 | + it('handles empty text payload correctly', () => { |
| 95 | + const handler = new MimeTypeHandler(mimeTypes.TEXT); |
| 96 | + const item = handler.makeOutputItem(''); |
| 97 | + expect(decodeData(item)).to.equal(''); |
| 98 | + expect((item as any).data.length).to.equal(0); |
| 99 | + }); |
| 100 | + |
| 101 | + it('for image/* with a base64 string decodes back to the original bytes', () => { |
| 102 | + const raw = new Uint8Array([10, 20, 30]); |
| 103 | + const b64 = Buffer.from(raw).toString('base64'); |
| 104 | + const handler = new MimeTypeHandler('image/png'); |
| 105 | + const item = handler.makeOutputItem(b64); |
| 106 | + expect((item as any).mime).to.equal('image/png'); |
| 107 | + const got = (item as any).data as Uint8Array; |
| 108 | + expect(Array.from(got)).to.deep.equal(Array.from(raw)); |
| 109 | + }); |
| 110 | + |
| 111 | + it('for image/* with a Uint8Array leaves the bytes untouched', () => { |
| 112 | + const raw = new Uint8Array([5, 6, 7]); |
| 113 | + const handler = new MimeTypeHandler('image/gif'); |
| 114 | + const item = handler.makeOutputItem(raw); |
| 115 | + expect((item as any).mime).to.equal('image/gif'); |
| 116 | + expect((item as any).data).to.equal(raw); |
| 117 | + }); |
| 118 | + |
| 119 | + it('unknown mime routes through text branch', () => { |
| 120 | + const handler = new MimeTypeHandler('application/xml'); |
| 121 | + const payload = '<note/>'; |
| 122 | + const item = handler.makeOutputItem(payload); |
| 123 | + expect((item as any).mime).to.equal('application/xml'); |
| 124 | + expect(decodeData(item)).to.equal(payload); |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + describe('static itemsFromBundle()', () => { |
| 129 | + it('filters to only text & image entries and decodes each correctly', () => { |
| 130 | + const rawImg = new Uint8Array([1, 2, 3]); |
| 131 | + const b64img = Buffer.from(rawImg).toString('base64'); |
| 132 | + const bundle: IMimeBundle = { |
| 133 | + [mimeTypes.TEXT]: 'foo', |
| 134 | + 'image/png': b64img, |
| 135 | + 'application/json': '{"x":1}', |
| 136 | + }; |
| 137 | + |
| 138 | + const items = MimeTypeHandler.itemsFromBundle(bundle); |
| 139 | + expect(items).to.have.length(2); |
| 140 | + |
| 141 | + const textItem = items.find(i => (i as any).mime === mimeTypes.TEXT)!; |
| 142 | + expect(decodeData(textItem)).to.equal('foo'); |
| 143 | + |
| 144 | + const imgItem = items.find(i => (i as any).mime === 'image/png')!; |
| 145 | + const gotBytes = (imgItem as any).data as Uint8Array; |
| 146 | + expect(Array.from(gotBytes)).to.deep.equal(Array.from(rawImg)); |
| 147 | + }); |
| 148 | + |
| 149 | + it('joins string-array values before creating the output item', () => { |
| 150 | + const bundle: IMimeBundle = { |
| 151 | + [mimeTypes.TEXT]: ['a', 'b', 'c'], |
| 152 | + }; |
| 153 | + const items = MimeTypeHandler.itemsFromBundle(bundle); |
| 154 | + expect(items).to.have.length(1); |
| 155 | + const only = items[0]; |
| 156 | + expect(decodeData(only)).to.equal('abc'); |
| 157 | + expect((only as any).mime).to.equal(mimeTypes.TEXT); |
| 158 | + }); |
| 159 | + |
| 160 | + it('joins base64-string-array for image payload', () => { |
| 161 | + // "SGVs" + "bG8=" => "SGVsbG8=" which decodes to "Hello" |
| 162 | + const segs = ['SGVs', 'bG8=']; |
| 163 | + const bundle: IMimeBundle = { |
| 164 | + 'image/png': segs, |
| 165 | + }; |
| 166 | + const items = MimeTypeHandler.itemsFromBundle(bundle); |
| 167 | + expect(items).to.have.length(1); |
| 168 | + const only = items[0]; |
| 169 | + expect((only as any).mime).to.equal('image/png'); |
| 170 | + expect(decodeData(only)).to.equal('Hello'); |
| 171 | + }); |
| 172 | + |
| 173 | + it('empty bundle yields no items', () => { |
| 174 | + const items = MimeTypeHandler.itemsFromBundle({}); |
| 175 | + expect(items).to.be.empty; |
| 176 | + }); |
| 177 | + |
| 178 | + it('unsupported mime types are dropped', () => { |
| 179 | + const bundle: IMimeBundle = { |
| 180 | + 'application/json': '[1,2,3]', |
| 181 | + 'video/mp4': 'abcd', |
| 182 | + }; |
| 183 | + expect(MimeTypeHandler.itemsFromBundle(bundle)).to.be.empty; |
| 184 | + }); |
| 185 | + }); |
| 186 | +}); |
0 commit comments