wvb 0.2.0-next.e459de3

Offline-first web resources delivery system for webview mounted frameworks/platforms
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
use http::Request;
use std::sync::Arc;
use wvb::BundleEntry;
use wvb::protocol::{BundleProtocol, Protocol};
use wvb::source::BundleSource;
use wvb::testing::*;
use wvb::updater::Updater;

#[tokio::test]
async fn manifest_persists_across_reload() {
  let mut system = MockSystem::new();
  system
    .source_mut()
    .add_builtin_bundle(MockBundle::new("app", "1.0.0").with_entry(
      "/index.html",
      BundleEntry::new(b"<h1>builtin</h1>", "text/html", None),
    ))
    .set_builtin_current_version("app", "1.0.0");
  system
    .remote_mut()
    .add_bundle(MockBundle::new("app", "2.0.0").with_entry(
      "/index.html",
      BundleEntry::new(b"<h1>remote</h1>", "text/html", None),
    ))
    .set_bundle_current_version("app", "2.0.0");

  let (builtin_dir, remote_dir) = system.source().dirs();
  let source = Arc::new(system.source().get_source());
  let remote = Arc::new(system.remote().get_remote());

  Updater::new(source.clone(), remote, None)
    .download("app", None)
    .await
    .unwrap();
  // Activate so current_version is persisted to the on-disk manifest.
  source.update_remote_version("app", "2.0.0").await.unwrap();

  // Simulate a restart with a fresh source over the same dirs.
  let reloaded = Arc::new(
    BundleSource::builder()
      .builtin_dir(builtin_dir)
      .remote_dir(remote_dir)
      .build(),
  );
  let version = reloaded.load_version("app").await.unwrap().unwrap();
  assert_eq!(
    version.version, "2.0.0",
    "downloaded version must survive a source reload"
  );

  let protocol = BundleProtocol::new(reloaded);
  let resp = protocol
    .handle(
      Request::builder()
        .uri("https://app.wvb/index.html")
        .method("GET")
        .body(vec![])
        .unwrap(),
    )
    .await
    .unwrap();
  assert_eq!(resp.status(), 200);
  assert_eq!(resp.body().as_ref(), b"<h1>remote</h1>");
}

// V1/V2 differ in length on purpose: equal LZ4 sizes would let a stale v1 descriptor read
// the v2 file with the right byte count and hide the bug.
#[tokio::test]
async fn descriptor_cache_invalidated_on_activation() {
  const V1: &[u8] = b"<h1>v1</h1>";
  const V2: &[u8] = b"<h1>version 2 - longer content ensures different LZ4 compressed size</h1>";

  let mut system = MockSystem::new();
  system
    .source_mut()
    .add_builtin_bundle(
      MockBundle::new("app", "1.0.0")
        .with_entry("/index.html", BundleEntry::new(V1, "text/html", None)),
    )
    .set_builtin_current_version("app", "1.0.0");
  system
    .remote_mut()
    .add_bundle(
      MockBundle::new("app", "2.0.0")
        .with_entry("/index.html", BundleEntry::new(V2, "text/html", None)),
    )
    .set_bundle_current_version("app", "2.0.0");

  let source = Arc::new(system.source().get_source());
  let remote = Arc::new(system.remote().get_remote());
  let protocol = BundleProtocol::new(source.clone());

  // Warm the descriptor cache with v1.
  let warm = protocol
    .handle(
      Request::builder()
        .uri("https://app.wvb/index.html")
        .method("GET")
        .body(vec![])
        .unwrap(),
    )
    .await
    .unwrap();
  assert_eq!(warm.body().as_ref(), V1);

  // Stage v2, then activate it.
  Updater::new(source.clone(), remote, None)
    .download("app", None)
    .await
    .unwrap();
  // Activation changes the active filepath; a stale v1 descriptor would mis-read the v2 file.
  source.update_remote_version("app", "2.0.0").await.unwrap();

  let resp = protocol
    .handle(
      Request::builder()
        .uri("https://app.wvb/index.html")
        .method("GET")
        .body(vec![])
        .unwrap(),
    )
    .await
    .unwrap();
  assert_eq!(
    resp.body().as_ref(),
    V2,
    "descriptor cache was not invalidated after activation"
  );
}

// A descriptor/file version mismatch must hard-error, not silently return wrong bytes.
#[tokio::test]
async fn descriptor_file_mismatch_errors() {
  use tokio::fs::File;
  use wvb::source::BundleSource;

  const V1: &[u8] = b"<h1>v1</h1>";
  const V2: &[u8] = b"<h1>version 2 - longer content to guarantee different LZ4 size</h1>";

  let mut system = MockSystem::new();
  system
    .source_mut()
    .add_builtin_bundle(
      MockBundle::new("app", "1.0.0")
        .with_entry("/index.html", BundleEntry::new(V1, "text/html", None)),
    )
    .set_builtin_current_version("app", "1.0.0");

  let (builtin_dir, remote_dir) = system.source().dirs();

  // Write the v2 file directly so the manifest still points at v1 (present but inactive).
  let v2_bundle = MockBundle::new("app", "2.0.0")
    .with_entry("/index.html", BundleEntry::new(V2, "text/html", None));
  let v2_dir = remote_dir.join("app");
  std::fs::create_dir_all(&v2_dir).unwrap();
  let v2_path = v2_dir.join("app_2.0.0.wvb");
  std::fs::write(&v2_path, v2_bundle.bundle_data()).unwrap();

  let source_v1 = BundleSource::builder()
    .builtin_dir(&builtin_dir)
    .remote_dir(&remote_dir)
    .build();

  // The v1 descriptor a task holds after a cache hit.
  let v1_descriptor = source_v1.fetch_descriptor("app").await.unwrap();

  // The v2 file reader() would return after the version bumps but before the cache clears.
  let v2_reader = File::open(&v2_path).await.unwrap();

  let result = v1_descriptor.async_get_data(v2_reader, "/index.html").await;
  assert!(
    result.is_err(),
    "using a v1 descriptor to read a v2 file must produce an error, not silently return wrong data"
  );
}

// A manifest entry whose .wvb file is gone must yield BundleNotFound, not a panic.
#[tokio::test]
async fn missing_file_returns_not_found() {
  let mut system = MockSystem::new();
  system
    .source_mut()
    .add_builtin_bundle(
      MockBundle::new("app", "1.0.0")
        .with_entry("/index.html", BundleEntry::new(b"hello", "text/html", None)),
    )
    .set_builtin_current_version("app", "1.0.0");

  let (builtin_dir, remote_dir) = system.source().dirs();

  // Delete the .wvb file but leave the manifest intact.
  let wvb_path = builtin_dir.join("app").join("app_1.0.0.wvb");
  std::fs::remove_file(&wvb_path).unwrap();

  let source = Arc::new(
    BundleSource::builder()
      .builtin_dir(&builtin_dir)
      .remote_dir(&remote_dir)
      .build(),
  );
  let protocol = BundleProtocol::new(source.clone());

  let err = protocol
    .handle(
      Request::builder()
        .uri("https://app.wvb/index.html")
        .method("GET")
        .body(vec![])
        .unwrap(),
    )
    .await
    .unwrap_err();
  assert!(
    matches!(err, wvb::Error::BundleNotFound),
    "expected BundleNotFound, got: {err}"
  );
}

// Invalid manifest JSON (e.g. truncated by a crash) must surface an error, not parse silently.
#[tokio::test]
async fn corrupt_manifest_errors() {
  let temp = TempDir::new();
  let builtin_dir = temp.dir().join("builtin");
  let remote_dir = temp.dir().join("remote");
  std::fs::create_dir_all(&builtin_dir).unwrap();
  std::fs::create_dir_all(&remote_dir).unwrap();

  std::fs::write(
    builtin_dir.join("manifest.json"),
    b"{ this is not valid json ",
  )
  .unwrap();

  let source = BundleSource::builder()
    .builtin_dir(&builtin_dir)
    .remote_dir(&remote_dir)
    .build();

  let result = source.load_version("app").await;
  assert!(
    result.is_err(),
    "corrupted manifest must return an error, not silently produce None"
  );
}

// A .wvb file of random bytes (e.g. a partial write) must fail to parse, not 200 with garbage.
#[tokio::test]
async fn corrupt_bundle_errors() {
  let mut system = MockSystem::new();
  system
    .source_mut()
    .add_builtin_bundle(MockBundle::new("app", "1.0.0").with_entry(
      "/index.html",
      BundleEntry::new(b"<h1>hello</h1>", "text/html", None),
    ))
    .set_builtin_current_version("app", "1.0.0");

  let (builtin_dir, remote_dir) = system.source().dirs();

  let wvb_path = builtin_dir.join("app").join("app_1.0.0.wvb");
  std::fs::write(&wvb_path, b"this is not a valid wvb file at all !!!").unwrap();

  let source = Arc::new(
    BundleSource::builder()
      .builtin_dir(&builtin_dir)
      .remote_dir(&remote_dir)
      .build(),
  );
  let protocol = BundleProtocol::new(source.clone());

  let result = protocol
    .handle(
      Request::builder()
        .uri("https://app.wvb/index.html")
        .method("GET")
        .body(vec![])
        .unwrap(),
    )
    .await;
  assert!(
    result.is_err(),
    "corrupted bundle file must return an error, not a 200 with garbage"
  );
}

// A zero-byte .wvb file must be rejected.
#[tokio::test]
async fn empty_bundle_errors() {
  let mut system = MockSystem::new();
  system
    .source_mut()
    .add_builtin_bundle(
      MockBundle::new("app", "1.0.0")
        .with_entry("/index.html", BundleEntry::new(b"hello", "text/html", None)),
    )
    .set_builtin_current_version("app", "1.0.0");

  let (builtin_dir, remote_dir) = system.source().dirs();
  std::fs::write(builtin_dir.join("app").join("app_1.0.0.wvb"), b"").unwrap();

  let source = Arc::new(
    BundleSource::builder()
      .builtin_dir(&builtin_dir)
      .remote_dir(&remote_dir)
      .build(),
  );
  let protocol = BundleProtocol::new(source.clone());

  let result = protocol
    .handle(
      Request::builder()
        .uri("https://app.wvb/index.html")
        .method("GET")
        .body(vec![])
        .unwrap(),
    )
    .await;
  assert!(result.is_err(), "empty .wvb file must return an error");
}

// A truncated .wvb file (interrupted download / power loss) must be rejected without panic.
#[tokio::test]
async fn truncated_bundle_errors() {
  let mut system = MockSystem::new();
  let bundle = MockBundle::new("app", "1.0.0").with_entry(
    "/index.html",
    BundleEntry::new(b"<h1>content</h1>", "text/html", None),
  );
  system
    .source_mut()
    .add_builtin_bundle(bundle.clone())
    .set_builtin_current_version("app", "1.0.0");

  let (builtin_dir, remote_dir) = system.source().dirs();

  let valid_bytes = bundle.bundle_data();
  let wvb_path = builtin_dir.join("app").join("app_1.0.0.wvb");
  std::fs::write(&wvb_path, &valid_bytes[..10.min(valid_bytes.len())]).unwrap();

  let source = Arc::new(
    BundleSource::builder()
      .builtin_dir(&builtin_dir)
      .remote_dir(&remote_dir)
      .build(),
  );
  let protocol = BundleProtocol::new(source.clone());

  let result = protocol
    .handle(
      Request::builder()
        .uri("https://app.wvb/index.html")
        .method("GET")
        .body(vec![])
        .unwrap(),
    )
    .await;
  assert!(result.is_err(), "truncated .wvb file must return an error");
}

// A .wvb file with no manifest entry (crash before save) must be invisible to load_version.
#[tokio::test]
async fn orphan_bundle_not_visible() {
  let temp = TempDir::new();
  let builtin_dir = temp.dir().join("builtin");
  let remote_dir = temp.dir().join("remote").join("app");
  std::fs::create_dir_all(&builtin_dir).unwrap();
  std::fs::create_dir_all(&remote_dir).unwrap();

  // Drop a .wvb file directly, with no manifest entry.
  let bundle = MockBundle::new("app", "2.0.0").with_entry(
    "/index.html",
    BundleEntry::new(b"orphan", "text/html", None),
  );
  std::fs::write(remote_dir.join("app_2.0.0.wvb"), bundle.bundle_data()).unwrap();

  let source = BundleSource::builder()
    .builtin_dir(&builtin_dir)
    .remote_dir(temp.dir().join("remote"))
    .build();

  let version = source.load_version("app").await.unwrap();
  assert!(
    version.is_none(),
    "a .wvb file without a manifest entry must not be visible to load_version"
  );
}

// Bytes modified after creation must fail to parse (fixed magic number + internal checksums).
#[tokio::test]
async fn bit_flipped_bundle_errors() {
  let mut system = MockSystem::new();
  let bundle = MockBundle::new("app", "1.0.0").with_entry(
    "/index.html",
    BundleEntry::new(b"<h1>original</h1>", "text/html", None),
  );
  system
    .source_mut()
    .add_builtin_bundle(bundle.clone())
    .set_builtin_current_version("app", "1.0.0");

  let (builtin_dir, remote_dir) = system.source().dirs();

  let wvb_path = builtin_dir.join("app").join("app_1.0.0.wvb");
  let mut bytes = std::fs::read(&wvb_path).unwrap();
  let mid = bytes.len() / 2;
  bytes[mid] ^= 0xFF;
  std::fs::write(&wvb_path, &bytes).unwrap();

  let source = Arc::new(
    BundleSource::builder()
      .builtin_dir(&builtin_dir)
      .remote_dir(&remote_dir)
      .build(),
  );
  let protocol = BundleProtocol::new(source.clone());

  let result = protocol
    .handle(
      Request::builder()
        .uri("https://app.wvb/index.html")
        .method("GET")
        .body(vec![])
        .unwrap(),
    )
    .await;
  assert!(
    result.is_err(),
    "a bit-flipped bundle must be rejected, not silently served"
  );
}