|
| 1 | +import Result "mo:base/Result"; |
| 2 | +import Blob "mo:base/Blob"; |
| 3 | +import { test; suite } "mo:test/async"; |
| 4 | + |
| 5 | +import Storage "../backend/storage/storage-canister"; |
| 6 | + |
| 7 | +var storage = await Storage.Storage(); |
| 8 | + |
| 9 | +// PRECONDITION: startUpload is idempotent — resets active upload (prevents stale data) |
| 10 | +await suite( |
| 11 | + "PRECONDITION: startUpload reset + finishUploads rejects empty chunks", |
| 12 | + func() : async () { |
| 13 | + let fileId = "core@2.3.0/src/Runtime.mo"; |
| 14 | + let realData = Blob.fromArray([1, 2, 3, 4, 5, 6, 7, 8]); |
| 15 | + |
| 16 | + await test( |
| 17 | + "start upload and fill chunk", |
| 18 | + func() : async () { |
| 19 | + assert Result.isOk(await storage.startUpload({ id = fileId; path = "src/Runtime.mo"; chunkCount = 1; owners = [] })); |
| 20 | + assert Result.isOk(await storage.uploadChunk(fileId, 0, realData)); |
| 21 | + }, |
| 22 | + ); |
| 23 | + |
| 24 | + await test( |
| 25 | + "second startUpload resets the active upload (idempotent)", |
| 26 | + func() : async () { |
| 27 | + assert Result.isOk(await storage.startUpload({ id = fileId; path = "src/Runtime.mo"; chunkCount = 1; owners = [] })); |
| 28 | + }, |
| 29 | + ); |
| 30 | + |
| 31 | + await test( |
| 32 | + "finishUploads rejects because reset cleared the chunk data", |
| 33 | + func() : async () { |
| 34 | + assert Result.isErr(await storage.finishUploads([fileId])); |
| 35 | + }, |
| 36 | + ); |
| 37 | + }, |
| 38 | +); |
| 39 | + |
| 40 | +// PRECONDITION: idempotent startUpload allows a clean retry to succeed |
| 41 | +await suite( |
| 42 | + "PRECONDITION: retry after reset succeeds with fresh data", |
| 43 | + func() : async () { |
| 44 | + storage := await Storage.Storage(); |
| 45 | + let fileId = "core@2.3.0/src/Runtime.mo"; |
| 46 | + let staleData = Blob.fromArray([1, 2, 3]); |
| 47 | + let freshData = Blob.fromArray([10, 20, 30, 40, 50]); |
| 48 | + |
| 49 | + await test( |
| 50 | + "stale session: start and upload", |
| 51 | + func() : async () { |
| 52 | + assert Result.isOk(await storage.startUpload({ id = fileId; path = "src/Runtime.mo"; chunkCount = 1; owners = [] })); |
| 53 | + assert Result.isOk(await storage.uploadChunk(fileId, 0, staleData)); |
| 54 | + }, |
| 55 | + ); |
| 56 | + |
| 57 | + await test( |
| 58 | + "retry session: startUpload resets, then upload fresh data", |
| 59 | + func() : async () { |
| 60 | + assert Result.isOk(await storage.startUpload({ id = fileId; path = "src/Runtime.mo"; chunkCount = 1; owners = [] })); |
| 61 | + assert Result.isOk(await storage.uploadChunk(fileId, 0, freshData)); |
| 62 | + }, |
| 63 | + ); |
| 64 | + |
| 65 | + await test( |
| 66 | + "finishUploads succeeds with fresh data", |
| 67 | + func() : async () { |
| 68 | + assert Result.isOk(await storage.finishUploads([fileId])); |
| 69 | + }, |
| 70 | + ); |
| 71 | + |
| 72 | + await test( |
| 73 | + "downloaded file has the fresh data", |
| 74 | + func() : async () { |
| 75 | + let chunkRes = await storage.downloadChunk(fileId, 0); |
| 76 | + switch (chunkRes) { |
| 77 | + case (#ok(chunk)) { |
| 78 | + assert chunk == freshData; |
| 79 | + }; |
| 80 | + case (#err(_)) { |
| 81 | + assert false; |
| 82 | + }; |
| 83 | + }; |
| 84 | + }, |
| 85 | + ); |
| 86 | + }, |
| 87 | +); |
| 88 | + |
| 89 | +// FIX VERIFICATION: finishUploads rejects files with empty chunks |
| 90 | +await suite( |
| 91 | + "FIX: finishUploads rejects empty chunks", |
| 92 | + func() : async () { |
| 93 | + storage := await Storage.Storage(); |
| 94 | + let fileId = "pkg@1.0.0/src/Lib.mo"; |
| 95 | + |
| 96 | + await test( |
| 97 | + "start upload with chunkCount=2 but upload only chunk 0", |
| 98 | + func() : async () { |
| 99 | + assert Result.isOk(await storage.startUpload({ id = fileId; path = "src/Lib.mo"; chunkCount = 2; owners = [] })); |
| 100 | + assert Result.isOk(await storage.uploadChunk(fileId, 0, Blob.fromArray([10, 20, 30]))); |
| 101 | + }, |
| 102 | + ); |
| 103 | + |
| 104 | + await test( |
| 105 | + "finishUploads is rejected because chunk 1 was never uploaded", |
| 106 | + func() : async () { |
| 107 | + let res = await storage.finishUploads([fileId]); |
| 108 | + assert Result.isErr(res); |
| 109 | + }, |
| 110 | + ); |
| 111 | + }, |
| 112 | +); |
| 113 | + |
| 114 | +// REGRESSION: normal upload flow still works |
| 115 | +await suite( |
| 116 | + "REGRESSION: normal upload flow", |
| 117 | + func() : async () { |
| 118 | + storage := await Storage.Storage(); |
| 119 | + let fileId = "pkg@2.0.0/src/Main.mo"; |
| 120 | + let data1 = Blob.fromArray([10, 20, 30, 40, 50]); |
| 121 | + let data2 = Blob.fromArray([60, 70, 80]); |
| 122 | + |
| 123 | + await test( |
| 124 | + "upload file with 2 chunks", |
| 125 | + func() : async () { |
| 126 | + assert Result.isOk(await storage.startUpload({ id = fileId; path = "src/Main.mo"; chunkCount = 2; owners = [] })); |
| 127 | + assert Result.isOk(await storage.uploadChunk(fileId, 0, data1)); |
| 128 | + assert Result.isOk(await storage.uploadChunk(fileId, 1, data2)); |
| 129 | + }, |
| 130 | + ); |
| 131 | + |
| 132 | + await test( |
| 133 | + "finishUploads succeeds when all chunks are present", |
| 134 | + func() : async () { |
| 135 | + assert Result.isOk(await storage.finishUploads([fileId])); |
| 136 | + }, |
| 137 | + ); |
| 138 | + |
| 139 | + await test( |
| 140 | + "downloaded chunks have correct data", |
| 141 | + func() : async () { |
| 142 | + let c0 = await storage.downloadChunk(fileId, 0); |
| 143 | + switch (c0) { |
| 144 | + case (#ok(chunk)) { assert chunk == data1 }; |
| 145 | + case (#err(_)) { assert false }; |
| 146 | + }; |
| 147 | + let c1 = await storage.downloadChunk(fileId, 1); |
| 148 | + switch (c1) { |
| 149 | + case (#ok(chunk)) { assert chunk == data2 }; |
| 150 | + case (#err(_)) { assert false }; |
| 151 | + }; |
| 152 | + }, |
| 153 | + ); |
| 154 | + }, |
| 155 | +); |
| 156 | + |
| 157 | +// REGRESSION: empty files (chunkCount=0) still work |
| 158 | +await suite( |
| 159 | + "REGRESSION: empty files (chunkCount=0) are allowed", |
| 160 | + func() : async () { |
| 161 | + storage := await Storage.Storage(); |
| 162 | + let fileId = "pkg@3.0.0/src/Empty.mo"; |
| 163 | + |
| 164 | + await test( |
| 165 | + "start upload with chunkCount=0", |
| 166 | + func() : async () { |
| 167 | + assert Result.isOk(await storage.startUpload({ id = fileId; path = "src/Empty.mo"; chunkCount = 0; owners = [] })); |
| 168 | + }, |
| 169 | + ); |
| 170 | + |
| 171 | + await test( |
| 172 | + "finishUploads succeeds for empty files", |
| 173 | + func() : async () { |
| 174 | + assert Result.isOk(await storage.finishUploads([fileId])); |
| 175 | + }, |
| 176 | + ); |
| 177 | + |
| 178 | + await test( |
| 179 | + "file meta reports 0 chunks", |
| 180 | + func() : async () { |
| 181 | + let res = await storage.getFileMeta(fileId); |
| 182 | + switch (res) { |
| 183 | + case (#ok(meta)) { assert meta.chunkCount == 0 }; |
| 184 | + case (#err(_)) { assert false }; |
| 185 | + }; |
| 186 | + }, |
| 187 | + ); |
| 188 | + }, |
| 189 | +); |
0 commit comments