File size: 2,715 Bytes
391c43e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { describe, it, expect } from 'vitest';
import {
  resolveDeploymentServing,
  replaceAssetPathsWithDeploymentPrefix,
} from '../deployment-paths';

const APP = 'https://inst-1.oswstudio.com';
const ID = 'abc123';

describe('resolveDeploymentServing (issue #14)', () => {
  it('serves at root for a custom domain regardless of proxy/slug', () => {
    const r = resolveDeploymentServing(
      { customDomain: 'sweets.com', slug: 'fine-bird-flame' },
      ID,
      { staticProxyEnabled: false, appUrl: APP }
    );
    expect(r.servedAtRoot).toBe(true);
    expect(r.baseUrl).toBe('https://sweets.com');
  });

  it('serves at the subdomain root when the static proxy is on and a slug exists', () => {
    const r = resolveDeploymentServing(
      { slug: 'fine-bird-flame' },
      ID,
      { staticProxyEnabled: true, appUrl: APP }
    );
    expect(r.servedAtRoot).toBe(true);
    expect(r.baseUrl).toBe('https://fine-bird-flame.inst-1.oswstudio.com');
  });

  it('serves under /deployments/{id}/ when a slug exists but the proxy is OFF (the #14 regression case)', () => {
    const r = resolveDeploymentServing(
      { slug: 'fine-bird-flame' },
      ID,
      { staticProxyEnabled: false, appUrl: APP }
    );
    expect(r.servedAtRoot).toBe(false);
    expect(r.baseUrl).toBe(`${APP}/deployments/${ID}`);
  });

  it('serves under /deployments/{id}/ when the proxy is on but there is no slug', () => {
    const r = resolveDeploymentServing(
      {},
      ID,
      { staticProxyEnabled: true, appUrl: APP }
    );
    expect(r.servedAtRoot).toBe(false);
    expect(r.baseUrl).toBe(`${APP}/deployments/${ID}`);
  });

  it('serves under /deployments/{id}/ with neither proxy nor slug', () => {
    const r = resolveDeploymentServing({}, ID, { staticProxyEnabled: false, appUrl: APP });
    expect(r.servedAtRoot).toBe(false);
    expect(r.baseUrl).toBe(`${APP}/deployments/${ID}`);
  });
});

describe('servedAtRoot flips asset path style (issue #14)', () => {
  const html = '<link href="/styles/style.css"><script src="/scripts/main.js"></script>';

  it('prefixes asset paths with /deployments/{id} when NOT served at root', () => {
    const out = replaceAssetPathsWithDeploymentPrefix(html, new Map(), [], ID, false);
    expect(out).toContain(`href="/deployments/${ID}/styles/style.css"`);
    expect(out).toContain(`src="/deployments/${ID}/scripts/main.js"`);
  });

  it('keeps asset paths root-relative when served at root', () => {
    const out = replaceAssetPathsWithDeploymentPrefix(html, new Map(), [], ID, true);
    expect(out).toContain('href="/styles/style.css"');
    expect(out).toContain('src="/scripts/main.js"');
    expect(out).not.toContain('/deployments/');
  });
});