Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/lib/hostname.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ export function sanitizeHostname(raw: string): string {
}

export function buildProjectHostname(projectSlug: string, subdomain?: string): string {
const machine = getMachineHostname();
const base = `${projectSlug}.${machine}.lvh.me`;
// lvh.me always resolves to 127.0.0.1, so the project slug alone is enough
// for a unique, easy-to-type local hostname — no machine segment needed.
const base = `${projectSlug}.lvh.me`;
if (subdomain) {
return `${subdomain}.${base}`;
}
Expand Down
20 changes: 5 additions & 15 deletions tests/lib/hostname.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import os from 'node:os';
import {describe, expect, it, vi} from 'vitest';
import {describe, expect, it} from 'vitest';
import {buildProjectHostname, sanitizeHostname} from '../../src/lib/hostname.js';

vi.mock('node:os', () => ({
default: {hostname: vi.fn()},
hostname: vi.fn(),
}));

const mockHostname = vi.mocked(os.hostname);

describe('sanitizeHostname', () => {
it('lowercases and strips .local suffix', () => {
expect(sanitizeHostname('Rasmus-MacBook.local')).toBe('rasmus-macbook');
Expand All @@ -28,15 +20,13 @@ describe('sanitizeHostname', () => {
});

describe('buildProjectHostname', () => {
it('combines project slug with machine hostname', () => {
mockHostname.mockReturnValue('rasmus-macbook.local');
expect(buildProjectHostname('my-theme')).toBe('my-theme.rasmus-macbook.lvh.me');
it('uses the project slug directly under lvh.me', () => {
expect(buildProjectHostname('my-theme')).toBe('my-theme.lvh.me');
});

it('builds phpmyadmin subdomain', () => {
mockHostname.mockReturnValue('rasmus-macbook.local');
it('builds a phpmyadmin subdomain', () => {
expect(buildProjectHostname('my-theme', 'phpmyadmin')).toBe(
'phpmyadmin.my-theme.rasmus-macbook.lvh.me',
'phpmyadmin.my-theme.lvh.me',
);
});
});
13 changes: 3 additions & 10 deletions tests/lib/status.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@ import {
} from '../../src/lib/status.js';
import type {ProjectConfig} from '../../src/types/config.js';

// buildProjectHostname embeds the machine hostname; pin it so URL assertions
// are deterministic regardless of the host the test runs on.
vi.mock('node:os', async (importOriginal) => {
const actual = await importOriginal<typeof import('node:os')>();
return {...actual, default: {...actual, hostname: () => 'testbox'}};
});

const PROJECT: ProjectConfig = {
project_id: 'proj-123',
name: 'acme',
Expand Down Expand Up @@ -78,9 +71,9 @@ describe('getProjectStatus', () => {
{name: 'proj-123-phpmyadmin', running: true},
]);
expect(status.urls).toEqual({
site: 'http://acme.testbox.lvh.me:5477',
admin: 'http://acme.testbox.lvh.me:5477/wp-admin',
phpmyadmin: 'http://phpmyadmin.acme.testbox.lvh.me:5477',
site: 'http://acme.lvh.me:5477',
admin: 'http://acme.lvh.me:5477/wp-admin',
phpmyadmin: 'http://phpmyadmin.acme.lvh.me:5477',
});
});

Expand Down
Loading