```ts
/**
 * Splits an array into consecutive groups of at most size elements, preserving order.
 * The final group is shorter when the length does not divide evenly.
 * An empty array yields an empty array. The input array must not be mutated.
 * Throw an Error unless size is a positive integer.
 *
 * @param items - The array to be split.
 * @param size - The maximum size of each resulting group.
 * @returns A new array containing the groups.
 */
export function chunk<T>(items: readonly T[], size: number): T[][] {
  if (size <= 0) {
    throw Error('Size must be a positive integer');
  }

  const result: T[][] = [];
  for (let i = 0; i < items.length; i += size) {
    result.push(items.slice(i, Math.min(i + size, items.length)));
  }
  return result;
}
```