Skip to content

High performance self-hosted photo and video management solution.

7.7 KB
Raw Blame
import { expect, Page } from '@playwright/test';
import { DateTime } from 'luxon';
import { TimelineAssetConfig } from 'src/ui/generators/timeline.js';

export const sleep = (ms: number) => {
  return new Promise((resolve) => setTimeout(resolve, ms));
};

export const padYearMonth = (yearMonth: string) => {
  const [year, month] = yearMonth.split('-', 2);
  return `${year}-${month.padStart(2, '0')}`;
};

export const poll = async <T>(
  page: Page,
  query: () => Promise<T>,
  callback?: (result: Awaited<T> | undefined) => boolean,
) => {
  let result;
  const timeout = Date.now() + 10_000;

  const terminate = callback || ((result: Awaited<T> | undefined) => !!result);
  while (!terminate(result) && Date.now() < timeout) {
    try {
      result = await query();
    } catch {
      // ignore
    }
    if (page.isClosed()) {
      return;
    }
    try {
      await page.waitForTimeout(50);
    } catch {
      return;
    }
  }
  if (!result) {
    // rerun to trigger error if any
    result = await query();
  }
  return result;
};

export const thumbnailUtils = {
  locator(page: Page) {
    return page.locator('[data-thumbnail-focus-container]');
  },
  withAssetId(page: Page, assetId: string) {
    return page.locator(`[data-thumbnail-focus-container][data-asset="${assetId}"]`);
  },
  selectButton(page: Page, assetId: string) {
    return page.locator(`[data-thumbnail-focus-container][data-asset="${assetId}"] button[role="checkbox"]`);
  },
  selectedAsset(page: Page) {
    return page.locator('[data-thumbnail-focus-container][data-selected]');
  },
  async clickAssetId(page: Page, assetId: string) {
    await thumbnailUtils.withAssetId(page, assetId).click();
  },
  async queryThumbnailInViewport(page: Page, collector: (assetId: string) => boolean) {
    const assetIds: string[] = [];
    // eslint-disable-next-line unicorn/no-this-outside-of-class
    for (const thumb of await this.locator(page).all()) {
      const box = await thumb.boundingBox();
      if (box) {
        const assetId = await thumb.evaluate((e) => e.dataset.asset);
        if (collector?.(assetId!)) {
          return [assetId!];
        }
        assetIds.push(assetId!);
      }
    }
    return assetIds;
  },
  async getFirstInViewport(page: Page) {
    return await poll(page, () => thumbnailUtils.queryThumbnailInViewport(page, () => true));
  },
  async getAllInViewport(page: Page, collector: (assetId: string) => boolean) {
    return await poll(page, () => thumbnailUtils.queryThumbnailInViewport(page, collector));
  },
  async expectThumbnailIsFavorite(page: Page, assetId: string) {
    await expect(thumbnailUtils.withAssetId(page, assetId).locator('[data-icon-favorite]')).toHaveCount(1);
  },
  async expectThumbnailIsNotFavorite(page: Page, assetId: string) {
    await expect(thumbnailUtils.withAssetId(page, assetId).locator('[data-icon-favorite]')).toHaveCount(0);
  },
  async expectThumbnailIsArchive(page: Page, assetId: string) {
    await expect(thumbnailUtils.withAssetId(page, assetId).locator('[data-icon-archive]')).toHaveCount(1);
  },
  async expectThumbnailIsNotArchive(page: Page, assetId: string) {
    await expect(thumbnailUtils.withAssetId(page, assetId).locator('[data-icon-archive]')).toHaveCount(0);
  },
  async expectSelectedDisabled(page: Page, assetId: string) {
    await expect(
      page.locator(`[data-thumbnail-focus-container][data-asset="${assetId}"][data-selected][data-disabled]`),
    ).toBeVisible();
  },
  async expectTimelineHasOnScreenAssets(page: Page) {
    const first = await thumbnailUtils.getFirstInViewport(page);
    if (page.isClosed()) {
      return;
    }
    expect(first).toBeTruthy();
  },
  async expectInViewport(page: Page, assetId: string) {
    const box = await poll(page, () => thumbnailUtils.withAssetId(page, assetId).boundingBox());
    if (page.isClosed()) {
      return;
    }
    expect(box).toBeTruthy();
  },
  async expectBottomIsTimelineBottom(page: Page, assetId: string) {
    const box = await thumbnailUtils.withAssetId(page, assetId).boundingBox();
    const gridBox = await timelineUtils.locator(page).boundingBox();
    if (page.isClosed()) {
      return;
    }
    expect(box!.y + box!.height).toBeCloseTo(gridBox!.y + gridBox!.height, 0);
  },
  async expectTopIsTimelineTop(page: Page, assetId: string) {
    const box = await thumbnailUtils.withAssetId(page, assetId).boundingBox();
    const gridBox = await timelineUtils.locator(page).boundingBox();
    if (page.isClosed()) {
      return;
    }
    expect(box!.y).toBeCloseTo(gridBox!.y, 0);
  },
};
export const timelineUtils = {
  locator(page: Page) {
    return page.locator('#asset-grid');
  },
  async waitForTimelineLoad(page: Page) {
    await expect(timelineUtils.locator(page)).toHaveCount(1);
    await expect(timelineUtils.locator(page)).toBeInViewport();
    await expect.poll(() => thumbnailUtils.locator(page).count()).toBeGreaterThan(0);
  },
  async getScrollTop(page: Page) {
    const queryTop = () =>
      page.evaluate(() => {
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore

        return document.querySelector('#asset-grid').scrollTop;
      });
    await expect.poll(queryTop).toBeGreaterThan(0);
    return await queryTop();
  },
};

export const assetViewerUtils = {
  locator(page: Page) {
    return page.locator('#immich-asset-viewer');
  },
  async waitForViewerLoad(page: Page, asset: TimelineAssetConfig) {
    await page
      .locator(
        `img[draggable="false"][src="/api/assets/${asset.id}/thumbnail?size=preview&c=${asset.thumbhash}&edited=true"]`,
      )
      .or(
        page.locator(`video[poster="/api/assets/${asset.id}/thumbnail?size=preview&c=${asset.thumbhash}&edited=true"]`),
      )
      .waitFor();
  },
  async expectActiveAssetToBe(page: Page, assetId: string) {
    const activeElement = () =>
      page.evaluate(() => {
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore
        // eslint-disable-next-line unicorn/no-optional-chaining-on-undeclared-variable
        return document.activeElement?.dataset?.asset;
      });
    await expect(poll(page, activeElement, (result) => result === assetId)).resolves.toBe(assetId);
  },
};
export const pageUtils = {
  async deepLinkPhotosPage(page: Page, assetId: string) {
    await page.goto(`/photos?at=${assetId}`);
    await timelineUtils.waitForTimelineLoad(page);
  },
  async openPhotosPage(page: Page) {
    await page.goto(`/photos`);
    await timelineUtils.waitForTimelineLoad(page);
  },
  async openFavorites(page: Page) {
    await page.goto(`/favorites`);
    await timelineUtils.waitForTimelineLoad(page);
  },
  async openAlbumPage(page: Page, albumId: string) {
    await page.goto(`/albums/${albumId}`);
    await timelineUtils.waitForTimelineLoad(page);
  },
  async openArchivePage(page: Page) {
    await page.goto(`/archive`);
    await timelineUtils.waitForTimelineLoad(page);
  },
  async deepLinkAlbumPage(page: Page, albumId: string, assetId: string) {
    await page.goto(`/albums/${albumId}?at=${assetId}`);
    await timelineUtils.waitForTimelineLoad(page);
  },
  async goToAsset(page: Page, assetDate: string) {
    await timelineUtils.locator(page).hover();
    const stringDate = DateTime.fromISO(assetDate).toFormat('MMddyyyy,hh:mm:ss.SSSa');
    await page.keyboard.press('g');
    await page.locator('#datetime').pressSequentially(stringDate);
    await page.getByText('Confirm').click();
  },
  async selectDay(page: Page, day: string) {
    const section = page.getByTitle(day).locator('xpath=ancestor::section[@data-group]');
    await section.hover();
    await section.locator('.w-8').click();
  },
  async pauseTestDebug() {
    console.log('NOTE: pausing test indefinitely for debug');
    await new Promise(() => void 0);
  },
};