|
| 1 | +package com.github.continuedev.continueintellijextension.unit |
| 2 | + |
| 3 | +import com.github.continuedev.continueintellijextension.browser.ContinueBrowser |
| 4 | +import com.github.continuedev.continueintellijextension.browser.ContinueBrowser.Companion.buildChunkScripts |
| 5 | +import junit.framework.TestCase |
| 6 | +import java.util.Base64 |
| 7 | + |
| 8 | +class ContinueBrowserChunkTest : TestCase() { |
| 9 | + |
| 10 | + fun `test small message produces single chunk`() { |
| 11 | + val json = """{"messageType":"test","data":"hello"}""" |
| 12 | + val scripts = buildChunkScripts(json, "buf1") |
| 13 | + |
| 14 | + assertEquals(1, scripts.chunks.size) |
| 15 | + assertReassemblesTo(json, scripts.chunks, "buf1") |
| 16 | + } |
| 17 | + |
| 18 | + fun `test message splits into expected number of chunks`() { |
| 19 | + val json = """{"data":"${"x".repeat(2_000_000)}"}""" |
| 20 | + val chunkSize = ContinueBrowser.CHUNK_SIZE |
| 21 | + val encoded = Base64.getEncoder().encodeToString(json.toByteArray(Charsets.UTF_8)) |
| 22 | + val expectedChunks = (encoded.length + chunkSize - 1) / chunkSize |
| 23 | + |
| 24 | + val scripts = buildChunkScripts(json, "buf2") |
| 25 | + |
| 26 | + assertEquals(expectedChunks, scripts.chunks.size) |
| 27 | + assertReassemblesTo(json, scripts.chunks, "buf2") |
| 28 | + } |
| 29 | + |
| 30 | + fun `test custom chunk size`() { |
| 31 | + val json = """{"data":"${"a".repeat(100)}"}""" |
| 32 | + val scripts = buildChunkScripts(json, "buf3", chunkSize = 32) |
| 33 | + |
| 34 | + assertTrue(scripts.chunks.size > 1) |
| 35 | + assertReassemblesTo(json, scripts.chunks, "buf3") |
| 36 | + } |
| 37 | + |
| 38 | + fun `test init script creates array buffer`() { |
| 39 | + val scripts = buildChunkScripts("{}", "myId") |
| 40 | + assertEquals("""window.__cc=window.__cc||{};window.__cc["myId"]=[];""", scripts.init) |
| 41 | + } |
| 42 | + |
| 43 | + fun `test finalize script joins and decodes`() { |
| 44 | + val scripts = buildChunkScripts("{}", "myId") |
| 45 | + assertTrue(scripts.finalize.contains("""window.__cc["myId"].join("")""")) |
| 46 | + assertTrue(scripts.finalize.contains("atob")) |
| 47 | + assertTrue(scripts.finalize.contains("TextDecoder")) |
| 48 | + assertTrue(scripts.finalize.contains("JSON.parse")) |
| 49 | + assertTrue(scripts.finalize.contains("""delete window.__cc["myId"]""")) |
| 50 | + } |
| 51 | + |
| 52 | + fun `test cleanup script deletes buffer`() { |
| 53 | + val scripts = buildChunkScripts("{}", "myId") |
| 54 | + assertEquals("""delete window.__cc["myId"];""", scripts.cleanup) |
| 55 | + } |
| 56 | + |
| 57 | + fun `test special characters survive base64 round-trip`() { |
| 58 | + val json = """{"data":"héllo wörld 日本語 emoji: 🎉 quotes: \"nested\""}""" |
| 59 | + val scripts = buildChunkScripts(json, "buf4", chunkSize = 32) |
| 60 | + |
| 61 | + assertReassemblesTo(json, scripts.chunks, "buf4") |
| 62 | + } |
| 63 | + |
| 64 | + fun `test exact chunk boundary`() { |
| 65 | + // Create a JSON string whose Base64 encoding is exactly 2x chunkSize |
| 66 | + val chunkSize = 64 |
| 67 | + val target = chunkSize * 2 |
| 68 | + // Base64 output is ceil(input/3)*4, so we need input = target * 3 / 4 bytes |
| 69 | + val payloadSize = target * 3 / 4 |
| 70 | + val json = "x".repeat(payloadSize) |
| 71 | + val encoded = Base64.getEncoder().encodeToString(json.toByteArray(Charsets.UTF_8)) |
| 72 | + |
| 73 | + assertEquals(target, encoded.length) |
| 74 | + |
| 75 | + val scripts = buildChunkScripts(json, "buf5", chunkSize = chunkSize) |
| 76 | + assertEquals(2, scripts.chunks.size) |
| 77 | + assertReassemblesTo(json, scripts.chunks, "buf5") |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Simulates the JS-side reassembly: extract push payloads, join, base64-decode, |
| 82 | + * and verify the result matches the original JSON. |
| 83 | + */ |
| 84 | + private fun assertReassemblesTo(expectedJson: String, chunkScripts: List<String>, bufferId: String) { |
| 85 | + val pushPrefix = """window.__cc["$bufferId"].push("""" |
| 86 | + val pushSuffix = """");""" |
| 87 | + |
| 88 | + val reassembled = chunkScripts.joinToString("") { script -> |
| 89 | + assertTrue("Chunk should use array push", script.startsWith(pushPrefix)) |
| 90 | + assertTrue(script.endsWith(pushSuffix)) |
| 91 | + script.removePrefix(pushPrefix).removeSuffix(pushSuffix) |
| 92 | + } |
| 93 | + |
| 94 | + val decoded = String(Base64.getDecoder().decode(reassembled), Charsets.UTF_8) |
| 95 | + assertEquals(expectedJson, decoded) |
| 96 | + } |
| 97 | +} |
0 commit comments